diff options
-rw-r--r-- | src/Mobs/Components/BurningComponent.h | 21 | ||||
-rw-r--r-- | src/Mobs/Components/BurningComponent.inc | 36 | ||||
-rw-r--r-- | src/Mobs/Components/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/Mobs/Components/EnvironmentComponent.cpp | 49 | ||||
-rw-r--r-- | src/Mobs/Components/EnvironmentComponent.h | 9 | ||||
-rw-r--r-- | src/Mobs/Monster.cpp | 1 | ||||
-rw-r--r-- | src/Mobs/Zombie.cpp | 3 | ||||
-rw-r--r-- | src/Mobs/Zombie.h | 11 |
8 files changed, 71 insertions, 63 deletions
diff --git a/src/Mobs/Components/BurningComponent.h b/src/Mobs/Components/BurningComponent.h new file mode 100644 index 000000000..d77a9dd30 --- /dev/null +++ b/src/Mobs/Components/BurningComponent.h @@ -0,0 +1,21 @@ + +#pragma once + +class cChunk; + +// This class is paramerised to allow mocking +template <class EntityType, class ChunkType> +class cBurningComponent +{ +private: + EntityType & m_Self; +public: + + cBurningComponent(EntityType & a_Self) + : m_Self(a_Self) + {} + + void Tick(float a_Dt, ChunkType & a_Chunk); +}; + +#include "BurningComponent.inc" diff --git a/src/Mobs/Components/BurningComponent.inc b/src/Mobs/Components/BurningComponent.inc new file mode 100644 index 000000000..432879700 --- /dev/null +++ b/src/Mobs/Components/BurningComponent.inc @@ -0,0 +1,36 @@ + +#include "BurningComponent.h" + +template <class EntityType, class ChunkType> +void cBurningComponent<EntityType, ChunkType>::Tick(float a_Dt, ChunkType & a_Chunk) +{ + + int RelY = (int)floor(m_Self.GetPosY()); + if ((RelY < 0) || (RelY >= cChunkDef::Height)) + { + // Outside the world + return; + } + int PosX = (int)floor(m_Self.GetPosX()); + int PosZ = (int)floor(m_Self.GetPosZ()); + int RelX = PosX - m_Self.GetChunkX() * cChunkDef::Width; + int RelZ = PosZ - m_Self.GetChunkZ() * cChunkDef::Width; + + if (!a_Chunk.IsLightValid()) + { + a_Chunk.GetWorld()->QueueLightChunk(m_Self.GetChunkX(), m_Self.GetChunkZ()); + return; + } + + if ( + (a_Chunk.GetSkyLight(RelX, RelY, RelZ) == 15) && // In the daylight + (a_Chunk.GetBlock(RelX, RelY, RelZ) != E_BLOCK_SOULSAND) && // Not on soulsand + (a_Chunk.GetWorld()->GetTimeOfDay() < (12000 + 1000)) && // It is nighttime + !m_Self.IsOnFire() && // Not already burning + a_Chunk.GetWorld()->IsWeatherWetAt(PosX, PosZ) // Not raining + ) + { + // Burn for 100 ticks, then decide again + m_Self.StartBurning(100); + } +} diff --git a/src/Mobs/Components/CMakeLists.txt b/src/Mobs/Components/CMakeLists.txt index a933dc9e0..ce4fd317a 100644 --- a/src/Mobs/Components/CMakeLists.txt +++ b/src/Mobs/Components/CMakeLists.txt @@ -14,9 +14,11 @@ SET (HDRS AIComponent.h AIAggressiveComponent.h AttackComponent.h + BurningComponent.inc + BurningComponent.h EnvironmentComponent.h MovementComponent.h) if(NOT MSVC) add_library(Components ${SRCS} ${HDRS}) -endif()
\ No newline at end of file +endif() diff --git a/src/Mobs/Components/EnvironmentComponent.cpp b/src/Mobs/Components/EnvironmentComponent.cpp index b44542683..27a7d47fa 100644 --- a/src/Mobs/Components/EnvironmentComponent.cpp +++ b/src/Mobs/Components/EnvironmentComponent.cpp @@ -7,52 +7,3 @@ cEnvironmentComponent::cEnvironmentComponent(cMonster * a_Entity, int a_SightDistance) : m_Self(a_Entity), m_SightDistance(a_SightDistance){} - - - -void cEnvironmentComponent::Tick(float a_Dt, cChunk & a_Chunk) { - - // Burning in daylight - HandleDaylightBurning(a_Chunk); -} - - - - - -void cEnvironmentComponent::HandleDaylightBurning(cChunk & a_Chunk) -{ - if (!m_BurnsInDaylight) - { - return; - } - - int RelY = (int)floor(m_Self->GetPosY()); - if ((RelY < 0) || (RelY >= cChunkDef::Height)) - { - // Outside the world - return; - } - int PosX = (int)floor(m_Self->GetPosX()); - int PosZ = (int)floor(m_Self->GetPosX()); - int RelX = PosX - m_Self->GetChunkX() * cChunkDef::Width; - int RelZ = PosZ - m_Self->GetChunkZ() * cChunkDef::Width; - - if (!a_Chunk.IsLightValid()) - { - m_Self->GetWorld()->QueueLightChunk(m_Self->GetChunkX(), m_Self->GetChunkZ()); - return; - } - - if ( - (a_Chunk.GetSkyLight(RelX, RelY, RelZ) == 15) && // In the daylight - (a_Chunk.GetBlock(RelX, RelY, RelZ) != E_BLOCK_SOULSAND) && // Not on soulsand - (m_Self->GetWorld()->GetTimeOfDay() < (12000 + 1000)) && // It is nighttime - !m_Self->IsOnFire() && // Not already burning - m_Self->GetWorld()->IsWeatherWetAt(PosX, PosZ) // Not raining - ) - { - // Burn for 100 ticks, then decide again - m_Self->StartBurning(100); - } -} diff --git a/src/Mobs/Components/EnvironmentComponent.h b/src/Mobs/Components/EnvironmentComponent.h index 078e81501..60a5c3c42 100644 --- a/src/Mobs/Components/EnvironmentComponent.h +++ b/src/Mobs/Components/EnvironmentComponent.h @@ -10,24 +10,15 @@ protected: cMonster * m_Self; int m_SightDistance; bool m_OnGround; - - bool m_BurnsInDaylight; public: cEnvironmentComponent(cMonster * a_Entity, int a_SightDistance); virtual ~cEnvironmentComponent(){} - - virtual void Tick(float a_Dt, cChunk & a_Chunk); // Get Functions int GetSightDistance() { return m_SightDistance ; } bool GetOnGround() { return m_OnGround; } - bool GetBurnsInDaylight() { return m_BurnsInDaylight; } // Set Functions void SetSightDistance(int a_SightDistance) { m_SightDistance = a_SightDistance; } void SetOnGround(bool a_Bool) { m_OnGround = a_Bool; } - void SetBurnsInDaylight(bool a_Bool) { m_BurnsInDaylight = a_Bool; } - - // Handle functions - void HandleDaylightBurning(cChunk & a_Chunk); }; diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index 029930266..4e7b14385 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -92,7 +92,6 @@ void cMonster::Tick(float a_Dt, cChunk & a_Chunk) LOG("Monster Tick..."); m_AI->Tick(a_Dt, a_Chunk); m_Attack->Tick(a_Dt, a_Chunk); - m_Environment->Tick(a_Dt, a_Chunk); m_Movement->Tick(a_Dt, a_Chunk); } diff --git a/src/Mobs/Zombie.cpp b/src/Mobs/Zombie.cpp index f96db6942..b8a26c449 100644 --- a/src/Mobs/Zombie.cpp +++ b/src/Mobs/Zombie.cpp @@ -12,7 +12,8 @@ cZombie::cZombie(bool a_IsVillagerZombie) : super("Zombie", mtZombie, "mob.zombie.hurt", "mob.zombie.death", 0.6, 1.8), m_IsVillagerZombie(a_IsVillagerZombie), - m_IsConverting(false) + m_IsConverting(false), + m_BurningComponent(*this) { } diff --git a/src/Mobs/Zombie.h b/src/Mobs/Zombie.h index e3f499c64..12d0e95fc 100644 --- a/src/Mobs/Zombie.h +++ b/src/Mobs/Zombie.h @@ -1,8 +1,8 @@ #pragma once #include "Monster.h" - - +#include "Components/BurningComponent.h" +#include "Chunk.h" @@ -17,6 +17,11 @@ public: CLASS_PROTODEF(cZombie) virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = NULL) override; + virtual void Tick(float a_Dt, cChunk & a_Chunk) + { + super::Tick(a_Dt, a_Chunk); + m_BurningComponent.Tick(a_Dt, a_Chunk); + } bool IsVillagerZombie(void) const { return m_IsVillagerZombie; } bool IsConverting (void) const { return m_IsConverting; } @@ -25,6 +30,8 @@ private: bool m_IsVillagerZombie; bool m_IsConverting; + + cBurningComponent<cEntity, cChunk> m_BurningComponent; } ; |