summaryrefslogtreecommitdiffstats
path: root/src/Mobs/Behaviors
diff options
context:
space:
mode:
Diffstat (limited to 'src/Mobs/Behaviors')
-rw-r--r--src/Mobs/Behaviors/Behavior.cpp5
-rw-r--r--src/Mobs/Behaviors/Behavior.h9
-rw-r--r--src/Mobs/Behaviors/BehaviorAggressive.cpp14
-rw-r--r--src/Mobs/Behaviors/BehaviorAggressive.h2
-rw-r--r--src/Mobs/Behaviors/BehaviorBreeder.cpp18
-rw-r--r--src/Mobs/Behaviors/BehaviorBreeder.h5
-rw-r--r--src/Mobs/Behaviors/BehaviorChaser.cpp21
-rw-r--r--src/Mobs/Behaviors/BehaviorChaser.h3
-rw-r--r--src/Mobs/Behaviors/BehaviorCoward.cpp17
-rw-r--r--src/Mobs/Behaviors/BehaviorCoward.h3
-rw-r--r--src/Mobs/Behaviors/BehaviorDayLightBurner.cpp13
-rw-r--r--src/Mobs/Behaviors/BehaviorDayLightBurner.h6
-rw-r--r--src/Mobs/Behaviors/BehaviorItemFollower.cpp7
-rw-r--r--src/Mobs/Behaviors/BehaviorItemFollower.h2
-rw-r--r--src/Mobs/Behaviors/BehaviorWanderer.cpp40
-rw-r--r--src/Mobs/Behaviors/BehaviorWanderer.h9
16 files changed, 123 insertions, 51 deletions
diff --git a/src/Mobs/Behaviors/Behavior.cpp b/src/Mobs/Behaviors/Behavior.cpp
index 695343732..86922427f 100644
--- a/src/Mobs/Behaviors/Behavior.cpp
+++ b/src/Mobs/Behaviors/Behavior.cpp
@@ -1,8 +1,7 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Behavior.h"
-
-
+#include "../Monster.h"
@@ -76,7 +75,7 @@ void cBehavior::PreTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
-void cBehavior::onRightClicked()
+void cBehavior::OnRightClicked(cPlayer & a_Player)
{
LOGD("ERROR: Called onRightClicked on a behavior that doesn't have one.");
ASSERT(1 == 0);
diff --git a/src/Mobs/Behaviors/Behavior.h b/src/Mobs/Behaviors/Behavior.h
index 989addf8d..0b554ec62 100644
--- a/src/Mobs/Behaviors/Behavior.h
+++ b/src/Mobs/Behaviors/Behavior.h
@@ -2,11 +2,16 @@
struct TakeDamageInfo;
class cChunk;
+class cPlayer;
+class cMonster;
#include <chrono>
class cBehavior
{
public:
+ virtual void AttachToMonster(cMonster & a_Parent) = 0;
+
+ // Tick-related
virtual bool IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
virtual bool ControlStarting(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
virtual bool ControlEnding(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
@@ -14,8 +19,8 @@ public:
virtual void PostTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
virtual void PreTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
-
- virtual void onRightClicked();
+ // Other
+ virtual void OnRightClicked(cPlayer & a_Player);
virtual void Destroyed();
virtual void DoTakeDamage(TakeDamageInfo & a_TDI);
virtual ~cBehavior() {}
diff --git a/src/Mobs/Behaviors/BehaviorAggressive.cpp b/src/Mobs/Behaviors/BehaviorAggressive.cpp
index f7c553f52..65618e123 100644
--- a/src/Mobs/Behaviors/BehaviorAggressive.cpp
+++ b/src/Mobs/Behaviors/BehaviorAggressive.cpp
@@ -6,21 +6,17 @@
#include "../../Chunk.h"
#include "../../Entities/Player.h"
-
-
-cBehaviorAggressive::cBehaviorAggressive(cMonster * a_Parent) : m_Parent(a_Parent)
+void cBehaviorAggressive::AttachToMonster(cMonster & a_Parent)
{
- ASSERT(m_Parent != nullptr);
- m_ParentChaser = m_Parent->GetBehaviorChaser();
- ASSERT(m_ParentChaser != nullptr);
+ m_Parent = &a_Parent;
+ m_Parent->AttachPreTickBehavior(this);
}
-
-
-
void cBehaviorAggressive::PreTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
+ UNUSED(a_Dt);
+ UNUSED(a_Chunk);
// Target something new if we have no target
if (m_ParentChaser->GetTarget() == nullptr)
{
diff --git a/src/Mobs/Behaviors/BehaviorAggressive.h b/src/Mobs/Behaviors/BehaviorAggressive.h
index 5bf86b23f..7dc7060c7 100644
--- a/src/Mobs/Behaviors/BehaviorAggressive.h
+++ b/src/Mobs/Behaviors/BehaviorAggressive.h
@@ -19,7 +19,7 @@ class cBehaviorAggressive : public cBehavior
{
public:
- cBehaviorAggressive(cMonster * a_Parent);
+ void AttachToMonster(cMonster & a_Parent) override;
// cBehaviorAggressive(cMonster * a_Parent, bool a_HatesPlayer);
// TODO agression toward specific players, and specific mobtypes, etc
diff --git a/src/Mobs/Behaviors/BehaviorBreeder.cpp b/src/Mobs/Behaviors/BehaviorBreeder.cpp
index b2bd8da5d..b32fafc8c 100644
--- a/src/Mobs/Behaviors/BehaviorBreeder.cpp
+++ b/src/Mobs/Behaviors/BehaviorBreeder.cpp
@@ -9,15 +9,25 @@
#include "../../Item.h"
#include "../../BoundingBox.h"
-cBehaviorBreeder::cBehaviorBreeder(cMonster * a_Parent) :
- m_Parent(a_Parent),
+cBehaviorBreeder::cBehaviorBreeder() :
m_LovePartner(nullptr),
m_LoveTimer(0),
m_LoveCooldown(0),
m_MatingTimer(0)
{
- m_Parent = a_Parent;
- ASSERT(m_Parent != nullptr);
+}
+
+
+
+
+
+void cBehaviorBreeder::AttachToMonster(cMonster & a_Parent)
+{
+ m_Parent = &a_Parent;
+ m_Parent->AttachTickBehavior(this);
+ m_Parent->AttachPostTickBehavior(this);
+ m_Parent->AttachRightClickBehavior(this);
+ m_Parent->AttachDestroyBehavior(this);
}
diff --git a/src/Mobs/Behaviors/BehaviorBreeder.h b/src/Mobs/Behaviors/BehaviorBreeder.h
index cf030b8f3..576234066 100644
--- a/src/Mobs/Behaviors/BehaviorBreeder.h
+++ b/src/Mobs/Behaviors/BehaviorBreeder.h
@@ -18,13 +18,14 @@ class cBehaviorBreeder : public cBehavior
{
public:
- cBehaviorBreeder(cMonster * a_Parent);
+ cBehaviorBreeder();
+ void AttachToMonster(cMonster & a_Parent) override;
// Functions our host Monster should invoke:
bool IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
void PostTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
- void OnRightClicked(cPlayer & a_Player);
+ void OnRightClicked(cPlayer & a_Player) override;
void Destroyed() override;
/** Returns the partner which the monster is currently mating with. */
diff --git a/src/Mobs/Behaviors/BehaviorChaser.cpp b/src/Mobs/Behaviors/BehaviorChaser.cpp
index 025c60dd0..8e2a70bd4 100644
--- a/src/Mobs/Behaviors/BehaviorChaser.cpp
+++ b/src/Mobs/Behaviors/BehaviorChaser.cpp
@@ -9,20 +9,33 @@
-cBehaviorChaser::cBehaviorChaser(cMonster * a_Parent) :
- m_Parent(a_Parent)
- , m_AttackRate(3)
+cBehaviorChaser::cBehaviorChaser() :
+ m_AttackRate(3)
, m_AttackDamage(1)
, m_AttackRange(1)
, m_AttackCoolDownTicksLeft(0)
, m_TicksSinceLastDamaged(100)
{
- ASSERT(m_Parent != nullptr);
+
+}
+
+
+
+
+
+void cBehaviorChaser::AttachToMonster(cMonster & a_Parent)
+{
+ m_Parent = &a_Parent;
+ m_Parent->AttachTickBehavior(this);
+ m_Parent->AttachDestroyBehavior(this);
+ m_Parent->AttachPostTickBehavior(this);
+ m_Parent->AttachDoTakeDamageBehavior(this);
}
+
bool cBehaviorChaser::IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
UNUSED(a_Dt);
diff --git a/src/Mobs/Behaviors/BehaviorChaser.h b/src/Mobs/Behaviors/BehaviorChaser.h
index d0910e11e..a40be6aba 100644
--- a/src/Mobs/Behaviors/BehaviorChaser.h
+++ b/src/Mobs/Behaviors/BehaviorChaser.h
@@ -17,7 +17,8 @@ class cBehaviorChaser : public cBehavior
{
public:
- cBehaviorChaser(cMonster * a_Parent);
+ cBehaviorChaser();
+ void AttachToMonster(cMonster & a_Parent) override;
// Functions our host Monster should invoke:
bool IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
diff --git a/src/Mobs/Behaviors/BehaviorCoward.cpp b/src/Mobs/Behaviors/BehaviorCoward.cpp
index 43be5c49c..d23da5ac6 100644
--- a/src/Mobs/Behaviors/BehaviorCoward.cpp
+++ b/src/Mobs/Behaviors/BehaviorCoward.cpp
@@ -6,16 +6,27 @@
#include "../../Entities/Player.h"
#include "../../Entities/Entity.h"
-cBehaviorCoward::cBehaviorCoward(cMonster * a_Parent) :
- m_Parent(a_Parent),
+cBehaviorCoward::cBehaviorCoward() :
m_Attacker(nullptr)
{
- ASSERT(m_Parent != nullptr);
}
+void cBehaviorCoward::AttachToMonster(cMonster & a_Parent)
+{
+ m_Parent = &a_Parent;
+ m_Parent->AttachTickBehavior(this);
+ m_Parent->AttachDestroyBehavior(this);
+ m_Parent->AttachPostTickBehavior(this);
+ m_Parent->AttachDoTakeDamageBehavior(this);
+}
+
+
+
+
+
bool cBehaviorCoward::IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
UNUSED(a_Dt);
diff --git a/src/Mobs/Behaviors/BehaviorCoward.h b/src/Mobs/Behaviors/BehaviorCoward.h
index 048101d5d..b72dbaf41 100644
--- a/src/Mobs/Behaviors/BehaviorCoward.h
+++ b/src/Mobs/Behaviors/BehaviorCoward.h
@@ -13,7 +13,8 @@ struct TakeDamageInfo;
class cBehaviorCoward : cBehavior
{
public:
- cBehaviorCoward(cMonster * a_Parent);
+ cBehaviorCoward();
+ void AttachToMonster(cMonster & a_Parent) override;
// Functions our host Monster should invoke:
bool IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
diff --git a/src/Mobs/Behaviors/BehaviorDayLightBurner.cpp b/src/Mobs/Behaviors/BehaviorDayLightBurner.cpp
index b28c3c49c..efa4b6518 100644
--- a/src/Mobs/Behaviors/BehaviorDayLightBurner.cpp
+++ b/src/Mobs/Behaviors/BehaviorDayLightBurner.cpp
@@ -7,11 +7,20 @@
#include "../../Chunk.h"
-cBehaviorDayLightBurner::cBehaviorDayLightBurner(cMonster * a_Parent) : m_Parent(a_Parent)
+
+
+
+
+void cBehaviorDayLightBurner::AttachToMonster(cMonster & a_Parent)
{
- ASSERT(m_Parent != nullptr);
+ m_Parent = &a_Parent;
+ m_Parent->AttachPostTickBehavior(this);
}
+
+
+
+
void cBehaviorDayLightBurner::PostTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
// mobTodo WouldBurn
diff --git a/src/Mobs/Behaviors/BehaviorDayLightBurner.h b/src/Mobs/Behaviors/BehaviorDayLightBurner.h
index 77fcce281..0acdeea40 100644
--- a/src/Mobs/Behaviors/BehaviorDayLightBurner.h
+++ b/src/Mobs/Behaviors/BehaviorDayLightBurner.h
@@ -12,13 +12,13 @@ class cChunk;
class cBehaviorDayLightBurner : cBehavior
{
public:
- cBehaviorDayLightBurner(cMonster * a_Parent);
+ void AttachToMonster(cMonster & a_Parent) override;
- void PostTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
+ void PostTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
bool WouldBurnAt(Vector3d a_Location, cChunk & a_Chunk);
// Functions our host Monster should invoke:
- void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
+ void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
private:
cMonster * m_Parent; // Our Parent
diff --git a/src/Mobs/Behaviors/BehaviorItemFollower.cpp b/src/Mobs/Behaviors/BehaviorItemFollower.cpp
index c6cb0df04..d10b46776 100644
--- a/src/Mobs/Behaviors/BehaviorItemFollower.cpp
+++ b/src/Mobs/Behaviors/BehaviorItemFollower.cpp
@@ -6,11 +6,10 @@
#include "../../Entities/Player.h"
-cBehaviorItemFollower::cBehaviorItemFollower(cMonster * a_Parent) :
- m_Parent(a_Parent)
+void cBehaviorItemFollower::AttachToMonster(cMonster & a_Parent)
{
- m_Parent = a_Parent;
- ASSERT(m_Parent != nullptr);
+ m_Parent = &a_Parent;
+ m_Parent->AttachTickBehavior(this);
}
diff --git a/src/Mobs/Behaviors/BehaviorItemFollower.h b/src/Mobs/Behaviors/BehaviorItemFollower.h
index 94f8bda15..277323ab1 100644
--- a/src/Mobs/Behaviors/BehaviorItemFollower.h
+++ b/src/Mobs/Behaviors/BehaviorItemFollower.h
@@ -12,7 +12,7 @@ class cItems;
class cBehaviorItemFollower : public cBehavior
{
public:
- cBehaviorItemFollower(cMonster * a_Parent);
+ void AttachToMonster(cMonster & a_Parent) override;
void GetBreedingItems(cItems & a_Items);
diff --git a/src/Mobs/Behaviors/BehaviorWanderer.cpp b/src/Mobs/Behaviors/BehaviorWanderer.cpp
index 53efd407c..44dda077b 100644
--- a/src/Mobs/Behaviors/BehaviorWanderer.cpp
+++ b/src/Mobs/Behaviors/BehaviorWanderer.cpp
@@ -4,18 +4,43 @@
#include "../../Chunk.h"
#include "../../World.h"
-cBehaviorWanderer::cBehaviorWanderer(cMonster * a_Parent)
- : m_Parent(a_Parent)
- , m_IdleInterval(0)
+cBehaviorWanderer::cBehaviorWanderer() : m_IdleInterval(0)
{
- ASSERT(m_Parent != nullptr);
+
}
-bool cBehaviorWanderer::ActiveTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
+
+
+
+
+void cBehaviorWanderer::AttachToMonster(cMonster & a_Parent)
+{
+ m_Parent = &a_Parent;
+ m_Parent->AttachTickBehavior(this);
+}
+
+
+
+
+
+bool cBehaviorWanderer::IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
+{
+ // Wandering behavior always happily accepts control.
+ // It should therefore be the last one attached to a monster.
+ UNUSED(a_Dt);
+ UNUSED(a_Chunk);
+ return true;
+}
+
+
+
+
+
+void cBehaviorWanderer::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
if (m_Parent->IsPathFinderActivated())
{
- return true; // Still getting there
+ return; // Still getting there
}
m_IdleInterval += a_Dt;
@@ -38,7 +63,7 @@ bool cBehaviorWanderer::ActiveTick(std::chrono::milliseconds a_Dt, cChunk & a_Ch
cChunk * Chunk = a_Chunk.GetNeighborChunk(static_cast<int>(Destination.x), static_cast<int>(Destination.z));
if ((Chunk == nullptr) || !Chunk->IsValid())
{
- return true;
+ return;
}
BLOCKTYPE BlockType;
@@ -56,5 +81,4 @@ bool cBehaviorWanderer::ActiveTick(std::chrono::milliseconds a_Dt, cChunk & a_Ch
}
}
}
- return true;
}
diff --git a/src/Mobs/Behaviors/BehaviorWanderer.h b/src/Mobs/Behaviors/BehaviorWanderer.h
index 258bf5f59..90dddcebc 100644
--- a/src/Mobs/Behaviors/BehaviorWanderer.h
+++ b/src/Mobs/Behaviors/BehaviorWanderer.h
@@ -2,19 +2,22 @@
// The mob will wander around
#include <chrono>
+#include "Behavior.h"
class cMonster;
class cEntity;
class cChunk;
-class cBehaviorWanderer
+class cBehaviorWanderer : cBehavior
{
public:
- cBehaviorWanderer(cMonster * a_Parent);
+ cBehaviorWanderer();
+ void AttachToMonster(cMonster & a_Parent) override;
// Functions our host Monster should invoke:
- bool ActiveTick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
+ bool IsControlDesired(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
+ void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
private: