diff options
author | Samuel Barney <samjbarney@gmail.com> | 2014-07-10 22:57:31 +0200 |
---|---|---|
committer | Samuel Barney <samjbarney@gmail.com> | 2014-07-10 22:57:31 +0200 |
commit | c91ea64666f1d7f3a239e3f0ae1bfe7f27a036e0 (patch) | |
tree | 02ed5300c6db047a54eca76ff58c264f93cfcdd7 /src/Entities | |
parent | Added temporary function to allow me to grab the sight distance (diff) | |
download | cuberite-c91ea64666f1d7f3a239e3f0ae1bfe7f27a036e0.tar cuberite-c91ea64666f1d7f3a239e3f0ae1bfe7f27a036e0.tar.gz cuberite-c91ea64666f1d7f3a239e3f0ae1bfe7f27a036e0.tar.bz2 cuberite-c91ea64666f1d7f3a239e3f0ae1bfe7f27a036e0.tar.lz cuberite-c91ea64666f1d7f3a239e3f0ae1bfe7f27a036e0.tar.xz cuberite-c91ea64666f1d7f3a239e3f0ae1bfe7f27a036e0.tar.zst cuberite-c91ea64666f1d7f3a239e3f0ae1bfe7f27a036e0.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Entities/Compoments/AIAgressiveComponent.h | 4 | ||||
-rw-r--r-- | src/Entities/Compoments/AIAgresssiveComponent.cpp | 95 |
2 files changed, 97 insertions, 2 deletions
diff --git a/src/Entities/Compoments/AIAgressiveComponent.h b/src/Entities/Compoments/AIAgressiveComponent.h index 338d311e3..2cd3f2304 100644 --- a/src/Entities/Compoments/AIAgressiveComponent.h +++ b/src/Entities/Compoments/AIAgressiveComponent.h @@ -3,6 +3,7 @@ class cAIAgressiveComponent : public cAIComponent { protected: + enum MState{ATTACKING, IDLE, CHASING, ESCAPING} m_EMState; cEntity * m_Target; public: cAIAgressiveComponent(cMonster * a_Monster) : cAIComponent(a_Monster), m_Target(null){} @@ -14,5 +15,8 @@ public: virtual void Attack(float a_Dt); protected: + virtual void EventLosePlayer(void); + virtual void CheckEventLostPlayer(void); + bool IsMovingToTargetPosition(); }
\ No newline at end of file diff --git a/src/Entities/Compoments/AIAgresssiveComponent.cpp b/src/Entities/Compoments/AIAgresssiveComponent.cpp index ae89ef5b9..4dcd3e618 100644 --- a/src/Entities/Compoments/AIAgresssiveComponent.cpp +++ b/src/Entities/Compoments/AIAgresssiveComponent.cpp @@ -1,5 +1,34 @@ #include "AIAgressiveComponent.h" + + +void cAIAggressiveComponent::Tick(float a_Dt, cChunk & a_Chunk) +{ + super::Tick(a_Dt, a_Chunk); + + if (m_EMState == CHASING) + { + CheckEventLostPlayer(); + } + else + { + CheckEventSeePlayer(); + } + + if (m_Target == NULL) + return; + + cTracer LineOfSight(GetWorld()); + Vector3d AttackDirection(m_Target->GetPosition() - GetPosition()); + + if (ReachedFinalDestination() && !LineOfSight.Trace(GetPosition(), AttackDirection, (int)AttackDirection.Length())) + { + // Attack if reached destination, target isn't null, and have a clear line of sight to target (so won't attack through walls) + Attack(a_Dt / 1000); + } +} + + void cAIAggressiveComponent::Attack(float a_Dt) { float attack_interval = m_Self->GetAttackInterval(); @@ -9,12 +38,13 @@ void cAIAggressiveComponent::Attack(float a_Dt) { // Setting this higher gives us more wiggle room for attackrate attack_interval = 0.0f; - m_Target->TakeDamage(dtMobAttack, this, m_AttackDamage, 0); + m_Target->TakeDamage(dtMobAttack, m_Self, m_AttackDamage, 0); } m_Self->SetAttackInterval(attack_interval) } + bool cAIAggressiveComponent::IsMovingToTargetPosition() { // Difference between destination x and target x is negligible (to 10^-12 precision) @@ -28,4 +58,65 @@ bool cAIAggressiveComponent::IsMovingToTargetPosition() return false; } return true; -}
\ No newline at end of file +} + + +/// Event Checkers +void cAIAggressiveComponent::CheckEventLostPlayer(void) +{ + if (m_Target != NULL) + { + if ((m_Target->GetPosition() - GetPosition()).Length() > m_Self->GetSightDistance()) + { + EventLosePlayer(); + } + } + else + { + EventLosePlayer(); + } +} + + +/// Event Handlers +void cAggressiveMonster::EventSeePlayer(cEntity * a_Entity) +{ + if (!((cPlayer *)a_Entity)->IsGameModeCreative()) + { + m_Target = a_Entity; + m_EMState = CHASING; + } +} + + +void cAIAggressiveComponent::EventLosePlayer(void) +{ + m_Target = NULL; + m_EMState = IDLE; +} + + +/// State Logic +void cAggressiveMonster::InStateChasing(float a_Dt) +{ + super::InStateChasing(a_Dt); + + if (m_Target != NULL) + { + if (m_Target->IsPlayer()) + { + if (((cPlayer *)m_Target)->IsGameModeCreative()) + { + m_EMState = IDLE; + return; + } + } + + if (!IsMovingToTargetPosition()) + { + m_Self->MoveToPosition(m_Target->GetPosition()); + } + } +} + + |