R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
SharedTypes.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created on 09/12/2025.
4** File description:
5** SharedTypes.hpp
6*/
7
8#pragma once
9
10#include <cstdint>
11#include <vector>
12#include "schemas/shared_types.capnp.h"
13
14// Cross-platform unreachable macro
15#ifdef _MSC_VER
16#define UNREACHABLE() __assume(0)
17#else
18#define UNREACHABLE() __builtin_unreachable()
19#endif
20
22
28 struct Vec2 {
29 float x;
30 float y;
31
32 Vec2() : x(0.0f), y(0.0f) {}
33 Vec2(float x, float y) : x(x), y(y) {}
34
35 // Convert to Cap'n Proto
36 void toCapnp(::Vec2::Builder builder) const {
37 builder.setX(x);
38 builder.setY(y);
39 }
40
41 // Convert from Cap'n Proto
42 static Vec2 fromCapnp(::Vec2::Reader reader) { return Vec2(reader.getX(), reader.getY()); }
43 };
44
48 enum class Action { MoveUp = 0, MoveDown = 1, MoveLeft = 2, MoveRight = 3, Shoot = 4 };
49
53 enum class EntityType {
54 Player = 0,
55 EnemyType1 = 1,
56 PlayerBullet = 2,
57 EnemyBullet = 3,
58 Wall = 4,
60 };
61
65 enum class DestroyReason { KilledByPlayer = 0, OutOfBounds = 1, Collision = 2 };
66
67 // Conversion helpers for enums
68 inline ::Action toCapnpAction(Action action) {
69 switch (action) {
70 case Action::MoveUp:
71 return ::Action::MOVE_UP;
73 return ::Action::MOVE_DOWN;
75 return ::Action::MOVE_LEFT;
77 return ::Action::MOVE_RIGHT;
78 case Action::Shoot:
79 return ::Action::SHOOT;
80 }
82 }
83
85 switch (action) {
86 case ::Action::MOVE_UP:
87 return Action::MoveUp;
88 case ::Action::MOVE_DOWN:
89 return Action::MoveDown;
90 case ::Action::MOVE_LEFT:
91 return Action::MoveLeft;
92 case ::Action::MOVE_RIGHT:
93 return Action::MoveRight;
94 case ::Action::SHOOT:
95 return Action::Shoot;
96 }
98 }
99
100 inline ::EntityType toCapnpEntityType(EntityType type) {
101 switch (type) {
103 return ::EntityType::PLAYER;
105 return ::EntityType::ENEMY_TYPE1;
107 return ::EntityType::PLAYER_BULLET;
109 return ::EntityType::ENEMY_BULLET;
110 case EntityType::Wall:
111 return ::EntityType::WALL;
113 return ::EntityType::ORBITAL_MODULE;
114 }
115 UNREACHABLE();
116 }
117
119 switch (type) {
120 case ::EntityType::PLAYER:
121 return EntityType::Player;
122 case ::EntityType::ENEMY_TYPE1:
124 case ::EntityType::PLAYER_BULLET:
126 case ::EntityType::ENEMY_BULLET:
128 case ::EntityType::WALL:
129 return EntityType::Wall;
130 case ::EntityType::ORBITAL_MODULE:
132 }
133 UNREACHABLE();
134 }
135
136 inline ::DestroyReason toCapnpDestroyReason(DestroyReason reason) {
137 switch (reason) {
139 return ::DestroyReason::KILLED_BY_PLAYER;
141 return ::DestroyReason::OUT_OF_BOUNDS;
143 return ::DestroyReason::COLLISION;
144 }
145 UNREACHABLE();
146 }
147
149 switch (reason) {
150 case ::DestroyReason::KILLED_BY_PLAYER:
152 case ::DestroyReason::OUT_OF_BOUNDS:
154 case ::DestroyReason::COLLISION:
156 }
157 UNREACHABLE();
158 }
159
166 uint32_t sequenceId;
167 std::vector<Action> actions{};
168
170 PlayerInputDTO(uint32_t sequenceId, const std::vector<Action> &actions)
172 };
173
174} // namespace RType::Messages::Shared
#define UNREACHABLE()
DestroyReason fromCapnpDestroyReason(::DestroyReason reason)
EntityType
Entity type enum - matches Cap'n Proto enum.
Action fromCapnpAction(::Action action)
EntityType fromCapnpEntityType(::EntityType type)
DestroyReason
Destroy reason enum - matches Cap'n Proto enum.
inline ::Action toCapnpAction(Action action)
inline ::DestroyReason toCapnpDestroyReason(DestroyReason reason)
inline ::EntityType toCapnpEntityType(EntityType type)
Action
Player action enum - matches Cap'n Proto enum.
Data Transfer Object for player input.
PlayerInputDTO(uint32_t sequenceId, const std::vector< Action > &actions)
C++ wrapper for Vec2 position.
static Vec2 fromCapnp(::Vec2::Reader reader)
void toCapnp(::Vec2::Builder builder) const