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
8#include <aegis2d/script/export.hpp>
10
11#include <algorithm>
12#include <array>
13#include <cstddef>
14#include <cstdint>
15#include <span>
16#include <string_view>
17#include <vector>
18
19namespace aegis2d::script {
20
22namespace detail {
23
24// How a field reads and writes from Lua: a number, a whole number in a range, or a boolean.
25enum class FieldType : std::uint8_t { Number, Integer, Unsigned, Boolean };
26
27template <typename Component>
28struct Field {
29 using Getter = double (*)(const Component&);
30 using Setter = void (*)(Component&, double);
31
32 constexpr Field(std::string_view field_name, FieldType field_type, Getter getter,
33 Setter setter) noexcept
34 : name{field_name}, type{field_type}, get{getter}, set{setter} {}
35
36 std::string_view name;
38 Getter get = nullptr;
39 Setter set = nullptr;
40};
41
42// The fields of each component, vectors flattened.
43inline constexpr std::array transform_fields{
45 "x", FieldType::Number, [](const ecs::Transform& c) { return double{c.position.x}; },
46 [](ecs::Transform& c, double v) { c.position.x = static_cast<float>(v); }},
48 "y", FieldType::Number, [](const ecs::Transform& c) { return double{c.position.y}; },
49 [](ecs::Transform& c, double v) { c.position.y = static_cast<float>(v); }},
50};
51inline constexpr std::array velocity_fields{
53 [](const ecs::Velocity& c) { return double{c.value.x}; },
54 [](ecs::Velocity& c, double v) { c.value.x = static_cast<float>(v); }},
56 [](const ecs::Velocity& c) { return double{c.value.y}; },
57 [](ecs::Velocity& c, double v) { c.value.y = static_cast<float>(v); }},
58};
59inline constexpr std::array sprite_fields{
61 [](const ecs::Sprite& c) { return static_cast<double>(c.texture); },
62 [](ecs::Sprite& c, double v) { c.texture = static_cast<std::uint32_t>(v); }},
64 [](const ecs::Sprite& c) { return double{c.size.x}; },
65 [](ecs::Sprite& c, double v) { c.size.x = static_cast<float>(v); }},
67 [](const ecs::Sprite& c) { return double{c.size.y}; },
68 [](ecs::Sprite& c, double v) { c.size.y = static_cast<float>(v); }},
70 [](const ecs::Sprite& c) { return static_cast<double>(c.frame); },
71 [](ecs::Sprite& c, double v) { c.frame = static_cast<std::uint32_t>(v); }},
73 [](const ecs::Sprite& c) { return static_cast<double>(c.layer); },
74 [](ecs::Sprite& c, double v) { c.layer = static_cast<std::int32_t>(v); }},
76 [](const ecs::Sprite& c) { return double{c.tint.x}; },
77 [](ecs::Sprite& c, double v) { c.tint.x = static_cast<float>(v); }},
79 [](const ecs::Sprite& c) { return double{c.tint.y}; },
80 [](ecs::Sprite& c, double v) { c.tint.y = static_cast<float>(v); }},
82 [](const ecs::Sprite& c) { return double{c.tint.z}; },
83 [](ecs::Sprite& c, double v) { c.tint.z = static_cast<float>(v); }},
85 [](const ecs::Sprite& c) { return double{c.tint.w}; },
86 [](ecs::Sprite& c, double v) { c.tint.w = static_cast<float>(v); }},
87};
88inline constexpr std::array animation_fields{
90 "first", FieldType::Unsigned,
91 [](const ecs::Animation& c) { return static_cast<double>(c.first); },
92 [](ecs::Animation& c, double v) { c.first = static_cast<std::uint32_t>(v); }},
94 "count", FieldType::Unsigned,
95 [](const ecs::Animation& c) { return static_cast<double>(c.count); },
96 [](ecs::Animation& c, double v) { c.count = static_cast<std::uint32_t>(v); }},
98 "frame_time", FieldType::Number,
99 [](const ecs::Animation& c) { return double{c.frame_time}; },
100 [](ecs::Animation& c, double v) { c.frame_time = static_cast<float>(v); }},
102 [](const ecs::Animation& c) { return c.loop ? 1.0 : 0.0; },
103 [](ecs::Animation& c, double v) { c.loop = v != 0.0; }},
104};
105inline constexpr std::array collider_fields{
107 [](const ecs::Collider& c) { return double{c.size.x}; },
108 [](ecs::Collider& c, double v) { c.size.x = static_cast<float>(v); }},
110 [](const ecs::Collider& c) { return double{c.size.y}; },
111 [](ecs::Collider& c, double v) { c.size.y = static_cast<float>(v); }},
113 "layer", FieldType::Unsigned,
114 [](const ecs::Collider& c) { return static_cast<double>(c.layer); },
115 [](ecs::Collider& c, double v) { c.layer = static_cast<std::uint32_t>(v); }},
117 "mask", FieldType::Unsigned,
118 [](const ecs::Collider& c) { return static_cast<double>(c.mask); },
119 [](ecs::Collider& c, double v) { c.mask = static_cast<std::uint32_t>(v); }},
120};
121inline constexpr std::array health_fields{
123 [](const ecs::Health& c) { return static_cast<double>(c.points); },
124 [](ecs::Health& c, double v) { c.points = static_cast<std::int32_t>(v); }},
125};
126inline constexpr std::array damage_fields{
128 [](const ecs::Damage& c) { return static_cast<double>(c.amount); },
129 [](ecs::Damage& c, double v) { c.amount = static_cast<std::int32_t>(v); }},
130 Field<ecs::Damage>{"consumed_on_hit", FieldType::Boolean,
131 [](const ecs::Damage& c) { return c.consumed_on_hit ? 1.0 : 0.0; },
132 [](ecs::Damage& c, double v) { c.consumed_on_hit = v != 0.0; }},
133};
134inline constexpr std::array score_fields{
136 [](const ecs::Score& c) { return static_cast<double>(c.points); },
137 [](ecs::Score& c, double v) { c.points = static_cast<std::uint32_t>(v); }},
138};
139inline constexpr std::array sine_motion_fields{
141 "x", FieldType::Number, [](const ecs::SineMotion& c) { return double{c.amplitude.x}; },
142 [](ecs::SineMotion& c, double v) { c.amplitude.x = static_cast<float>(v); }},
144 "y", FieldType::Number, [](const ecs::SineMotion& c) { return double{c.amplitude.y}; },
145 [](ecs::SineMotion& c, double v) { c.amplitude.y = static_cast<float>(v); }},
147 [](const ecs::SineMotion& c) { return double{c.period}; },
148 [](ecs::SineMotion& c, double v) { c.period = static_cast<float>(v); }},
149};
150// The waypoints stay with the C++ code; scripts change the pace, such as a slow.
151inline constexpr std::array path_follower_fields{
153 "speed", FieldType::Number, [](const ecs::PathFollower& c) { return double{c.speed}; },
154 [](ecs::PathFollower& c, double v) { c.speed = static_cast<float>(v); }},
156 "progress", FieldType::Number,
157 [](const ecs::PathFollower& c) { return double{c.progress}; },
158 [](ecs::PathFollower& c, double v) { c.progress = static_cast<float>(v); }},
160 [](const ecs::PathFollower& c) { return c.arrived ? 1.0 : 0.0; },
161 [](ecs::PathFollower& c, double v) { c.arrived = v != 0.0; }},
162};
163
164// A component type, whatever it is: the functions that use the registry, compiled in the
165// caller's code (see bind_components()).
166struct Kind {
167 std::string_view name;
168 std::span<const std::string_view> fields;
169 FieldType (*type)(std::size_t field);
170 bool (*has)(const ecs::Registry& registry, ecs::Entity entity);
171 void (*add)(ecs::Registry& registry, ecs::Entity entity);
172 bool (*remove)(ecs::Registry& registry, ecs::Entity entity);
173 void (*entities)(ecs::Registry& registry, std::vector<ecs::Entity>& found);
174 double (*read)(ecs::Registry& registry, ecs::Entity entity, std::size_t field);
175 void (*write)(ecs::Registry& registry, ecs::Entity entity, std::size_t field, double value);
176};
177
178template <typename Component, const auto& Fields>
179constexpr Kind kind_of(std::string_view name) {
180 static constexpr auto names = [] {
181 std::array<std::string_view, Fields.size()> result{};
182 std::ranges::transform(Fields, result.begin(),
183 [](const auto& field) { return field.name; });
184 return result;
185 }();
186 return {
187 .name = name,
188 .fields = names,
189 .type = [](std::size_t field) { return Fields.at(field).type; },
190 .has = [](const ecs::Registry& registry,
191 ecs::Entity entity) { return registry.has<Component>(entity); },
192 .add =
193 [](ecs::Registry& registry, ecs::Entity entity) {
194 if (!registry.has<Component>(entity)) {
195 registry.emplace<Component>(entity);
196 }
197 },
198 .remove = [](ecs::Registry& registry,
199 ecs::Entity entity) { return registry.remove<Component>(entity); },
200 .entities =
201 [](ecs::Registry& registry, std::vector<ecs::Entity>& found) {
202 if (const auto pool = registry.find_pool<Component>()) {
203 const auto entities = pool->get().entities();
204 found.assign(entities.begin(), entities.end());
205 }
206 },
207 .read =
208 [](ecs::Registry& registry, ecs::Entity entity, std::size_t field) {
209 return Fields.at(field).get(registry.get<Component>(entity));
210 },
211 .write = [](ecs::Registry& registry, ecs::Entity entity, std::size_t field,
212 double value) { Fields.at(field).set(registry.get<Component>(entity), value); },
213 };
214}
215
216inline constexpr std::array kinds{
217 kind_of<ecs::Transform, transform_fields>("Transform"),
218 kind_of<ecs::Velocity, velocity_fields>("Velocity"),
219 kind_of<ecs::Sprite, sprite_fields>("Sprite"),
220 kind_of<ecs::Animation, animation_fields>("Animation"),
221 kind_of<ecs::Collider, collider_fields>("Collider"),
222 kind_of<ecs::Health, health_fields>("Health"),
223 kind_of<ecs::Damage, damage_fields>("Damage"),
224 kind_of<ecs::Score, score_fields>("Score"),
225 kind_of<ecs::SineMotion, sine_motion_fields>("SineMotion"),
226 kind_of<ecs::PathFollower, path_follower_fields>("PathFollower"),
227};
228
229inline constexpr std::array kind_names = [] {
230 std::array<std::string_view, kinds.size()> result{};
231 std::ranges::transform(kinds, result.begin(), [](const Kind& kind) { return kind.name; });
232 return result;
233}();
234
239AEGIS2D_SCRIPT_EXPORT void bind_kinds(ScriptState& lua, ecs::Registry& registry,
240 std::span<const Kind> kinds);
241
242} // namespace detail
243
278inline void bind_components(ScriptState& lua, ecs::Registry& registry) {
279 detail::bind_kinds(lua, registry, detail::kinds);
280}
281
285[[nodiscard]] inline std::span<const std::string_view> component_names() noexcept {
286 return detail::kind_names;
287}
288
292[[nodiscard]] inline std::span<const std::string_view>
293component_fields(std::string_view component) noexcept {
294 const auto found = std::ranges::find(detail::kinds, component, &detail::Kind::name);
295 return found != detail::kinds.end() ? found->fields : std::span<const std::string_view>{};
296}
297
298} // namespace aegis2d::script
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
Entry point of the ECS: creates entities and stores their components.
Definition registry.hpp:34
Component & get(Entity entity)
Component of an entity.
Definition registry.hpp:124
std::optional< std::reference_wrapper< SparseSet< Component > > > find_pool()
Pool of a component type, if it was ever used.
Definition registry.hpp:195
bool has(Entity entity) const
Tells whether an entity has every listed component.
Definition registry.hpp:114
Component & emplace(Entity entity, Args &&... args)
Gives an entity a component built in place.
Definition registry.hpp:76
bool remove(Entity entity)
Removes a component from an entity.
Definition registry.hpp:103
A Lua state for the game's scripts, sandboxed.
Definition state.hpp:52
Components shared by the client and the server.
Generational entity handle: an index into the component pools plus a generation.
constexpr std::array path_follower_fields
Definition components.hpp:151
FieldType
Definition components.hpp:25
constexpr std::array sine_motion_fields
Definition components.hpp:139
constexpr std::array score_fields
Definition components.hpp:134
constexpr std::array velocity_fields
Definition components.hpp:51
constexpr std::array health_fields
Definition components.hpp:121
constexpr std::array transform_fields
Definition components.hpp:43
constexpr std::array sprite_fields
Definition components.hpp:59
constexpr std::array kinds
Definition components.hpp:216
constexpr Kind kind_of(std::string_view name)
Definition components.hpp:179
constexpr std::array collider_fields
Definition components.hpp:105
constexpr std::array animation_fields
Definition components.hpp:88
constexpr std::array kind_names
Definition components.hpp:229
constexpr std::array damage_fields
Definition components.hpp:126
AEGIS2D_SCRIPT_EXPORT void bind_kinds(ScriptState &lua, ecs::Registry &registry, std::span< const Kind > kinds)
Gives the scripts of a state the components of a registry, through kinds.
Entity behaviors in Lua, so that new enemies and patterns need no recompilation.
Definition components.hpp:19
void bind_components(ScriptState &lua, ecs::Registry &registry)
Gives the scripts of a state the common components of a registry.
Definition components.hpp:278
AEGIS2D_SCRIPT_EXPORT std::string_view name() noexcept
Name of the module, used to check that it is linked.
std::span< const std::string_view > component_fields(std::string_view component) noexcept
Fields of a component, as scripts name them.
Definition components.hpp:293
std::span< const std::string_view > component_names() noexcept
Names of the components scripts can use.
Definition components.hpp:285
Path following: entities that walk along waypoints at a steady speed, and tell when they reach the en...
Registry: owns the entities and one component pool per component type.
A sandboxed Lua state: scripts run without files, processes nor debug access, and their errors never ...
Frames of the entity's Sprite shown in turn, at a steady rate.
Definition components.hpp:56
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
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
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
How much damage an entity can still take.
Definition components.hpp:115
std::int32_t points
Remaining health points.
Definition components.hpp:117
An entity that walks along waypoints, such as an enemy of a tower defense on the path found by NavGri...
Definition path_following.hpp:26
float progress
Distance walked from the first waypoint, in logical pixels.
Definition path_following.hpp:33
float speed
Speed along the path, in logical pixels per second.
Definition path_following.hpp:31
bool arrived
true once the last waypoint is reached; the entity stops there.
Definition path_following.hpp:35
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
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
Definition components.hpp:28
Getter get
Definition components.hpp:38
double(*)(const Component &) Getter
Definition components.hpp:29
FieldType type
Definition components.hpp:37
Setter set
Definition components.hpp:39
constexpr Field(std::string_view field_name, FieldType field_type, Getter getter, Setter setter) noexcept
Definition components.hpp:32
void(*)(Component &, double) Setter
Definition components.hpp:30
std::string_view name
Definition components.hpp:36
Definition components.hpp:166
void(* add)(ecs::Registry &registry, ecs::Entity entity)
Definition components.hpp:171
bool(* remove)(ecs::Registry &registry, ecs::Entity entity)
Definition components.hpp:172
FieldType(* type)(std::size_t field)
Definition components.hpp:169
double(* read)(ecs::Registry &registry, ecs::Entity entity, std::size_t field)
Definition components.hpp:174
bool(* has)(const ecs::Registry &registry, ecs::Entity entity)
Definition components.hpp:170
std::string_view name
Definition components.hpp:167
void(* entities)(ecs::Registry &registry, std::vector< ecs::Entity > &found)
Definition components.hpp:173
void(* write)(ecs::Registry &registry, ecs::Entity entity, std::size_t field, double value)
Definition components.hpp:175
std::span< const std::string_view > fields
Definition components.hpp:168