add StateMachine

This commit is contained in:
2025-08-09 11:23:17 +02:00
parent ac3e949323
commit cba790f804
23 changed files with 174 additions and 244 deletions

View File

@@ -3,22 +3,7 @@
namespace td {
namespace client {
void Client::Update() {
auto timeElapsed = std::chrono::system_clock::now() - m_LastTime;
float timeSeconds = std::chrono::duration<float, std::chrono::seconds::period>(timeElapsed).count();
Update(timeSeconds);
m_LastTime = std::chrono::system_clock::now();
}
void Client::UpdateState(const std::shared_ptr<IClientState>& a_State) {
m_State = a_State;
m_State->SetClient(this);
}
void Client::Update(float a_Delta) {
assert(m_State);
m_State->Update(a_Delta);
}
} // namespace client
} // namespace td

View File

@@ -0,0 +1,16 @@
#include <client/ClientState.h>
#include <client/Client.h>
namespace td {
namespace client {
ClientState::ClientState(Client& a_Client) : Client::State(a_Client) {
Connect(m_StateMachine.m_Socket->OnReceive, std::bind(&ClientState::HandlePacket, this, std::placeholders::_1));
}
void ClientState::SendPacket(const protocol::PacketBase& a_Packet) {
m_StateMachine.m_Socket->Send(a_Packet);
}
} // namespace server
} // namespace td

View File

@@ -1,27 +0,0 @@
#include <client/IClientState.h>
#include <client/Client.h>
namespace td {
namespace client {
void IClientState::SetClient(Client* a_Client) {
assert(a_Client);
m_Client = a_Client;
Connect(m_Client->m_Socket->OnReceive, std::bind(&IClientState::HandlePacket, this, std::placeholders::_1));
Init();
}
IClientState::IClientState() : m_Client(nullptr) {}
IClientState::~IClientState() {}
void IClientState::SendPacket(const protocol::PacketBase& a_Packet) {
m_Client->m_Socket->Send(a_Packet);
}
void IClientState::SetNewState(const std::shared_ptr<IClientState>& a_NewState) {
m_Client->UpdateState(a_NewState);
}
} // namespace server
} // namespace td

View File

@@ -3,10 +3,9 @@
namespace td {
namespace client {
GameState::GameState(const std::shared_ptr<game::World>& a_World) : m_World(a_World) {}
GameState::GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World) : ClientState(a_Client), m_World(a_World) {}
void GameState::HandlePacket(const protocol::PacketBase& a_Packet) {}
void GameState::Update(float a_Delta) {}
void GameState::Init() {}
} // namespace client
} // namespace td