From 701dd6b120e7e5116ea8ee37795facb4db05fc64 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 1 Jan 2026 22:19:55 +0100 Subject: [PATCH] add basic entity movement --- src/td/game/World.cpp | 14 ++++++- src/td/simulation/CommandApply.cpp | 7 +++- src/td/simulation/system/EntityMove.cpp | 52 ++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/td/game/World.cpp b/src/td/game/World.cpp index cb5a935..4b0d444 100644 --- a/src/td/game/World.cpp +++ b/src/td/game/World.cpp @@ -1,7 +1,10 @@ +#include +#include #include +#include -#include #include +#include namespace td { namespace game { @@ -89,5 +92,14 @@ void World::ResetSnapshots(std::shared_ptr& a_Current, std:: m_NextState = a_Next; } +TilePtr World::GetTile(std::int32_t x, std::int32_t y) const { + ChunkCoord coords{static_cast(x / Chunk::ChunkWidth), static_cast(y / Chunk::ChunkHeight)}; + auto it = m_Chunks.find(coords); + assert(it != m_Chunks.end()); + auto chunk = it->second; + Vec2i inchunkCoords{x % Chunk::ChunkWidth, y % Chunk::ChunkHeight}; + return GetTilePtr(chunk->GetTileIndex(inchunkCoords.y * Chunk::ChunkWidth + inchunkCoords.x)); +} + } // namespace game } // namespace td diff --git a/src/td/simulation/CommandApply.cpp b/src/td/simulation/CommandApply.cpp index 8e17aef..ae6e234 100644 --- a/src/td/simulation/CommandApply.cpp +++ b/src/td/simulation/CommandApply.cpp @@ -1,3 +1,4 @@ +#include #include namespace td { @@ -6,7 +7,7 @@ namespace sim { CommandApply::CommandApply(const game::World& a_World, WorldSnapshot& a_Snapshot) : m_World(a_World), m_Snapshot(a_Snapshot) {} void CommandApply::Handle(const protocol::commands::EndCommand& a_End) { - (void) m_World; + (void)m_World; } void CommandApply::Handle(const protocol::commands::PlaceTowerCommand& a_PlaceTower) { @@ -24,6 +25,10 @@ void CommandApply::Handle(const protocol::commands::PlayerJoinCommand& a_PlayerJ void CommandApply::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) { auto zombie = std::make_shared(); zombie->m_Position = a_SpawnTroop->m_Position; + + // TODO: make it spawn dependant + zombie->m_Direction = Direction::PositiveY; + m_Snapshot.m_Mobs.push_back(zombie); } diff --git a/src/td/simulation/system/EntityMove.cpp b/src/td/simulation/system/EntityMove.cpp index 798a6f8..b597831 100644 --- a/src/td/simulation/system/EntityMove.cpp +++ b/src/td/simulation/system/EntityMove.cpp @@ -1,11 +1,61 @@ +#include +#include +#include #include +#include + namespace td { namespace sim { +static Vec2i GetUnitDirection(Direction a_Direction) { + switch (a_Direction) { + case Direction::PositiveX: + return {1, 0}; + case Direction::NegativeX: + return {-1, 0}; + case Direction::PositiveY: + return {0, 1}; + case Direction::NegativeY: + return {0, -1}; + } + return {0, 0}; +} + +class DirectionTileVisitor : public game::TileHandler { + private: + Direction m_Direction; + + public: + DirectionTileVisitor() {} + + virtual void Handle(const game::EmptyTile& a_Tile) override {} + + virtual void Handle(const game::TowerTile& a_Tile) override {} + + virtual void Handle(const game::DecorationTile& a_Tile) override {} + + virtual void Handle(const game::WalkableTile& a_Tile) override { + m_Direction = a_Tile->m_Direction; + } + + const Direction GetDirection() { + return m_Direction; + } +}; + void EntityMove::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) { for (auto& mob : a_State.m_Mobs) { - mob->m_Position.x += a_Delta; + auto tile = a_World.GetTile(static_cast(mob->m_Position.x), static_cast(mob->m_Position.y)); + Direction direction = mob->m_Direction; + if (tile) { + DirectionTileVisitor visitor; + tile->Dispatch(visitor); + direction = visitor.GetDirection(); + } + auto directVector = GetUnitDirection(direction); + mob->m_Position.x += directVector.x * a_Delta; + mob->m_Position.y += directVector.y * a_Delta; } }