summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Mobs/Behaviors/BehaviorAttacker.h1
-rw-r--r--src/Mobs/Behaviors/BehaviorAttackerRanged.cpp37
-rw-r--r--src/Mobs/Behaviors/BehaviorAttackerRanged.h12
-rw-r--r--src/Mobs/Blaze.cpp51
-rw-r--r--src/Mobs/Blaze.h11
-rw-r--r--src/Mobs/Skeleton.cpp18
-rw-r--r--src/Mobs/Spider.cpp4
7 files changed, 93 insertions, 41 deletions
diff --git a/src/Mobs/Behaviors/BehaviorAttacker.h b/src/Mobs/Behaviors/BehaviorAttacker.h
index 82a377112..428e7a340 100644
--- a/src/Mobs/Behaviors/BehaviorAttacker.h
+++ b/src/Mobs/Behaviors/BehaviorAttacker.h
@@ -78,5 +78,6 @@ private:
// The mob we want to attack
cPawn * m_Target;
int m_StrikeTickCnt;
+ bool m_ShouldRetaliate; // Should we attack back whoever attacks us?
};
diff --git a/src/Mobs/Behaviors/BehaviorAttackerRanged.cpp b/src/Mobs/Behaviors/BehaviorAttackerRanged.cpp
index 366c77c68..888dc3497 100644
--- a/src/Mobs/Behaviors/BehaviorAttackerRanged.cpp
+++ b/src/Mobs/Behaviors/BehaviorAttackerRanged.cpp
@@ -6,19 +6,38 @@
#include "../../BlockID.h"
#include "../../Entities/ArrowEntity.h"
+cBehaviorAttackerRanged::cBehaviorAttackerRanged(
+ RangedShootingFunction a_RangedShootingFunction, int a_ProjectileAmount,
+ int a_ShootingIntervals) :
+ m_ShootingFunction(a_RangedShootingFunction),
+ m_ProjectileAmount(a_ProjectileAmount),
+ m_ShootingIntervals(a_ShootingIntervals)
+{
+
+}
+
+
bool cBehaviorAttackerRanged::DoStrike(int a_StrikeTickCnt)
{
UNUSED(a_StrikeTickCnt);
- auto & Random = GetRandomProvider();
- if ((GetTarget() != nullptr) && (m_AttackCoolDownTicksLeft == 0))
+
+ // stop shooting if target is lost
+ if ((GetTarget() == nullptr))
{
- Vector3d Inaccuracy = Vector3d(Random.RandReal<double>(-0.25, 0.25), Random.RandReal<double>(-0.25, 0.25), Random.RandReal<double>(-0.25, 0.25));
- Vector3d Speed = (GetTarget()->GetPosition() + Inaccuracy - m_Parent->GetPosition()) * 5;
- Speed.y += Random.RandInt(-1, 1);
+ return true;
+ }
- auto Arrow = cpp14::make_unique<cArrowEntity>(m_Parent, m_Parent->GetPosX(), m_Parent->GetPosY() + 1, m_Parent->GetPosZ(), Speed);
- auto ArrowPtr = Arrow.get();
- ArrowPtr->Initialize(std::move(Arrow), *m_Parent->GetWorld());
+ // Stop shooting if we've shot m_ProjectileAmount times.
+ if (a_StrikeTickCnt - 1 == m_ShootingIntervals * m_ProjectileAmount)
+ {
+ return true;
+ }
+
+ // shoot once every m_ShootingIntervals.
+ // Starting immediately at first call to DoStrike
+ if ((a_StrikeTickCnt - 1) % m_ShootingIntervals == 0)
+ {
+ m_ShootingFunction(*this, *m_Parent, *GetTarget());
}
- return true; // Finish the strike. It only takes 1 tick.
+ return false;
}
diff --git a/src/Mobs/Behaviors/BehaviorAttackerRanged.h b/src/Mobs/Behaviors/BehaviorAttackerRanged.h
index 012590490..1d9e4fcd9 100644
--- a/src/Mobs/Behaviors/BehaviorAttackerRanged.h
+++ b/src/Mobs/Behaviors/BehaviorAttackerRanged.h
@@ -1,11 +1,23 @@
#pragma once
#include "BehaviorAttacker.h"
+#include <functional>
+class cBehaviorAttackerRanged;
/** Grants the mob that ability to approach a target and then melee attack it.
Use BehaviorAttackerMelee::SetTarget to attack. */
+typedef std::function<void(cBehaviorAttackerRanged & a_Behavior,
+ cMonster & a_Attacker, cPawn & a_Attacked)> RangedShootingFunction;
+
class cBehaviorAttackerRanged : public cBehaviorAttacker
{
public:
+ cBehaviorAttackerRanged(RangedShootingFunction a_RangedShootingFUnction,
+ int a_ProjectileAmount = 1, int a_ShootingIntervals = 1);
bool DoStrike(int a_StrikeTickCnt) override;
+
+private:
+ RangedShootingFunction m_ShootingFunction;
+ int m_ProjectileAmount;
+ int m_ShootingIntervals;
};
diff --git a/src/Mobs/Blaze.cpp b/src/Mobs/Blaze.cpp
index 5778c7ef2..34c784737 100644
--- a/src/Mobs/Blaze.cpp
+++ b/src/Mobs/Blaze.cpp
@@ -6,12 +6,33 @@
#include "../Entities/FireChargeEntity.h"
+void FireballShootingFunction(cBehaviorAttackerRanged & a_Behavior,
+ cMonster & a_Attacker, cPawn & a_Attacked)
+{
+ UNUSED(a_Behavior);
+
+ // Setting this higher gives us more wiggle room for attackrate
+ Vector3d Speed = a_Attacker.GetLookVector() * 4;
+ Speed.y = Speed.y + 3;
+
+ auto FireCharge = cpp14::make_unique<cFireChargeEntity>(&a_Attacker,
+ a_Attacked.GetPosX(), a_Attacked.GetPosY() + 1, a_Attacked.GetPosZ(), Speed);
+ auto FireChargePtr = FireCharge.get();
+ FireChargePtr->Initialize(std::move(FireCharge), *(a_Attacker.GetWorld()));
+}
+
+
+
cBlaze::cBlaze(void) :
- super(mtBlaze, "entity.blaze.hurt", "entity.blaze.death", 0.6, 1.8)
+ super(mtBlaze, "entity.blaze.hurt", "entity.blaze.death", 0.6, 1.8),
+ m_BehaviorAttackerRanged(FireballShootingFunction, 3, 5)
{
m_EMPersonality = AGGRESSIVE;
+ m_BehaviorAttackerRanged.AttachToMonster(*this);
+ m_BehaviorDoNothing.AttachToMonster(*this);
+ m_BehaviorAggressive.AttachToMonster(*this);
SetGravity(-8.0f);
SetAirDrag(0.05f);
GetMonsterConfig("Blaze");
@@ -29,31 +50,3 @@ void cBlaze::GetDrops(cItems & a_Drops, cEntity * a_Killer)
AddRandomDropItem(a_Drops, 0, 1 + LootingLevel, E_ITEM_BLAZE_ROD);
}
}
-
-
-
-
-// mobTODO
-/*
-bool cBlaze::Attack(std::chrono::milliseconds a_Dt)
-{
- if ((GetTarget() != nullptr) && (m_AttackCoolDownTicksLeft == 0))
- {
- // Setting this higher gives us more wiggle room for attackrate
- Vector3d Speed = GetLookVector() * 20;
- Speed.y = Speed.y + 1;
-
- auto FireCharge = cpp14::make_unique<cFireChargeEntity>(this, GetPosX(), GetPosY() + 1, GetPosZ(), Speed);
- auto FireChargePtr = FireCharge.get();
- if (!FireChargePtr->Initialize(std::move(FireCharge), *m_World))
- {
- return false;
- }
-
- ResetAttackCooldown();
- // ToDo: Shoot 3 fireballs instead of 1.
-
- return true;
- }
- return false;
-}*/
diff --git a/src/Mobs/Blaze.h b/src/Mobs/Blaze.h
index 1fb165a37..aad6b4af6 100644
--- a/src/Mobs/Blaze.h
+++ b/src/Mobs/Blaze.h
@@ -1,6 +1,9 @@
#pragma once
#include "Monster.h"
+#include "Behaviors/BehaviorAttackerRanged.h"
+#include "Behaviors/BehaviorDoNothing.h"
+#include "Behaviors/BehaviorAggressive.h"
class cBlaze :
public cMonster
@@ -13,4 +16,12 @@ public:
CLASS_PROTODEF(cBlaze)
virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override;
+
+private:
+ // tick behaviors
+ cBehaviorAttackerRanged m_BehaviorAttackerRanged;
+ cBehaviorDoNothing m_BehaviorDoNothing;
+
+ // other behaviors
+ cBehaviorAggressive m_BehaviorAggressive;
} ;
diff --git a/src/Mobs/Skeleton.cpp b/src/Mobs/Skeleton.cpp
index fe7c6182e..4567ce1d1 100644
--- a/src/Mobs/Skeleton.cpp
+++ b/src/Mobs/Skeleton.cpp
@@ -6,12 +6,26 @@
#include "../Entities/ArrowEntity.h"
#include "ClientHandle.h"
-
+void ArrowShootingFunction(cBehaviorAttackerRanged & a_Behavior,
+ cMonster & a_Attacker, cPawn & a_Attacked)
+{
+ UNUSED(a_Behavior);
+ auto & Random = GetRandomProvider();
+ Vector3d Inaccuracy = Vector3d(Random.RandReal<double>(-0.25, 0.25), Random.RandReal<double>(-0.25, 0.25), Random.RandReal<double>(-0.25, 0.25));
+ Vector3d Speed = (a_Attacked.GetPosition() + Inaccuracy - a_Attacker.GetPosition()) * 5;
+ Speed.y += Random.RandInt(-1, 1);
+
+ auto Arrow = cpp14::make_unique<cArrowEntity>(&a_Attacker,
+ a_Attacked.GetPosX(), a_Attacked.GetPosY() + 1, a_Attacked.GetPosZ(), Speed);
+ auto ArrowPtr = Arrow.get();
+ ArrowPtr->Initialize(std::move(Arrow), *(a_Attacker.GetWorld()));
+}
cSkeleton::cSkeleton(bool IsWither) :
super(mtSkeleton, "entity.skeleton.hurt", "entity.skeleton.death", 0.6, 1.8),
- m_bIsWither(IsWither)
+ m_bIsWither(IsWither),
+ m_BehaviorAttackerRanged(ArrowShootingFunction)
{
m_EMPersonality = AGGRESSIVE;
m_BehaviorAttackerRanged.AttachToMonster(*this);
diff --git a/src/Mobs/Spider.cpp b/src/Mobs/Spider.cpp
index 0a824aca2..6db7905ec 100644
--- a/src/Mobs/Spider.cpp
+++ b/src/Mobs/Spider.cpp
@@ -25,8 +25,10 @@ bool AggressiveAtNightFunction(cBehaviorAggressive & a_Behavior, cMonster & a_Mo
!((Chunk->GetSkyLightAltered(Rel.x, Rel.y, Rel.z) > 11) || (Chunk->GetBlockLight(Rel.x, Rel.y, Rel.z) > 11))
)
{
- return true;
+ return true;
}
+
+ return false;
}
cSpider::cSpider(void) :