8#include <aegis2d/script/export.hpp>
27template <
typename Component>
29 using Getter = double (*)(
const Component&);
30 using Setter = void (*)(Component&, double);
61 [](
const ecs::Sprite& c) {
return static_cast<double>(c.texture); },
64 [](
const ecs::Sprite& c) {
return double{c.size.x}; },
67 [](
const ecs::Sprite& c) {
return double{c.size.y}; },
70 [](
const ecs::Sprite& c) {
return static_cast<double>(c.frame); },
73 [](
const ecs::Sprite& c) {
return static_cast<double>(c.layer); },
76 [](
const ecs::Sprite& c) {
return double{c.tint.x}; },
79 [](
const ecs::Sprite& c) {
return double{c.tint.y}; },
82 [](
const ecs::Sprite& c) {
return double{c.tint.z}; },
85 [](
const ecs::Sprite& c) {
return double{c.tint.w}; },
91 [](
const ecs::Animation& c) {
return static_cast<double>(c.first); },
95 [](
const ecs::Animation& c) {
return static_cast<double>(c.count); },
114 [](
const ecs::Collider& c) {
return static_cast<double>(c.layer); },
118 [](
const ecs::Collider& c) {
return static_cast<double>(c.mask); },
123 [](
const ecs::Health& c) {
return static_cast<double>(c.points); },
128 [](
const ecs::Damage& c) {
return static_cast<double>(c.amount); },
131 [](
const ecs::Damage& c) {
return c.consumed_on_hit ? 1.0 : 0.0; },
136 [](
const ecs::Score& c) {
return static_cast<double>(c.points); },
168 std::span<const std::string_view>
fields;
178template <
typename Component, const auto& Fields>
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; });
189 .type = [](std::size_t field) {
return Fields.at(field).type; },
194 if (!registry.
has<Component>(entity)) {
195 registry.
emplace<Component>(entity);
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());
209 return Fields.at(field).get(registry.
get<Component>(entity));
212 double value) { Fields.at(field).set(registry.
get<Component>(entity), value); },
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"),
230 std::array<std::string_view,
kinds.size()> result{};
231 std::ranges::transform(
kinds, result.begin(), [](
const Kind& kind) { return kind.name; });
240 std::span<const Kind>
kinds);
292[[nodiscard]]
inline std::span<const std::string_view>
295 return found !=
detail::kinds.end() ? found->fields : std::span<const std::string_view>{};
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 ®istry, 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 ®istry)
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
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 ®istry, ecs::Entity entity)
Definition components.hpp:171
bool(* remove)(ecs::Registry ®istry, ecs::Entity entity)
Definition components.hpp:172
FieldType(* type)(std::size_t field)
Definition components.hpp:169
double(* read)(ecs::Registry ®istry, ecs::Entity entity, std::size_t field)
Definition components.hpp:174
bool(* has)(const ecs::Registry ®istry, ecs::Entity entity)
Definition components.hpp:170
std::string_view name
Definition components.hpp:167
void(* entities)(ecs::Registry ®istry, std::vector< ecs::Entity > &found)
Definition components.hpp:173
void(* write)(ecs::Registry ®istry, ecs::Entity entity, std::size_t field, double value)
Definition components.hpp:175
std::span< const std::string_view > fields
Definition components.hpp:168