R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
EntityDestroyed.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created on 09/12/2025.
4** File description:
5** EntityDestroyed.hpp
6*/
7
8#pragma once
9
10#include <capnp/message.h>
11#include <capnp/serialize.h>
12#include <cstdint>
13#include <vector>
14#include "../Shared/SharedTypes.hpp"
15#include "schemas/s2c_messages.capnp.h"
16
17namespace RType::Messages::S2C {
18
26 public:
27 uint32_t entityId;
29
30 EntityDestroyed() : entityId(0), reason(Shared::DestroyReason::OutOfBounds) {}
32
33 [[nodiscard]] std::vector<uint8_t> serialize() const {
34 capnp::MallocMessageBuilder message;
35 auto builder = message.initRoot<::EntityDestroyed>();
36
37 builder.setEntityId(entityId);
38 builder.setReason(Shared::toCapnpDestroyReason(reason));
39
40 auto bytes = capnp::messageToFlatArray(message);
41 auto byteArray = bytes.asBytes();
42 return std::vector<uint8_t>(byteArray.begin(), byteArray.end());
43 }
44
45 static EntityDestroyed deserialize(const std::vector<uint8_t> &data) {
46 // Ensure buffer is word-aligned for Cap'n Proto (undefined behavior if not)
47 KJ_REQUIRE(data.size() % sizeof(capnp::word) == 0,
48 "Serialized data size must be a multiple of capnp::word");
49 auto aligned = kj::heapArray<uint8_t>(data.size());
50 memcpy(aligned.begin(), data.data(), data.size());
51 kj::ArrayPtr<const capnp::word> words(reinterpret_cast<const capnp::word *>(aligned.begin()),
52 data.size() / sizeof(capnp::word));
53
54 capnp::FlatArrayMessageReader message(words);
55 auto reader = message.getRoot<::EntityDestroyed>();
56
57 EntityDestroyed result;
58 result.entityId = reader.getEntityId();
59 result.reason = Shared::fromCapnpDestroyReason(reader.getReason());
60
61 return result;
62 }
63 };
64
65} // namespace RType::Messages::S2C
Entity destruction notification.
static EntityDestroyed deserialize(const std::vector< uint8_t > &data)
std::vector< uint8_t > serialize() const
EntityDestroyed(uint32_t id, Shared::DestroyReason r)
Server-to-Client messages.
Definition Server.hpp:34
DestroyReason fromCapnpDestroyReason(::DestroyReason reason)
DestroyReason
Destroy reason enum - matches Cap'n Proto enum.
inline ::DestroyReason toCapnpDestroyReason(DestroyReason reason)