R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
events.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <rtype/snapshot.hpp>
6
7#include <cstddef>
8#include <cstdint>
9#include <optional>
10#include <span>
11#include <string>
12#include <variant>
13#include <vector>
14
15namespace rtype {
16
19enum class EventType : std::uint8_t {
21 Spawn = 1,
23 Destroy = 2,
25 Death = 3,
27 PlayerJoined = 4,
29 PlayerLeft = 5,
31 GameOver = 6,
32};
33
36enum class DestroyReason : std::uint8_t {
38 Despawned = 0,
40 Killed = 1,
42 Consumed = 2,
44 PlayerLeft = 3,
45};
46
49enum class LeaveReason : std::uint8_t {
51 Left = 0,
53 TimedOut = 1,
55 Kicked = 2,
56};
57
59inline constexpr std::uint8_t no_player = 255;
60
62inline constexpr std::size_t spawn_event_size = 27;
64inline constexpr std::size_t destroy_event_size = 18;
66inline constexpr std::size_t death_event_size = 15;
68inline constexpr std::size_t player_left_event_size = 7;
69
71struct SpawnEvent {
73 std::uint32_t tick = 0;
75 std::uint32_t id = 0;
77 std::uint8_t kind = 0;
79 float x = 0.0F;
81 float y = 0.0F;
83 float vx = 0.0F;
85 float vy = 0.0F;
87 std::uint8_t owner = no_player;
88
91 bool operator==(const SpawnEvent&) const = default;
92};
93
97 std::uint32_t tick = 0;
99 std::uint32_t id = 0;
103 float x = 0.0F;
105 float y = 0.0F;
106
109 bool operator==(const DestroyEvent&) const = default;
110};
111
115 std::uint32_t tick = 0;
117 std::uint8_t player = 0;
119 std::uint8_t lives = 0;
121 float x = 0.0F;
123 float y = 0.0F;
124
127 bool operator==(const DeathEvent&) const = default;
128};
129
133 std::uint32_t tick = 0;
135 std::uint8_t player = 0;
137 std::string name;
138
141 bool operator==(const PlayerJoinedEvent&) const = default;
142};
143
147 std::uint32_t tick = 0;
149 std::uint8_t player = 0;
152
155 bool operator==(const PlayerLeftEvent&) const = default;
156};
157
160 std::variant<SpawnEvent, DestroyEvent, DeathEvent, PlayerJoinedEvent, PlayerLeftEvent>;
161
166[[nodiscard]] std::vector<std::byte> write_event(const GameEvent& event);
167
171[[nodiscard]] std::vector<std::vector<std::byte>> write_events(std::span<const GameEvent> events);
172
180[[nodiscard]] std::optional<GameEvent> read_event(std::span<const std::byte> bytes);
181
182} // namespace rtype
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
LeaveReason
Why a player left the game, in PlayerLeft.
Definition events.hpp:49
@ Left
It quit: Logout, Disconnect, or its TCP connection closed.
@ TimedOut
No valid datagram for too long, or no event acknowledged (RFC section 5.5).
@ Kicked
A protocol error ended its session (RFC section 3.2).
std::vector< std::vector< std::byte > > write_events(std::span< const GameEvent > events)
Encodes events with write_event(), for ServerNetwork::send_events().
std::variant< SpawnEvent, DestroyEvent, DeathEvent, PlayerJoinedEvent, PlayerLeftEvent > GameEvent
One of the gameplay events the server sends.
Definition events.hpp:160
DestroyReason
Why an entity was destroyed, which tells the client what to show.
Definition events.hpp:36
@ Consumed
A missile that hit its target: it vanishes.
@ Despawned
It left the play area: no effect.
@ Killed
It was destroyed: an explosion.
std::vector< std::byte > write_event(const GameEvent &event)
Encodes an event as the bytes of RFC section 4.4: type, tick, then its fields.
constexpr std::size_t spawn_event_size
Size of a Spawn event, type and tick included.
Definition events.hpp:62
constexpr std::size_t player_left_event_size
Size of a PlayerLeft event, type and tick included.
Definition events.hpp:68
EventType
Type of an event, its first byte (RFC section 4.4).
Definition events.hpp:19
@ Destroy
An entity disappears.
@ Spawn
An entity appears; a missile's is also its shot.
@ Death
A player lost a life.
@ PlayerLeft
A player left the game.
@ GameOver
No player has lives left.
@ PlayerJoined
A player joined the game.
std::optional< GameEvent > read_event(std::span< const std::byte > bytes)
Decodes the bytes of one event, as a client does (RFC section 6.4).
constexpr std::size_t destroy_event_size
Size of a Destroy event, type and tick included.
Definition events.hpp:64
constexpr std::uint8_t no_player
Player number meaning "no player", as the owner of an enemy.
Definition events.hpp:59
constexpr std::size_t death_event_size
Size of a Death event, type and tick included.
Definition events.hpp:66
Snapshots of the world sent to the clients (RFC section 4.3).
A player lost a life where its ship was.
Definition events.hpp:113
float x
Position of the ship when it was hit, in logical pixels.
Definition events.hpp:121
bool operator==(const DeathEvent &) const =default
Field-by-field equality.
std::uint8_t player
Player number, 0 to 3.
Definition events.hpp:117
float y
Position of the ship when it was hit, in logical pixels.
Definition events.hpp:123
std::uint32_t tick
Server tick of the death.
Definition events.hpp:115
std::uint8_t lives
Lives left; 0 means the player is out, and a Destroy of its ship follows.
Definition events.hpp:119
An entity disappears, at its last position.
Definition events.hpp:95
bool operator==(const DestroyEvent &) const =default
Field-by-field equality.
std::uint32_t tick
Server tick at which it disappeared.
Definition events.hpp:97
float x
Last position of its center, in logical pixels.
Definition events.hpp:103
float y
Last position of its center, in logical pixels.
Definition events.hpp:105
DestroyReason reason
Why, which tells the client what to show.
Definition events.hpp:101
A player joined the game.
Definition events.hpp:131
bool operator==(const PlayerJoinedEvent &) const =default
Field-by-field equality.
std::string name
Name given at login, valid as valid_player_name() says.
Definition events.hpp:137
std::uint32_t tick
Server tick at which it joined.
Definition events.hpp:133
std::uint8_t player
Player number, 0 to 3.
Definition events.hpp:135
A player left the game; the Destroy of its ship follows if it had one.
Definition events.hpp:145
bool operator==(const PlayerLeftEvent &) const =default
Field-by-field equality.
std::uint8_t player
Player number, 0 to 3.
Definition events.hpp:149
std::uint32_t tick
Server tick at which it left.
Definition events.hpp:147
LeaveReason reason
Why it left.
Definition events.hpp:151
An entity appears, with its kind, position and velocity.
Definition events.hpp:71
float vy
Velocity, in logical pixels per second.
Definition events.hpp:85
float y
Position of its center, in logical pixels.
Definition events.hpp:81
float vx
Velocity, in logical pixels per second.
Definition events.hpp:83
bool operator==(const SpawnEvent &) const =default
Field-by-field equality.
std::uint8_t owner
Player it belongs to (a ship's player, a missile's shooter), or no_player.
Definition events.hpp:87
float x
Position of its center, in logical pixels.
Definition events.hpp:79
std::uint8_t kind
Kind, an EntityKind value.
Definition events.hpp:77
std::uint32_t tick
Server tick at which it appeared.
Definition events.hpp:73