diff --git a/assets/stats.json b/assets/stats.json new file mode 100644 index 0000000..59f8008 --- /dev/null +++ b/assets/stats.json @@ -0,0 +1,5 @@ +{ + "Zombie" : { + + } +} \ No newline at end of file diff --git a/include/td/Constants.h b/include/td/Constants.h new file mode 100644 index 0000000..10e45e1 --- /dev/null +++ b/include/td/Constants.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +namespace td { + +constexpr int TPS = 20; +constexpr float STEP_PERIOD = 1.0f / static_cast(TPS); + +} // namespace td diff --git a/include/td/Types.h b/include/td/Types.h index c293bf8..0bc9948 100644 --- a/include/td/Types.h +++ b/include/td/Types.h @@ -66,5 +66,6 @@ struct EntityCoords { }; using PeerID = std::uint16_t; +using StepsType = std::uint16_t; } // namespace td diff --git a/include/td/game/GameHistory.h b/include/td/game/GameHistory.h index c5c4f61..2c951ed 100644 --- a/include/td/game/GameHistory.h +++ b/include/td/game/GameHistory.h @@ -9,12 +9,10 @@ namespace game { class GameHistory { private: - using HistorySizeType = std::uint16_t; + using HistorySizeType = StepsType; std::vector> m_History; - HistorySizeType m_Cursor; - public: GameHistory(); @@ -24,10 +22,6 @@ class GameHistory { bool HasLockStep(HistorySizeType a_Index) const; - const protocol::LockStep& GetNextStep(); - - bool HasNextStep() const; - void FromPacket(td::protocol::pdata::LockSteps&& a_Steps); td::protocol::packets::LockSteps ToPacket(HistorySizeType a_StartIndex); diff --git a/include/td/game/World.h b/include/td/game/World.h new file mode 100644 index 0000000..41e6407 --- /dev/null +++ b/include/td/game/World.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +namespace td { +namespace game { + +class World { + private: + std::vector m_WorldStates; + GameHistory m_GameHistory; + + float m_OffsetTime; + + public: + World(); + + void Tick(float a_Delta); + + private: + std::uint16_t GetCursorPos(); +}; + +} // namespace game +} // namespace td diff --git a/include/td/game/WorldState.h b/include/td/game/WorldState.h new file mode 100644 index 0000000..6db7a4a --- /dev/null +++ b/include/td/game/WorldState.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +namespace td { +namespace game { + +class WorldState { + private: + // list of players, mobs, towers, castles + public: + WorldState() {} + + WorldState GetNextState(const protocol::LockStep& a_Step); + + private: + void Tick(float a_Delta); +}; + +} // namespace game +} // namespace td diff --git a/include/td/network/EnetClient.h b/include/td/network/EnetClient.h new file mode 100644 index 0000000..7b9637e --- /dev/null +++ b/include/td/network/EnetClient.h @@ -0,0 +1 @@ +#pragma once \ No newline at end of file diff --git a/include/td/network/EnetConnection.h b/include/td/network/EnetConnection.h new file mode 100644 index 0000000..7b9637e --- /dev/null +++ b/include/td/network/EnetConnection.h @@ -0,0 +1 @@ +#pragma once \ No newline at end of file diff --git a/include/td/network/EnetServer.h b/include/td/network/EnetServer.h new file mode 100644 index 0000000..7b9637e --- /dev/null +++ b/include/td/network/EnetServer.h @@ -0,0 +1 @@ +#pragma once \ No newline at end of file diff --git a/src/td/game/GameHistory.cpp b/src/td/game/GameHistory.cpp index 2ed127e..b3c30cf 100644 --- a/src/td/game/GameHistory.cpp +++ b/src/td/game/GameHistory.cpp @@ -3,7 +3,7 @@ namespace td { namespace game { -GameHistory::GameHistory() : m_History(std::numeric_limits::max()), m_Cursor(0) {} +GameHistory::GameHistory() : m_History(std::numeric_limits::max()) {} void GameHistory::SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep) { m_History[a_Index] = std::move(a_LockStep); @@ -17,14 +17,6 @@ bool GameHistory::HasLockStep(HistorySizeType a_Index) const { return m_History[a_Index].has_value(); } -const protocol::LockStep& GameHistory::GetNextStep() { - return GetLockStep(m_Cursor++); -} - -bool GameHistory::HasNextStep() const { - return HasLockStep(m_Cursor); -} - void GameHistory::FromPacket(td::protocol::pdata::LockSteps&& a_Steps) { for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { protocol::LockStep& step = a_Steps.m_LockSteps[i]; diff --git a/src/td/game/World.cpp b/src/td/game/World.cpp new file mode 100644 index 0000000..19928b0 --- /dev/null +++ b/src/td/game/World.cpp @@ -0,0 +1,31 @@ +#include + +#include + +#include + +namespace td { +namespace game { + +World::World() : m_WorldStates(std::numeric_limits::max()), m_OffsetTime(0) {} + +void World::Tick(float a_Delta) { + m_OffsetTime += a_Delta; + + if (GetCursorPos() > m_WorldStates.size()) { + if (!m_GameHistory.HasLockStep(GetCursorPos())) { + // oupsi + return; + } + WorldState& lastState = m_WorldStates.back(); + WorldState nextState = lastState.GetNextState(m_GameHistory.GetLockStep(GetCursorPos())); + m_WorldStates.push_back(std::move(nextState)); + } +} + +std::uint16_t World::GetCursorPos() { + return (std::uint16_t) m_OffsetTime * TPS; +} + +} // namespace game +} // namespace td diff --git a/src/td/game/WorldState.cpp b/src/td/game/WorldState.cpp new file mode 100644 index 0000000..c6a4905 --- /dev/null +++ b/src/td/game/WorldState.cpp @@ -0,0 +1,43 @@ +#include + +#include + +#include + +namespace td { +namespace game { + +class CommandHandler : public protocol::CommandVisitor { + private: + WorldState& m_WorldState; + + public: + CommandHandler(WorldState& a_WorldState) : m_WorldState(a_WorldState) {} + + void Visit(const protocol::commands::End&) override {} + void Visit(const protocol::commands::PlaceTower&) override {} + void Visit(const protocol::commands::PlayerJoin&) override {} + void Visit(const protocol::commands::SpawnTroop&) override {} + void Visit(const protocol::commands::TeamChange&) override {} + void Visit(const protocol::commands::UpgradeTower&) override {} + void Visit(const protocol::commands::UseItem&) override {} +}; + +WorldState WorldState::GetNextState(const protocol::LockStep& a_Step) { + WorldState newState = *this; + CommandHandler handler(*this); + for (auto command : a_Step) { + handler.Check(*command); + } + newState.Tick(STEP_PERIOD); + return newState; +} + +void WorldState::Tick(float a_Delta) { + // move mobs + // process towers + // process damages +} + +} // namespace game +} // namespace td