32 lines
944 B
C++
32 lines
944 B
C++
#include <server/state/GameState.h>
|
|
#include <server/state/GameStateHandler.h>
|
|
|
|
#include <iostream>
|
|
|
|
namespace td {
|
|
namespace server {
|
|
|
|
GameState::GameState(Server& a_Server, const std::shared_ptr<game::World>& a_World) : ServerState(a_Server), m_World(a_World), m_Simulation(*m_World, STEP_TIME), m_Time(0) {
|
|
std::cout << "Switched to Game state !\n";
|
|
BroadcastPacket(m_Simulation.MakePacket());
|
|
}
|
|
|
|
void GameState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {
|
|
GameStateHandler handler(*this, a_Id);
|
|
a_Packet.Dispatch(handler);
|
|
}
|
|
|
|
void GameState::Update(float a_Delta) {
|
|
// TODO: don't make STEP_TIME constant
|
|
static const float stepTimeSecond = static_cast<float>(STEP_TIME) / 1000.0f;
|
|
m_Time += a_Delta;
|
|
if (m_Time > stepTimeSecond) {
|
|
m_Time = std::fmod(m_Time, stepTimeSecond);
|
|
auto lockStepPacket = m_Simulation.Update();
|
|
BroadcastPacket(lockStepPacket);
|
|
}
|
|
}
|
|
|
|
} // namespace server
|
|
} // namespace td
|