and again

This commit is contained in:
2025-07-18 18:56:49 +02:00
parent 6d0e56eb46
commit 090ea962d3
19 changed files with 115 additions and 186 deletions

View File

@@ -5,9 +5,9 @@ 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;
void CommandApply::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) {
auto zombie = std::make_shared<game::Zombie>();
zombie->m_Position = a_SpawnTroop->m_Position;
m_Snapshot.m_Mobs.push_back(zombie);
}

View File

@@ -16,16 +16,24 @@ RealTimeSimulation::RealTimeSimulation(game::World& a_World, GameHistory&& a_His
m_History(std::move(a_History)),
m_CurrentTime(0),
m_LastTime(GetTime()),
m_CurrentStep(0) {}
m_CurrentStep(0) {
Step();
}
void RealTimeSimulation::Update() {
float RealTimeSimulation::Update() {
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
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++;
Step();
m_CurrentTime -= m_StepTime;
}
return (float) m_CurrentTime / (float) m_StepTime;
}
void RealTimeSimulation::Step() {
m_World.Tick(m_History[m_CurrentStep], FpFloat(m_StepTime) / FpFloat(1000));
m_CurrentStep++;
}
} // namespace sim

View File

@@ -13,9 +13,9 @@ WorldTicker::WorldTicker() {
}
WorldSnapshot WorldTicker::NextStep(
const game::World& a_World, const WorldSnapshot& a_PreviousState, const protocol::LockStep& a_LockStep, FpFloat a_Delta) {
const game::World& a_World, WorldSnapshot& a_PreviousState, const protocol::LockStep& a_LockStep, FpFloat a_Delta) {
WorldSnapshot next = CreateNext(a_PreviousState);
ApplySteps(a_World, next, a_LockStep);
ApplySteps(a_World, next, a_LockStep); // maybe swap with tick ?
Tick(a_World, next, a_Delta);
return next;
}
@@ -33,8 +33,21 @@ void WorldTicker::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFlo
}
}
WorldSnapshot WorldTicker::CreateNext(const WorldSnapshot& a_PreviousState) {
return a_PreviousState;
WorldSnapshot WorldTicker::CreateNext(WorldSnapshot& a_PreviousState) {
static game::MobFactory mobFactory;
WorldSnapshot next {
.m_Towers = a_PreviousState.m_Towers,
.m_Teams = a_PreviousState.m_Teams
};
next.m_Mobs.reserve(a_PreviousState.m_Mobs.size());
for (auto& mob : a_PreviousState.m_Mobs) {
game::MobPtr newMob = std::shared_ptr<game::Mob>(mobFactory.CreateMessage(mob->GetId()).release());
*newMob = *mob;
mob->m_Next = newMob;
next.m_Mobs.push_back(newMob);
}
return next;
}
} // namespace sim

View File

@@ -5,7 +5,7 @@ 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;
mob->m_Position.x += a_Delta;
}
}