add StateMachine
This commit is contained in:
@@ -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
|
||||
|
||||
16
src/client/ClientState.cpp
Normal file
16
src/client/ClientState.cpp
Normal 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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
24
src/main.cpp
24
src/main.cpp
@@ -1,6 +1,7 @@
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <fstream>
|
||||
#include <td/game/World.h>
|
||||
#include <td/input/Display.h>
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
@@ -102,6 +103,14 @@ void FastForward(td::game::World& a_World, const td::sim::GameHistory& a_LockSte
|
||||
}
|
||||
}
|
||||
|
||||
float GetDelta() {
|
||||
static std::chrono::time_point<std::chrono::system_clock> m_LastTime = std::chrono::system_clock::now();
|
||||
auto timeElapsed = std::chrono::system_clock::now() - m_LastTime;
|
||||
float timeSeconds = std::chrono::duration<float, std::chrono::seconds::period>(timeElapsed).count();
|
||||
m_LastTime = std::chrono::system_clock::now();
|
||||
return timeSeconds;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
td::game::WorldPtr serverWorld = GetWorld();
|
||||
|
||||
@@ -132,7 +141,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
td::sim::ClientSimulation simulation(*clientWorld, td::STEP_TIME);
|
||||
ClientHandler clientHandler(simulation);
|
||||
|
||||
|
||||
// packets from the server to the client
|
||||
clientFakeSocket->OnReceive.Connect([&clientHandler](const td::protocol::PacketBase& a_Packet) {
|
||||
a_Packet.Dispatch(clientHandler);
|
||||
@@ -151,14 +160,15 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
});
|
||||
|
||||
server.UpdateState(std::make_shared<td::server::GameState>(serverWorld));
|
||||
client.UpdateState(std::make_shared<td::client::GameState>(clientWorld));
|
||||
server.ChangeState<td::server::GameState>(serverWorld);
|
||||
client.ChangeState<td::client::GameState>(clientWorld);
|
||||
|
||||
while (!display.IsCloseRequested()) {
|
||||
display.PollEvents();
|
||||
server.Update();
|
||||
client.Update();
|
||||
float lerp = simulation.Update();
|
||||
float delta = GetDelta();
|
||||
server.Update(delta);
|
||||
client.Update(delta);
|
||||
float lerp = simulation.Update(delta);
|
||||
renderer.Render(lerp);
|
||||
display.Update();
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
#include <server/IServerState.h>
|
||||
#include <server/Server.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
void IServerState::SetServer(Server* a_Server) {
|
||||
assert(a_Server);
|
||||
m_Server = a_Server;
|
||||
Connect(m_Server->m_Socket->OnConnect, std::bind(&IServerState::OnPlayerJoin, this, std::placeholders::_1));
|
||||
Connect(m_Server->m_Socket->OnDisconnect, std::bind(&IServerState::OnPlayerLeave, this, std::placeholders::_1));
|
||||
Connect(m_Server->m_Socket->OnReceive, std::bind(&IServerState::HandlePacket, this, std::placeholders::_1, std::placeholders::_2));
|
||||
Init();
|
||||
}
|
||||
|
||||
IServerState::IServerState() : m_Server(nullptr) {}
|
||||
|
||||
IServerState::~IServerState() {}
|
||||
|
||||
void IServerState::SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {
|
||||
m_Server->m_Socket->Send(a_Id, a_Packet);
|
||||
}
|
||||
|
||||
void IServerState::BroadcastPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_Server->m_Socket->Broadcast(a_Packet);
|
||||
}
|
||||
|
||||
void IServerState::SetNewState(const std::shared_ptr<IServerState>& a_NewState) {
|
||||
m_Server->UpdateState(a_NewState);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -3,22 +3,7 @@
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
void Server::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 Server::UpdateState(const std::shared_ptr<IServerState>& a_State) {
|
||||
m_State = a_State;
|
||||
m_State->SetServer(this);
|
||||
}
|
||||
|
||||
void Server::Update(float a_Delta) {
|
||||
assert(m_State);
|
||||
m_State->Update(a_Delta);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
24
src/server/ServerState.cpp
Normal file
24
src/server/ServerState.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <server/ServerState.h>
|
||||
#include <server/Server.h>
|
||||
|
||||
#include <td/common/StateMachine.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
ServerState::ServerState(Server& a_Server) : Server::State(a_Server) {
|
||||
Connect(m_StateMachine.m_Socket->OnReceive, std::bind(&ServerState::HandlePacket, this, std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
ServerState::~ServerState() {}
|
||||
|
||||
void ServerState::SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {
|
||||
m_StateMachine.m_Socket->Send(a_Id, a_Packet);
|
||||
}
|
||||
|
||||
void ServerState::BroadcastPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_StateMachine.m_Socket->Broadcast(a_Packet);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace
|
||||
@@ -6,9 +6,7 @@
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
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() {
|
||||
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());
|
||||
}
|
||||
@@ -28,9 +26,5 @@ void GameState::Update(float a_Delta) {
|
||||
}
|
||||
}
|
||||
|
||||
void GameState::OnPlayerJoin(PlayerID a_Id) {}
|
||||
|
||||
void GameState::OnPlayerLeave(PlayerID a_Id) {}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
@@ -11,15 +11,7 @@ void LobbyState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packe
|
||||
}
|
||||
|
||||
void LobbyState::Update(float a_Delta) {
|
||||
SetNewState(std::make_shared<GameState>(m_World));
|
||||
}
|
||||
|
||||
void LobbyState::OnPlayerJoin(PlayerID a_Id) {
|
||||
|
||||
}
|
||||
|
||||
void LobbyState::OnPlayerLeave(PlayerID a_Id) {
|
||||
|
||||
m_StateMachine.ChangeState<GameState>(m_World);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
|
||||
@@ -20,7 +20,6 @@ ClientSimulation::ClientSimulation(game::World& a_World, GameHistory&& a_History
|
||||
m_StepTime(a_StepTime),
|
||||
m_World(a_World),
|
||||
m_CurrentTime(0),
|
||||
m_LastTime(GetTime()),
|
||||
m_CurrentStep(0),
|
||||
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
|
||||
m_LastValidStep(0) {
|
||||
@@ -36,20 +35,19 @@ ClientSimulation::ClientSimulation(game::World& a_World, std::uint64_t a_StepTim
|
||||
m_World(a_World),
|
||||
m_History(std::numeric_limits<StepTime>::max()),
|
||||
m_CurrentTime(0),
|
||||
m_LastTime(GetTime()),
|
||||
m_CurrentStep(0),
|
||||
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
|
||||
m_LastValidStep(0) {}
|
||||
|
||||
float ClientSimulation::Update() {
|
||||
float ClientSimulation::Update(float a_Delta) {
|
||||
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
|
||||
m_CurrentTime += GetTime() - m_LastTime;
|
||||
m_LastTime = GetTime();
|
||||
if (m_CurrentTime > m_StepTime) {
|
||||
static const float stepTimeSecond = static_cast<float>(STEP_TIME) / 1000.0f;
|
||||
m_CurrentTime += a_Delta;
|
||||
if (m_CurrentTime > stepTimeSecond) {
|
||||
m_CurrentTime = std::fmod(m_CurrentTime, stepTimeSecond);
|
||||
Step();
|
||||
m_CurrentTime %= m_StepTime;
|
||||
}
|
||||
return (float)m_CurrentTime / (float)m_StepTime;
|
||||
return (float)m_CurrentTime / stepTimeSecond;
|
||||
}
|
||||
|
||||
bool ClientSimulation::Step() {
|
||||
|
||||
Reference in New Issue
Block a user