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

@@ -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();
}