diff options
author | Samuel Barney <samjbarney@gmail.com> | 2014-08-20 23:42:51 +0200 |
---|---|---|
committer | Samuel Barney <samjbarney@gmail.com> | 2014-08-20 23:42:51 +0200 |
commit | ece054bd6bc37922df910942eb8f2b0c2cfb720c (patch) | |
tree | dcd43a13e16994d605bd0e604a7b0c839f533faf /src/Mobs | |
parent | Got the core of NewMonster working (diff) | |
download | cuberite-ece054bd6bc37922df910942eb8f2b0c2cfb720c.tar cuberite-ece054bd6bc37922df910942eb8f2b0c2cfb720c.tar.gz cuberite-ece054bd6bc37922df910942eb8f2b0c2cfb720c.tar.bz2 cuberite-ece054bd6bc37922df910942eb8f2b0c2cfb720c.tar.lz cuberite-ece054bd6bc37922df910942eb8f2b0c2cfb720c.tar.xz cuberite-ece054bd6bc37922df910942eb8f2b0c2cfb720c.tar.zst cuberite-ece054bd6bc37922df910942eb8f2b0c2cfb720c.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Mobs/Components/AIAggressiveComponent.h | 12 | ||||
-rw-r--r-- | src/Mobs/Components/AIAggresssiveComponent.cpp | 138 | ||||
-rw-r--r-- | src/Mobs/Components/AIComponent.cpp | 4 | ||||
-rw-r--r-- | src/Mobs/Components/AIComponent.h | 9 |
4 files changed, 10 insertions, 153 deletions
diff --git a/src/Mobs/Components/AIAggressiveComponent.h b/src/Mobs/Components/AIAggressiveComponent.h index 28bfa7b2d..73c0f972e 100644 --- a/src/Mobs/Components/AIAggressiveComponent.h +++ b/src/Mobs/Components/AIAggressiveComponent.h @@ -7,19 +7,9 @@ protected: enum MState{ATTACKING, IDLE, CHASING, ESCAPING} m_EMState; cEntity * m_Target; public: - cAIAggressiveComponent(cMonster * a_Monster); + cAIAggressiveComponent(cNewMonster * a_Monster); virtual void Tick(float a_Dt, cChunk & a_Chunk) override; - virtual void InStateChasing(float a_Dt); virtual void Attack(float a_Dt); - -protected: - virtual void CheckEventLostPlayer(void); - virtual void CheckEventSeePlayer(void); - virtual void EventLosePlayer(void); - virtual void EventSeePlayer(cEntity *); - - bool IsMovingToTargetPosition(); - bool ReachedFinalDestination(); }; diff --git a/src/Mobs/Components/AIAggresssiveComponent.cpp b/src/Mobs/Components/AIAggresssiveComponent.cpp index cd7e34e2f..8bbe42b75 100644 --- a/src/Mobs/Components/AIAggresssiveComponent.cpp +++ b/src/Mobs/Components/AIAggresssiveComponent.cpp @@ -1,155 +1,21 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "AIAggressiveComponent.h" -#include "../Entities/Player.h" -#include "../Tracer.h" #include <iostream> -#include "Monster.h" +#include "../NewMonster.h" -cAIAggressiveComponent::cAIAggressiveComponent(cMonster * a_Monster) : cAIComponent(a_Monster), m_Target(NULL){} +cAIAggressiveComponent::cAIAggressiveComponent(cNewMonster * a_Monster) : cAIComponent(a_Monster), m_Target(NULL){} 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(m_Self->GetWorld()); - Vector3d AttackDirection(m_Target->GetPosition() - m_Self->GetPosition()); - - if (ReachedFinalDestination() && !LineOfSight.Trace(m_Self->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(); - attack_interval += a_Dt * m_Self->GetAttackRate(); - - if ((m_Target != NULL) && (attack_interval > 3.0)) - { - // Setting this higher gives us more wiggle room for attackrate - attack_interval = 0.0f; - m_Target->TakeDamage(dtMobAttack, m_Self, m_Self->GetAttackDamage(), 0); - } - - m_Self->SetAttackInterval(attack_interval); -} - - -bool cAIAggressiveComponent::IsMovingToTargetPosition() -{ - // Difference between destination x and target x is negligible (to 10^-12 precision) - if (fabsf((float)m_Self->m_FinalDestination.x - (float)m_Target->GetPosX()) < std::numeric_limits<float>::epsilon()) - { - return false; - } - // Difference between destination z and target z is negligible (to 10^-12 precision) - else if (fabsf((float)m_Self->m_FinalDestination.z - (float)m_Target->GetPosZ()) > std::numeric_limits<float>::epsilon()) - { - return false; - } - return true; -} - - -bool cAIAggressiveComponent::ReachedFinalDestination() -{ - if ((m_Self->GetPosition() - m_Self->m_FinalDestination).Length() <= m_Self->GetAttackRange()) - { - return true; - } - - return false; } -/// Event Checkers -//Checks to see if EventSeePlayer should be fired -//monster sez: Do I see the player -void cAIAggressiveComponent::CheckEventSeePlayer(void) -{ - // TODO: Rewrite this to use cWorld's DoWithPlayers() - cPlayer * Closest = m_Self->GetWorld()->FindClosestPlayer(m_Self->GetPosition(), (float)m_Self->GetSightDistance(), false); - - if (Closest != NULL) - { - std::cout << "Found Player\n"; - EventSeePlayer(Closest); - } -} - - -void cAIAggressiveComponent::CheckEventLostPlayer(void) -{ - if (m_Target != NULL) - { - if ((m_Target->GetPosition() - m_Self->GetPosition()).Length() > m_Self->GetSightDistance()) - { - EventLosePlayer(); - } - } - else - { - EventLosePlayer(); - } -} - - -/// Event Handlers -void cAIAggressiveComponent::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 cAIAggressiveComponent::InStateChasing(float 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()); - } - } -} - - diff --git a/src/Mobs/Components/AIComponent.cpp b/src/Mobs/Components/AIComponent.cpp index 3f0a908b1..c7ac34947 100644 --- a/src/Mobs/Components/AIComponent.cpp +++ b/src/Mobs/Components/AIComponent.cpp @@ -1,5 +1,5 @@ #include "Globals.h" #include "AIComponent.h" -#include "Monster.h" +#include "../NewMonster.h" -cAIComponent::cAIComponent(cMonster * a_Entity) : m_Self(a_Entity){} +cAIComponent::cAIComponent(cNewMonster * a_Entity) : m_Self(a_Entity){} diff --git a/src/Mobs/Components/AIComponent.h b/src/Mobs/Components/AIComponent.h index 3165f6c4f..0e43db3a0 100644 --- a/src/Mobs/Components/AIComponent.h +++ b/src/Mobs/Components/AIComponent.h @@ -1,14 +1,15 @@ #pragma once -#include "../Entities/Entity.h" -class cMonster; +class cNewMonster; +class cEntity; +class cChunk; class cAIComponent { protected: - cMonster * m_Self; + cNewMonster * m_Self; public: - cAIComponent(cMonster * a_Entity); + cAIComponent(cNewMonster * a_Entity); virtual ~cAIComponent(){} virtual void Tick(float a_Dt, cChunk & a_Chunk){} |