Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
BulletCleanupSystem.cpp
Go to the documentation of this file.
1
9
10namespace rtp::server
11{
13 RoomSystem& roomSystem,
14 NetworkSyncSystem& networkSync)
15 : _registry(registry), _roomSystem(roomSystem), _networkSync(networkSync)
16 {
17 }
18
19 void BulletCleanupSystem::despawn(const ecs::Entity& entity, uint32_t roomId)
20 {
21 auto transformRes = _registry.get<ecs::components::Transform>();
24 if (!transformRes || !typeRes || !netRes) {
25 return;
26 }
27
28 auto &transforms = transformRes->get();
29 auto &types = typeRes->get();
30 auto &nets = netRes->get();
31 if (!transforms.has(entity) || !types.has(entity) || !nets.has(entity)) {
32 return;
33 }
34
35 const auto &transform = transforms[entity];
36 const auto &type = types[entity];
37 const auto &net = nets[entity];
38
39 auto room = _roomSystem.getRoom(roomId);
40 if (!room) {
41 _registry.kill(entity);
42 return;
43 }
44
45 const auto players = room->getPlayers();
46 if (players.empty()) {
47 _registry.kill(entity);
48 return;
49 }
50
53 payload.netId = net.id;
54 payload.type = static_cast<uint8_t>(type.type);
55 payload.position = transform.position;
56 packet << payload;
57
58 for (const auto& player : players) {
60 }
61
62 _registry.kill(entity);
63 }
64
66 {
67 (void)dt;
68 auto transformsRes = _registry.get<ecs::components::Transform>();
69 auto typesRes = _registry.get<ecs::components::EntityType>();
70 auto roomsRes = _registry.get<ecs::components::RoomId>();
71
72 if (!transformsRes || !typesRes || !roomsRes) {
73 return;
74 }
75
76 auto &transforms = transformsRes->get();
77 auto &types = typesRes->get();
78 auto &rooms = roomsRes->get();
79
80 std::vector<std::pair<ecs::Entity, uint32_t>> pending;
81
82 for (auto entity : types.entities()) {
83 if (!types.has(entity)) {
84 continue;
85 }
86 const auto etype = types[entity].type;
87 if (etype != net::EntityType::Bullet &&
90 etype != net::EntityType::Scout &&
91 etype != net::EntityType::Tank &&
92 etype != net::EntityType::Boss &&
96 continue;
97 }
98 if (!transforms.has(entity) || !rooms.has(entity)) {
99 continue;
100 }
101
102 const auto &tf = transforms[entity];
103 if (tf.position.x < _minX || tf.position.x > _maxX) {
104 pending.emplace_back(entity, rooms[entity].id);
105 }
106 }
107
108 for (const auto& [entity, roomId] : pending) {
109 despawn(entity, roomId);
110 }
111 }
112} // namespace rtp::server
Represents an entity in the ECS (Entity-Component-System) architecture.
Definition Entity.hpp:63
void kill(Entity entity)
Definition Registry.cpp:73
auto get(this const Self &self) -> std::expected< std::reference_wrapper< ConstLike< Self, SparseArray< T > > >, rtp::Error >
Network packet with header and serializable body.
Definition Packet.hpp:471
float _minX
Left boundary for bullet despawn.
void despawn(const ecs::Entity &entity, uint32_t roomId)
Despawn an entity and notify players in the room.
RoomSystem & _roomSystem
Reference to the RoomSystem.
BulletCleanupSystem(ecs::Registry &registry, RoomSystem &roomSystem, NetworkSyncSystem &networkSync)
Constructor for BulletCleanupSystem.
void update(float dt) override
Update system logic for one frame.
float _maxX
Right boundary for bullet despawn.
NetworkSyncSystem & _networkSync
Reference to the NetworkSyncSystem.
ecs::Registry & _registry
Reference to the ECS registry.
System to handle network-related operations on the server side.
void sendPacketToSession(uint32_t sessionId, const net::Packet &packet, net::NetworkMode mode)
Send a packet to a specific session.
System to handle room-related operations on the server side.
std::shared_ptr< Room > getRoom(uint32_t roomId)
Get a room by its ID.
File : Network.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue.hajjar-llauquen@epit...
@ EntityDeath
Entity death notification.
File : GameManager.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
Component representing a network identifier for an entity.
Definition NetworkId.hpp:22
Component representing a network identifier for an entity.
Definition RoomId.hpp:22
Component representing position, rotation, and scale of an entity.
Definition Transform.hpp:23
Entity death notification data.
Definition Packet.hpp:362
uint32_t netId
Network entity identifier.
Definition Packet.hpp:363