R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
GameStateSerializer.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** rtype
4** File description:
5** GameStateSerializer.cpp - Game state serialization implementation
6*/
7
9#include <algorithm>
10#include <cmath>
11#include <iostream>
19
20namespace server {
21
23 uint32_t serverTick) {
24 GameStateSnapshot snapshot;
25 snapshot.serverTick = serverTick;
26 snapshot.activePlayerCount = 0;
27
28 // Get all entities with Transform component (all visible entities)
29 auto entities = world.query<ecs::Transform>();
30
31 // Serialize each entity
32 for (auto &entity : entities) {
33 EntitySnapshot entitySnapshot = serializeEntity(world, entity.getAddress());
34 snapshot.entities.push_back(entitySnapshot);
35
36 // Count players
37 if (entity.has<ecs::Player>()) {
38 snapshot.activePlayerCount++;
39 }
40 }
41
42 return snapshot;
43 }
44
46 ecs::wrapper::ECSWorld &world, uint32_t serverTick,
47 [[maybe_unused]] const GameStateSnapshot &lastSnapshot) {
48 // Currently returns full snapshot for simplicity and reliability
49 // Delta compression would compare with lastSnapshot and only include:
50 // - New entities (not in lastSnapshot)
51 // - Entities with changed position/health/state
52 // - Destroyed entities (in lastSnapshot but not in current world)
53 // This optimization can be implemented when network bandwidth becomes a bottleneck
54 return createFullSnapshot(world, serverTick);
55 }
56
58 EntitySnapshot snapshot;
59 snapshot.entityId = entityId;
60 snapshot.posX = 0.0f;
61 snapshot.posY = 0.0f;
62 snapshot.velX = 0.0f;
63 snapshot.velY = 0.0f;
64 snapshot.currentHealth = 0;
65 snapshot.maxHealth = 0;
66 snapshot.playerId = 0;
67 snapshot.isAlive = true;
68
69 try {
70 ecs::wrapper::Entity entity = world.getEntity(entityId);
71
72 // Try to get Transform
73 if (entity.has<ecs::Transform>()) {
74 const ecs::Transform &transform = entity.get<ecs::Transform>();
75 snapshot.posX = transform.getPosition().x;
76 snapshot.posY = transform.getPosition().y;
77 }
78
79 // Try to get Velocity
80 if (entity.has<ecs::Velocity>()) {
81 const ecs::Velocity &velocity = entity.get<ecs::Velocity>();
82 ecs::Velocity::Vector2 dir = velocity.getDirection();
83 float speed = velocity.getSpeed();
84 snapshot.velX = dir.x * speed;
85 snapshot.velY = dir.y * speed;
86 }
87
88 // Try to get Health
89 if (entity.has<ecs::Health>()) {
90 const ecs::Health &health = entity.get<ecs::Health>();
91 snapshot.currentHealth = health.getCurrentHealth();
92 snapshot.maxHealth = health.getMaxHealth();
93 snapshot.isAlive = (health.getCurrentHealth() > 0);
94 }
95
96 // Try to get Player
97 if (entity.has<ecs::Player>()) {
98 const ecs::Player &player = entity.get<ecs::Player>();
99 snapshot.playerId = player.getPlayerId();
100 }
101 } catch (const std::exception &e) {
102 LOG_ERROR("Error serializing entity ", entityId, ": ", e.what());
103 }
104
105 return snapshot;
106 }
107
108} // namespace server
#define LOG_ERROR(...)
Definition Logger.hpp:183
Component representing entity health and invincibility.
Definition Health.hpp:20
int getCurrentHealth() const
Get current health points.
Definition Health.hpp:44
int getMaxHealth() const
Get maximum health points.
Definition Health.hpp:50
Component identifying an entity as a player with game statistics.
Definition Player.hpp:20
int getPlayerId() const
Get player ID.
Definition Player.hpp:47
Component representing position, rotation and scale in 2D space.
Definition Transform.hpp:20
Vector2 getPosition() const
Get the position vector.
Definition Transform.hpp:61
Component representing movement direction and speed.
Definition Velocity.hpp:20
Vector2 getDirection() const
Get the direction vector.
Definition Velocity.hpp:45
float getSpeed() const
Get the movement speed.
Definition Velocity.hpp:51
High-level ECS manager providing clean server-side API.
Definition ECSWorld.hpp:122
std::vector< Entity > query()
Get all entities with specific components.
Entity getEntity(Address address)
Get an entity wrapper from an address.
Definition ECSWorld.cpp:83
High-level entity wrapper providing fluent interface.
Definition ECSWorld.hpp:33
bool has() const
Check if this entity has a specific component.
T & get()
Get a component from this entity.
static GameStateSnapshot createDeltaUpdate(ecs::wrapper::ECSWorld &world, uint32_t serverTick, const GameStateSnapshot &lastSnapshot)
Create a delta update (changed entities only)
static EntitySnapshot serializeEntity(ecs::wrapper::ECSWorld &world, uint32_t entityId)
Serialize entity to network format.
static GameStateSnapshot createFullSnapshot(ecs::wrapper::ECSWorld &world, uint32_t serverTick)
Create a full game state snapshot.
float x
X coordinate.
Definition Transform.hpp:27
float y
Y coordinate.
Definition Transform.hpp:28
2D vector for direction.
Definition Velocity.hpp:26
Serialized entity state for network transmission.
Complete game state at a given tick.
std::vector< EntitySnapshot > entities