too many things
This commit is contained in:
15
src/td/simulation/CommandApply.cpp
Normal file
15
src/td/simulation/CommandApply.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <td/simulation/CommandApply.h>
|
||||
|
||||
namespace td {
|
||||
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::cdata::SpawnTroop& a_SpawnTroop) {
|
||||
auto zombie = std::make_shared<game::Zombie>(0, *a_SpawnTroop.m_Level, a_SpawnTroop.m_Sender);
|
||||
zombie->GetPosition() = a_SpawnTroop.m_Position;
|
||||
m_Snapshot.m_Mobs.push_back(zombie);
|
||||
}
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
44
src/td/simulation/GameHistory.cpp
Normal file
44
src/td/simulation/GameHistory.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <td/simulation/GameHistory.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
GameHistory::GameHistory() : m_History(std::numeric_limits<HistorySizeType>::max()), m_Cursor(0) {}
|
||||
|
||||
void GameHistory::SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep) {
|
||||
m_History[a_Index] = std::move(a_LockStep);
|
||||
}
|
||||
|
||||
const protocol::LockStep& GameHistory::GetLockStep(HistorySizeType a_Index) const {
|
||||
return *m_History[a_Index];
|
||||
}
|
||||
|
||||
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];
|
||||
SetLockStep(i + a_Steps.m_FirstFrameNumber, std::move(step));
|
||||
}
|
||||
}
|
||||
|
||||
td::protocol::packets::LockStepsPacket GameHistory::ToPacket(HistorySizeType a_StartIndex) {
|
||||
Array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps;
|
||||
for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
|
||||
steps[i] = GetLockStep(a_StartIndex + i);
|
||||
}
|
||||
return {a_StartIndex, std::move(steps)};
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
32
src/td/simulation/RealTimeSimulation.cpp
Normal file
32
src/td/simulation/RealTimeSimulation.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <td/simulation/RealTimeSimulation.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
std::uint64_t GetTime() {
|
||||
return static_cast<std::uint64_t>(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());
|
||||
}
|
||||
|
||||
RealTimeSimulation::RealTimeSimulation(game::World& a_World, GameHistory&& a_History, std::uint64_t a_StepTime) :
|
||||
m_StepTime(a_StepTime),
|
||||
m_World(a_World),
|
||||
m_History(std::move(a_History)),
|
||||
m_CurrentTime(0),
|
||||
m_LastTime(GetTime()),
|
||||
m_CurrentStep(0) {}
|
||||
|
||||
void RealTimeSimulation::Update() {
|
||||
m_CurrentTime += GetTime() - m_LastTime;
|
||||
m_LastTime = GetTime();
|
||||
if (m_CurrentTime > m_StepTime) {
|
||||
m_World.Tick(m_History[m_CurrentStep], FpFloat(m_StepTime) / FpFloat(1000));
|
||||
m_CurrentStep++;
|
||||
m_CurrentTime -= m_StepTime;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
41
src/td/simulation/WorldTicker.cpp
Normal file
41
src/td/simulation/WorldTicker.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <td/simulation/WorldTicker.h>
|
||||
|
||||
#include <td/simulation/system/EntityMove.h>
|
||||
#include <td/simulation/CommandApply.h>
|
||||
|
||||
#define ADD_SYSTEM(class) std::move(std::make_unique<class>())
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
WorldTicker::WorldTicker() {
|
||||
AddSystem<EntityMove>();
|
||||
}
|
||||
|
||||
WorldSnapshot WorldTicker::NextStep(
|
||||
const game::World& a_World, const WorldSnapshot& a_PreviousState, const protocol::LockStep& a_LockStep, FpFloat a_Delta) {
|
||||
WorldSnapshot next = CreateNext(a_PreviousState);
|
||||
ApplySteps(a_World, next, a_LockStep);
|
||||
Tick(a_World, next, a_Delta);
|
||||
return next;
|
||||
}
|
||||
|
||||
void WorldTicker::ApplySteps(const game::World& a_World, WorldSnapshot& a_State, const protocol::LockStep& a_LockStep) {
|
||||
CommandApply cmdHandler(a_World, a_State);
|
||||
for (const auto& cmd : a_LockStep) {
|
||||
cmd->Dispatch(cmdHandler);
|
||||
}
|
||||
}
|
||||
|
||||
void WorldTicker::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) {
|
||||
for (const auto& system : m_Systems) {
|
||||
system->Tick(a_World, a_State, a_Delta);
|
||||
}
|
||||
}
|
||||
|
||||
WorldSnapshot WorldTicker::CreateNext(const WorldSnapshot& a_PreviousState) {
|
||||
return a_PreviousState;
|
||||
}
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
13
src/td/simulation/system/EntityMove.cpp
Normal file
13
src/td/simulation/system/EntityMove.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <td/simulation/system/EntityMove.h>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
void EntityMove::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) {
|
||||
for (auto& mob : a_State.m_Mobs) {
|
||||
mob->GetPosition().x += a_Delta;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user