R-Type 0.1
Loading...
Searching...
No Matches
ServerDeathSystem.hpp
1#pragma once
2
3#include "Protocol/EntityPayload.hpp"
4#include "Server.hpp"
5#include "components/Dead.hpp"
6#include "components/NetworkEntity.hpp"
7#include <ISystem.hpp>
8#include <cstdint>
9#include <unordered_map>
10#include <vector>
11
12namespace rtype::server {
21class ServerDeathSystem final : public rtk::ecs::ISystem {
22public:
30 Server &server,
31 const std::unordered_map<std::uint32_t, std::size_t> &players,
32 const std::uint32_t &serverTick)
33 : _server(server), _players(players), _serverTick(serverTick) {}
34
35 ~ServerDeathSystem() override = default;
36
40 void onStart(rtk::ecs::Registry &) override {}
41
46 void update(rtk::ecs::Registry &reg, float) override {
47 auto view = reg.view<component::Dead>();
48 auto &netEntities = reg.get_components<component::NetworkEntity>();
49
50 std::vector<std::size_t> toDestroy;
51 for (auto entity : view) {
52 toDestroy.push_back(entity);
53 }
54
55 for (auto entity : toDestroy) {
56 auto *netEnt = netEntities.get(entity);
57 if (netEnt && netEnt->id != 0) {
58 const auto payload = protocol::destroyEntityPayload(
59 _serverTick, netEnt->id, protocol::DestroyReason::Collision);
60 for (const auto &[playerId, dummy] : _players) {
61 (void)dummy;
62 _server.sendDestroyEntity(playerId, payload);
63 }
64 }
65 reg.remove_entity_from_all_pools(entity);
66 reg.kill_entity(entity);
67 }
68 }
69
73 void onStop(rtk::ecs::Registry &) override {}
74
75private:
76 Server &_server;
77 const std::unordered_map<std::uint32_t, std::size_t> &_players;
78 const std::uint32_t &_serverTick;
79};
80
82} // namespace rtype::server
Destroys dead entities and tells every player with DestroyEntity.
Definition ServerDeathSystem.hpp:21
void update(rtk::ecs::Registry &reg, float) override
Broadcasts and destroys every entity tagged with Dead.
Definition ServerDeathSystem.hpp:46
void onStop(rtk::ecs::Registry &) override
Does nothing.
Definition ServerDeathSystem.hpp:73
ServerDeathSystem(Server &server, const std::unordered_map< std::uint32_t, std::size_t > &players, const std::uint32_t &serverTick)
Creates the system.
Definition ServerDeathSystem.hpp:29
void onStart(rtk::ecs::Registry &) override
Does nothing.
Definition ServerDeathSystem.hpp:40
Game level layer over NetworkServer that decodes client messages and encodes server messages.
Definition Server.hpp:30
bool sendDestroyEntity(std::uint32_t connectionId, const protocol::DestroyEntityPayload &payload)
Sends a DestroyEntity message.
Definition Server.cpp:170
DestroyEntityPayload destroyEntityPayload(std::uint32_t serverTick, std::uint32_t networkId, DestroyReason reason)
Builds a DestroyEntity payload.
Definition EntityPayload.hpp:142
@ Collision
Removed by gameplay, such as a hit.
Tag marking an entity for destruction at the end of the frame.
Definition Dead.hpp:17
Links a local entity to its replicated counterpart.
Definition NetworkEntity.hpp:18