begin client-server

This commit is contained in:
2025-08-06 20:10:56 +02:00
parent 89213e9a97
commit c813c49707
26 changed files with 264 additions and 188 deletions

View File

@@ -1,9 +1,9 @@
#include <iostream>
#include <td/protocol/packet/PacketSerialize.h>
#include <fstream>
#include <td/game/World.h>
#include <td/input/Display.h>
#include <td/protocol/packet/PacketSerialize.h>
#include <td/protocol/packet/Packets.h>
#include <td/render/renderer/EntityRenderer.h>
#include <td/render/renderer/TowerRenderer.h>
@@ -17,7 +17,7 @@
#include <server/Server.h>
#include <server/socket/FakeSocket.h>
#include <server/state/LobbyState.h>
#include <server/state/GameState.h>
class WorldApply : public td::protocol::PacketHandler {
private:
@@ -36,6 +36,24 @@ class WorldApply : public td::protocol::PacketHandler {
}
};
class ClientHandler : public td::protocol::PacketHandler {
private:
td::sim::ClientSimulation& m_Simulation;
using td::protocol::PacketHandler::Handle;
public:
ClientHandler(td::sim::ClientSimulation& a_Simulation) : m_Simulation(a_Simulation) {}
void Handle(const td::protocol::packets::LockStepsPacket& a_LockStep) {
m_Simulation.Handle(a_LockStep);
}
void Handle(const td::protocol::packets::PredictCommandPacket& a_Predict) {
m_Simulation.Handle(a_Predict);
}
};
void Save(const td::protocol::PacketBase& header, const td::protocol::PacketBase& data) {
auto comp = std::make_shared<sp::ZlibCompress>();
@@ -48,7 +66,7 @@ void Save(const td::protocol::PacketBase& header, const td::protocol::PacketBase
stream.WriteMessage(data, false);
}
td::game::World GetWorld() {
td::game::WorldPtr GetWorld() {
auto comp = std::make_shared<sp::ZlibCompress>();
std::ifstream fStream("test/tdmap.tdmap2");
@@ -59,8 +77,8 @@ td::game::World GetWorld() {
auto header = stream.ReadMessage(td::protocol::PacketID::WorldHeader);
auto data = stream.ReadMessage(td::protocol::PacketID::WorldData);
td::game::World w;
auto wa = std::make_shared<WorldApply>(w);
auto w = std::make_shared<td::game::World>();
auto wa = std::make_shared<WorldApply>(*w);
td::protocol::PacketDispatcher d;
d.RegisterHandler(wa);
@@ -80,24 +98,12 @@ void FastForward(td::game::World& a_World, const td::sim::GameHistory& a_LockSte
}
}
td::sim::GameHistory GetCustomHistory() {
constexpr std::size_t MAX_COUNT = 20 * 60 * 40;
td::sim::GameHistory gh(MAX_COUNT);
auto spawn = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::SpawnTroopCommand>(td::EntityType::Zombie, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
gh[0].push_back(spawn);
auto tower = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::PlaceTowerCommand>(td::TowerType::Archer, 0, td::TowerCoords{77, 13}));
gh[0].push_back(tower);
return gh;
}
int main(int argc, char** argv) {
td::game::World w = GetWorld();
td::game::WorldPtr serverWorld = GetWorld();
// server
auto fakeSocket = std::make_shared<td::server::FakeSocket>();
td::server::Server server(fakeSocket);
// init GL context
td::Display display(1920, 1080, "Tower-Defense 2");
@@ -106,42 +112,40 @@ int main(int argc, char** argv) {
display.OnAspectRatioChange.Connect([&cam](float a_AspectRatio) { cam.UpdatePerspective(a_AspectRatio); });
td::sim::GameHistory gh = GetCustomHistory();
td::game::WorldPtr clientWorld = GetWorld();
td::render::RenderPipeline renderer;
renderer.AddRenderer<td::render::WorldRenderer>(cam, w);
renderer.AddRenderer<td::render::EntityRenderer>(cam, w);
renderer.AddRenderer<td::render::TowerRenderer>(cam, w);
renderer.AddRenderer<td::render::WorldRenderer>(cam, *clientWorld);
renderer.AddRenderer<td::render::EntityRenderer>(cam, *clientWorld);
renderer.AddRenderer<td::render::TowerRenderer>(cam, *clientWorld);
cam.SetCamPos({77, 7, 13});
cam.UpdatePerspective(display.GetAspectRatio());
td::sim::ClientSimulation simulation(w, 50);
td::sim::ClientSimulation simulation(*clientWorld, td::STEP_TIME);
ClientHandler clientHandler(simulation);
display.OnKeyDown.Connect([&simulation](SDL_Keycode key) {
static int counter = 0;
// temporary tests
display.OnKeyDown.Connect([&fakeSocket](SDL_Keycode key) {
if (key == SDLK_A) {
auto spawn = td::protocol::CommandPtr(
std::make_shared<td::protocol::commands::SpawnTroopCommand>(td::EntityType::Zombie, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
std::array<td::protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps{};
steps[0].push_back(spawn);
td::protocol::packets::LockStepsPacket packet{counter * LOCKSTEP_BUFFER_SIZE * 3, steps};
simulation.Handle(packet);
counter++;
fakeSocket->OnReceive(0, td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
} else if (key == SDLK_Z) {
fakeSocket->OnReceive(0, td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
}
});
fakeSocket->ConnectFakePeer(0);
// server
auto socket = std::make_shared<td::server::FakeSocket>();
td::server::Server server(socket);
server.UpdateState(std::make_shared<td::server::LobbyState>());
server.Update(1.0f);
server.Update(1.0f);
socket->OnDisconnect(0);
// packets from the server to the client
fakeSocket->OnSend.Connect([&clientHandler](td::PeerID a_Peer, const td::protocol::PacketBase& a_Packet) {
a_Packet.Dispatch(clientHandler);
});
server.UpdateState(std::make_shared<td::server::GameState>(serverWorld));
while (!display.IsCloseRequested()) {
display.PollEvents();
server.Update();
float lerp = simulation.Update();
renderer.Render(lerp);
display.Update();