Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
components.hpp
Go to the documentation of this file.
1
3#pragma once
6
7#include <cstddef>
8#include <cstdint>
9#include <functional>
10
11namespace aegis2d::ecs {
12
17struct Transform {
20};
21
25struct Velocity {
28};
29
34struct Sprite {
37 std::uint32_t texture = 0;
41 std::uint32_t frame = 0;
43 std::int32_t layer = 0;
46 Vec4 tint{1.0F, 1.0F, 1.0F, 1.0F};
47};
48
56struct Animation {
58 std::uint32_t first = 0;
60 std::uint32_t count = 1;
62 float frame_time = 0.1F;
64 bool loop = true;
66 float elapsed = 0.0F;
67
70 [[nodiscard]] constexpr float duration() const noexcept {
71 return static_cast<float>(count == 0 ? 1 : count) * frame_time;
72 }
73
76 [[nodiscard]] constexpr std::uint32_t frame() const noexcept {
77 const std::uint32_t frames = count == 0 ? 1 : count;
78 if (!(frame_time > 0.0F) || !(elapsed > 0.0F)) {
79 return first;
80 }
81 const float steps = elapsed / frame_time;
82 if (!(steps < static_cast<float>(frames))) {
83 return loop ? first : first + frames - 1;
84 }
85 const auto step = static_cast<std::uint32_t>(steps);
86 return first + (step < frames ? step : frames - 1);
87 }
88
91 [[nodiscard]] constexpr bool finished() const noexcept {
92 return !loop && !(elapsed < duration());
93 }
94};
95
101struct Collider {
107 std::uint32_t layer = 0;
109 std::uint32_t mask = 0;
110};
111
115struct Health {
117 std::int32_t points = 1;
118
121 [[nodiscard]] constexpr bool depleted() const noexcept { return points <= 0; }
122};
123
128struct Damage {
130 std::int32_t amount = 1;
132 bool consumed_on_hit = true;
133};
134
137struct Owner {
140};
141
143struct Score {
145 std::uint32_t points = 0;
146};
147
151 std::uint32_t points = 100;
152};
153
158struct Lives {
160 std::uint32_t count = 3;
164 std::int32_t health = 1;
165};
166
168enum class Action : std::uint8_t {
170 Up = 1U << 0U,
172 Down = 1U << 1U,
174 Left = 1U << 2U,
176 Right = 1U << 3U,
178 Fire = 1U << 4U,
179};
180
186 std::uint8_t held = 0;
187
191 [[nodiscard]] constexpr bool holds(Action action) const noexcept {
192 return (held & static_cast<std::uint8_t>(action)) != 0U;
193 }
194
198 constexpr void set(Action action, bool pressed) noexcept {
199 const auto bit = static_cast<std::uint8_t>(action);
200 held = static_cast<std::uint8_t>(pressed ? held | bit : held & ~bit);
201 }
202};
203
210
220 float margin = 0.0F;
221};
222
235 float period = 1.0F;
238 float elapsed = 0.0F;
239};
240
245struct NetworkId {
248
252 [[nodiscard]] static constexpr NetworkId of(Entity entity) noexcept {
253 return NetworkId{.value = entity.id()};
254 }
255
257 friend constexpr bool operator==(const NetworkId&, const NetworkId&) noexcept = default;
258};
259
260} // namespace aegis2d::ecs
261
262// Hashes a network id by its value, for the client's id-to-entity map.
264template <>
265struct std::hash<aegis2d::ecs::NetworkId> {
266 std::size_t operator()(const aegis2d::ecs::NetworkId& id) const noexcept {
267 return std::hash<aegis2d::ecs::Entity::Id>{}(id.value);
268 }
269};
271
272// Moved to the shmup module; the old names stay until the games use the new ones.
274
275namespace aegis2d::ecs {
276using shmup::AutoTrigger;
277using shmup::Weapon;
278} // namespace aegis2d::ecs
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
std::uint32_t Id
Packed value: generation in the 8 high bits, index in the 24 low bits.
Definition entity.hpp:30
Generational entity handle: an index into the component pools plus a generation.
Vector and matrix types of the engine (GLM behind engine names).
Entity-Component-System used by client and server.
Definition animation.hpp:13
Action
Actions a player can hold, one bit each in PlayerInput.
Definition components.hpp:168
@ Up
Move up (towards y = 0).
@ Right
Move right.
Root namespace of the engine.
Definition audio.hpp:17
glm::vec4 Vec4
4D vector of floats (glm::vec4), e.g. homogeneous positions.
Definition math.hpp:25
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Components of the shoot'em up module: player ships and weapons.
Frames of the entity's Sprite shown in turn, at a steady rate.
Definition components.hpp:56
float elapsed
Seconds into the clip; kept below duration() while looping.
Definition components.hpp:66
bool loop
true to start again after the last frame, false to stop on it.
Definition components.hpp:64
std::uint32_t first
First frame of the clip in the sprite sheet.
Definition components.hpp:58
constexpr float duration() const noexcept
Length of the clip.
Definition components.hpp:70
constexpr std::uint32_t frame() const noexcept
Frame to show now.
Definition components.hpp:76
constexpr bool finished() const noexcept
Tells whether a clip that does not loop has shown its last frame long enough.
Definition components.hpp:91
std::uint32_t count
Number of frames of the clip; 0 counts as 1.
Definition components.hpp:60
float frame_time
Seconds each frame stays on screen; 0 or less keeps the first frame.
Definition components.hpp:62
Axis-aligned box an entity collides with.
Definition components.hpp:101
std::uint32_t layer
Layers the entity belongs to, one bit each.
Definition components.hpp:107
Vec2 offset
Center of the box relative to the entity's position.
Definition components.hpp:105
std::uint32_t mask
Layers the entity can collide with.
Definition components.hpp:109
Vec2 size
Width and height of the box, in logical pixels.
Definition components.hpp:103
How much damage an entity deals to what it hits.
Definition components.hpp:128
bool consumed_on_hit
true if the entity disappears after its first hit (missiles).
Definition components.hpp:132
std::int32_t amount
Health points removed from the target by one hit.
Definition components.hpp:130
Entity destroyed once its Animation has played to the end: a visual effect that lasts as long as its ...
Definition components.hpp:209
How much damage an entity can still take.
Definition components.hpp:115
constexpr bool depleted() const noexcept
Tells whether the entity has no health left.
Definition components.hpp:121
std::int32_t points
Remaining health points.
Definition components.hpp:117
Lives of a player, and how it comes back after losing one.
Definition components.hpp:158
Vec2 spawn
Where the player respawns (Transform::position).
Definition components.hpp:162
std::uint32_t count
Lives left, the current one included; 0 once the player is out.
Definition components.hpp:160
std::int32_t health
Health points given back on respawn.
Definition components.hpp:164
Identity of an entity shared by the server and the clients.
Definition components.hpp:245
Entity::Id value
The server entity's Entity::id().
Definition components.hpp:247
friend constexpr bool operator==(const NetworkId &, const NetworkId &) noexcept=default
Compares the ids.
static constexpr NetworkId of(Entity entity) noexcept
Network id of a server entity.
Definition components.hpp:252
Entity destroyed once it has left the play area for good.
Definition components.hpp:217
float margin
Distance beyond each edge of the area, in logical pixels, before the entity counts as gone.
Definition components.hpp:220
Entity that created this one, e.g. the player who fired a missile.
Definition components.hpp:137
Entity entity
The creator; null if there is none.
Definition components.hpp:139
Actions a player holds during the tick, as received from its client.
Definition components.hpp:184
std::uint8_t held
Held actions, one Action bit each.
Definition components.hpp:186
constexpr bool holds(Action action) const noexcept
Tells whether an action is held.
Definition components.hpp:191
constexpr void set(Action action, bool pressed) noexcept
Holds or releases an action.
Definition components.hpp:198
Points earned by the player who destroys this entity.
Definition components.hpp:149
std::uint32_t points
Points of one kill, 100 by default.
Definition components.hpp:151
Points a player has earned.
Definition components.hpp:143
std::uint32_t points
Total points; it stops at the largest value instead of wrapping.
Definition components.hpp:145
Sine wave added to an entity's movement, like an enemy weaving up and down.
Definition components.hpp:230
Vec2 amplitude
Largest offset from the path, in logical pixels, and its direction: {0, 48} weaves 48 pixels up and d...
Definition components.hpp:233
float period
Seconds for one full wave; a period of zero or less does not move the entity.
Definition components.hpp:235
float elapsed
Seconds into the current wave, kept in [0, period). Starting above 0 shifts the phase,...
Definition components.hpp:238
What an entity looks like.
Definition components.hpp:34
std::int32_t layer
Drawing order: higher layers are drawn over lower ones.
Definition components.hpp:43
Vec2 size
Size on screen, in logical pixels.
Definition components.hpp:39
std::uint32_t texture
Texture of the resource manager (render::TextureId), 0 for a plain rectangle.
Definition components.hpp:37
Vec4 tint
RGBA color multiplied with the texture, components in [0, 1]; opaque white keeps it unchanged.
Definition components.hpp:46
std::uint32_t frame
Frame of the texture's sprite sheet, 0 for the first (or only) frame.
Definition components.hpp:41
Where an entity is.
Definition components.hpp:17
Vec2 position
Center of the entity.
Definition components.hpp:19
How fast an entity moves.
Definition components.hpp:25
Vec2 value
Speed in logical pixels per second.
Definition components.hpp:27