add StateMachine
This commit is contained in:
@@ -1,29 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
#include <client/IClientState.h>
|
||||
#include <chrono>
|
||||
#include <td/common/StateMachine.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class Client {
|
||||
class ClientState;
|
||||
|
||||
class Client : public StateMachine<Client, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IClientSocket> m_Socket;
|
||||
std::shared_ptr<IClientState> m_State;
|
||||
std::chrono::time_point<std::chrono::system_clock> m_LastTime;
|
||||
|
||||
public:
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket), m_LastTime(std::chrono::system_clock::now()) {}
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket) {}
|
||||
|
||||
void Update();
|
||||
|
||||
void UpdateState(const std::shared_ptr<IClientState>& a_State);
|
||||
|
||||
private:
|
||||
void Update(float a_Delta);
|
||||
|
||||
friend class IClientState;
|
||||
friend class ClientState;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
20
include/client/ClientState.h
Normal file
20
include/client/ClientState.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/Client.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientState : public Client::State, private utils::SlotGuard {
|
||||
public:
|
||||
ClientState(Client& a_Client);
|
||||
virtual ~ClientState() {}
|
||||
|
||||
protected:
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) {}
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,33 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class Client;
|
||||
|
||||
class IClientState : private utils::SlotGuard {
|
||||
public:
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Update(float a_Delta) = 0;
|
||||
|
||||
IClientState();
|
||||
virtual ~IClientState();
|
||||
|
||||
protected:
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
void SetNewState(const std::shared_ptr<IClientState>& a_NewState);
|
||||
virtual void Init() {}
|
||||
|
||||
private:
|
||||
Client* m_Client;
|
||||
|
||||
void SetClient(Client* a_Client);
|
||||
|
||||
friend class Client;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,25 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientState.h>
|
||||
#include <client/ClientState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ServerSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class GameState : public IClientState {
|
||||
class GameState : public ClientState {
|
||||
private:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
|
||||
public:
|
||||
GameState(const std::shared_ptr<game::World>& a_World);
|
||||
GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World);
|
||||
~GameState() {}
|
||||
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
protected:
|
||||
virtual void Init() override;
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/IServerSocket.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class Server;
|
||||
|
||||
class IServerState : private utils::SlotGuard {
|
||||
public:
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Update(float a_Delta) = 0;
|
||||
virtual void OnPlayerJoin(PlayerID a_Id) = 0;
|
||||
virtual void OnPlayerLeave(PlayerID a_Id) = 0;
|
||||
|
||||
IServerState();
|
||||
virtual ~IServerState();
|
||||
|
||||
protected:
|
||||
void SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet);
|
||||
void BroadcastPacket(const protocol::PacketBase& a_Packet);
|
||||
void SetNewState(const std::shared_ptr<IServerState>& a_NewState);
|
||||
virtual void Init() {}
|
||||
|
||||
private:
|
||||
Server* m_Server;
|
||||
|
||||
void SetServer(Server* a_Server);
|
||||
|
||||
friend class Server;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,30 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <server/IServerSocket.h>
|
||||
#include <server/IServerState.h>
|
||||
#include <chrono>
|
||||
#include <td/common/StateMachine.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class Server {
|
||||
class ServerState;
|
||||
|
||||
class Server : public StateMachine<Server, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IServerSocket> m_Socket;
|
||||
std::shared_ptr<IServerState> m_State;
|
||||
std::chrono::time_point<std::chrono::system_clock> m_LastTime;
|
||||
|
||||
public:
|
||||
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket), m_LastTime(std::chrono::system_clock::now()) {}
|
||||
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {}
|
||||
|
||||
void Update();
|
||||
|
||||
void UpdateState(const std::shared_ptr<IServerState>& a_State);
|
||||
|
||||
private:
|
||||
void Update(float a_Delta);
|
||||
|
||||
friend class IServerState;
|
||||
friend class ServerState;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
||||
23
include/server/ServerState.h
Normal file
23
include/server/ServerState.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/Server.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class ServerState : public Server::State, private utils::SlotGuard {
|
||||
public:
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Update(float a_Delta) = 0;
|
||||
|
||||
ServerState(Server& a_Server);
|
||||
virtual ~ServerState();
|
||||
|
||||
protected:
|
||||
void SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet);
|
||||
void BroadcastPacket(const protocol::PacketBase& a_Packet);
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/IServerState.h>
|
||||
#include <server/ServerState.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class EndGameState : public IServerState {
|
||||
class EndGameState : public ServerState {
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/IServerState.h>
|
||||
#include <server/ServerState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ServerSimulation.h>
|
||||
|
||||
@@ -9,23 +9,18 @@ namespace server {
|
||||
|
||||
class GameStateHandler;
|
||||
|
||||
class GameState : public IServerState {
|
||||
class GameState : public ServerState {
|
||||
private:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
sim::ServerSimulation m_Simulation;
|
||||
float m_Time;
|
||||
|
||||
public:
|
||||
GameState(const std::shared_ptr<game::World>& a_World);
|
||||
GameState(Server& a_Server, const std::shared_ptr<game::World>& a_World);
|
||||
~GameState() {}
|
||||
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
virtual void OnPlayerJoin(PlayerID a_Id) override;
|
||||
virtual void OnPlayerLeave(PlayerID a_Id) override;
|
||||
|
||||
protected:
|
||||
virtual void Init() override;
|
||||
|
||||
friend class GameStateHandler;
|
||||
};
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/IServerState.h>
|
||||
#include <server/ServerState.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
// this class is temporary useless
|
||||
class LobbyState : public IServerState {
|
||||
class LobbyState : public ServerState {
|
||||
private:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
public:
|
||||
LobbyState(const std::shared_ptr<game::World>& a_World) : m_World(a_World) {}
|
||||
LobbyState(Server& a_Server, const std::shared_ptr<game::World>& a_World);
|
||||
~LobbyState() {}
|
||||
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
virtual void OnPlayerJoin(PlayerID a_Id) override;
|
||||
virtual void OnPlayerLeave(PlayerID a_Id) override;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
||||
39
include/td/common/StateMachine.h
Normal file
39
include/td/common/StateMachine.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace td {
|
||||
|
||||
template <typename TDerived, typename TReturn, typename... TArgs>
|
||||
class StateMachine {
|
||||
public:
|
||||
class State {
|
||||
public:
|
||||
State(TDerived& a_StateMachine) : m_StateMachine(a_StateMachine) {}
|
||||
virtual ~State() {}
|
||||
|
||||
virtual TReturn Update(TArgs... args) = 0;
|
||||
|
||||
protected:
|
||||
TDerived& m_StateMachine;
|
||||
};
|
||||
|
||||
StateMachine() {}
|
||||
virtual ~StateMachine() {}
|
||||
|
||||
TReturn Update(TArgs... args) {
|
||||
assert(m_State);
|
||||
return m_State->Update(args...);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void ChangeState(Args&&... args) {
|
||||
m_State = std::make_unique<T>(static_cast<TDerived&>(*this), args...);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<State> m_State;
|
||||
};
|
||||
|
||||
|
||||
} // namespace td
|
||||
@@ -16,8 +16,7 @@ class ClientSimulation : public protocol::PacketHandler {
|
||||
std::uint64_t m_StepTime;
|
||||
game::World& m_World;
|
||||
GameBuffer m_History;
|
||||
std::uint64_t m_CurrentTime;
|
||||
std::uint64_t m_LastTime;
|
||||
float m_CurrentTime;
|
||||
StepTime m_CurrentStep;
|
||||
|
||||
std::shared_ptr<WorldSnapshot> m_LastSnapshot;
|
||||
@@ -45,7 +44,7 @@ class ClientSimulation : public protocol::PacketHandler {
|
||||
/**
|
||||
* \return the progress [0-1] between two steps
|
||||
*/
|
||||
float Update();
|
||||
float Update(float a_Delta);
|
||||
|
||||
virtual void Handle(const protocol::packets::LockStepsPacket& a_LockSteps) override;
|
||||
virtual void Handle(const protocol::packets::LockStepResponsePacket& a_LockSteps) override;
|
||||
|
||||
@@ -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