begin client-server

This commit is contained in:
2025-08-06 20:10:56 +02:00
parent 89213e9a97
commit c813c49707
26 changed files with 264 additions and 188 deletions

View File

@@ -1,25 +1,36 @@
#include <server/state/GameState.h>
#include <server/state/GameStateHandler.h>
#include <iostream>
namespace td {
namespace server {
void GameState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {
GameState::GameState(const std::shared_ptr<game::World>& a_World) : m_World(a_World), m_Simulation(*m_World, STEP_TIME), m_Time(0) {}
void GameState::Init() {
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) {
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);
}
}
void GameState::OnPlayerJoin(PlayerID a_Id) {
void GameState::OnPlayerJoin(PlayerID a_Id) {}
}
void GameState::OnPlayerLeave(PlayerID a_Id) {
std::cout << "Game leave !" << std::endl;
}
void GameState::OnPlayerLeave(PlayerID a_Id) {}
} // namespace server
} // namespace td

View File

@@ -0,0 +1,24 @@
#include <server/state/GameState.h>
#include <server/state/GameStateHandler.h>
namespace td {
namespace server {
GameStateHandler::GameStateHandler(GameState& a_GameState, PlayerID a_PlayerId) : m_GameState(a_GameState), m_PlayerId(a_PlayerId) {}
// TODO: redo this
void GameStateHandler::Handle(const protocol::packets::SpawnTroopPacket& a_Packet) {
static const EntityCoords DEFAULT_POS(td::FpFloat(77), td::FpFloat(13));
td::protocol::commands::SpawnTroopCommand spawn(*a_Packet->m_Type, *a_Packet->m_Level, DEFAULT_POS, m_PlayerId);
m_GameState.m_Simulation.Handle(spawn);
}
// TODO: and this
void GameStateHandler::Handle(const protocol::packets::PlaceTowerPacket& a_Packet) {
td::protocol::commands::PlaceTowerCommand place(a_Packet->m_Type, m_PlayerId, a_Packet->m_Position);
m_GameState.m_Simulation.Handle(place);
}
} // namespace server
} // namespace td

View File

@@ -11,7 +11,7 @@ void LobbyState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packe
}
void LobbyState::Update(float a_Delta) {
SetNewState(std::make_shared<GameState>());
SetNewState(std::make_shared<GameState>(m_World));
}
void LobbyState::OnPlayerJoin(PlayerID a_Id) {
@@ -19,7 +19,7 @@ void LobbyState::OnPlayerJoin(PlayerID a_Id) {
}
void LobbyState::OnPlayerLeave(PlayerID a_Id) {
std::cout << "Lobby leave !" << std::endl;
}
} // namespace server