add client

This commit is contained in:
2025-08-08 13:24:50 +02:00
parent b09c7f9efd
commit ac3e949323
14 changed files with 306 additions and 33 deletions

24
src/client/Client.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include <client/Client.h>
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,27 @@
#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

@@ -0,0 +1,19 @@
#include <client/socket/FakeSocket.h>
#include <server/socket/FakeSocket.h>
namespace td {
namespace client {
void FakeSocket::Send(const protocol::PacketBase& a_Packet) {
m_Server->OnReceivePeer(m_PeerId, a_Packet);
}
std::shared_ptr<FakeSocket> FakeSocket::Connect(const std::shared_ptr<server::FakeSocket>& a_Server) {
auto socket = std::make_shared<FakeSocket>(Private());
socket->m_Server = a_Server;
socket->m_PeerId = a_Server->ConnectFakePeer(socket);
return socket;
}
} // namespace client
} // namespace td

View File

@@ -0,0 +1,12 @@
#include <client/state/GameState.h>
namespace td {
namespace client {
GameState::GameState(const std::shared_ptr<game::World>& a_World) : 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