|
Aegis2D
Modular C++26 2D game engine for networked games
|
Entity-Component-System used by client and server. More...
Namespaces | |
| namespace | detail |
Classes | |
| struct | Aabb |
| Axis-aligned box given by its two corners, in logical pixels. More... | |
| struct | Animation |
| Frames of the entity's Sprite shown in turn, at a steady rate. More... | |
| struct | Award |
| Points given to a player for a kill. More... | |
| struct | Cell |
| Column and row of a grid cell, from the top-left one. More... | |
| struct | Collider |
| Axis-aligned box an entity collides with. More... | |
| class | CollisionGrid |
| Broad phase of collision detection: a uniform grid over the play area. More... | |
| struct | CollisionPair |
| Two entities that may collide, the one with the smaller handle first. More... | |
| struct | Contact |
| Two entities whose boxes overlap and whose layers interact. More... | |
| struct | Damage |
| How much damage an entity deals to what it hits. More... | |
| struct | DamageReport |
| What apply_damage() did during one tick, in the order of the contacts. More... | |
| struct | DestroyAfterAnimation |
| Entity destroyed once its Animation has played to the end: a visual effect that lasts as long as its clip, such as an explosion. More... | |
| class | Entity |
| Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation. More... | |
| class | EntityAllocator |
| Hands out entity handles and recycles the indices of destroyed entities. More... | |
| struct | Explosion |
| An entity whose health reached zero blows up: an enemy shot down, or a player that loses a life. More... | |
| class | FunctionSystem |
| System made of a function, for logic that keeps no state of its own. More... | |
| struct | Health |
| How much damage an entity can still take. More... | |
| struct | Hit |
| One hit that removed health points. More... | |
| class | ISystem |
| Game logic run on the registry once per tick (movement, collisions, spawns...). More... | |
| struct | Kill |
| An entity whose health reached zero, and the hit that did it. More... | |
| struct | LifeLost |
| A player who lost a life during the tick. More... | |
| struct | Lives |
| Lives of a player, and how it comes back after losing one. More... | |
| class | NavGrid |
| Grid of cells that can be walked or not, each with a cost, and the shortest path between two of them. More... | |
| struct | NavPath |
| Path between two cells, found again only when the grid has changed. More... | |
| struct | NetworkId |
| Identity of an entity shared by the server and the clients. More... | |
| struct | OffscreenDespawn |
| Entity destroyed once it has left the play area for good. More... | |
| struct | Owner |
| Entity that created this one, e.g. the player who fired a missile. More... | |
| struct | PathEnded |
| An entity that reached the last waypoint of its path. More... | |
| struct | PathFollower |
| An entity that walks along waypoints, such as an enemy of a tower defense on the path found by NavGrid::find_path(). More... | |
| struct | PlayerInput |
| Actions a player holds during the tick, as received from its client. More... | |
| class | PluginContext |
| What a plugin can add to the game, given to its start function. More... | |
| class | Plugins |
| The plugins a game loaded, kept loaded as long as it lives. More... | |
| class | RandomSpawner |
| Decides when and where entities spawn, the same way for the same seed. More... | |
| class | Registry |
| Entry point of the ECS: creates entities and stores their components. More... | |
| class | Scheduler |
| Ordered list of named systems, all updated once per tick. More... | |
| struct | Score |
| Points a player has earned. More... | |
| struct | ScoreValue |
| Points earned by the player who destroys this entity. More... | |
| struct | SineMotion |
| Sine wave added to an entity's movement, like an enemy weaving up and down. More... | |
| class | SparseSet |
| Pool of one component type, stored in a sparse set. More... | |
| class | SparseSetBase |
| Type-erased part of a sparse set: which entities are in the pool, and where. More... | |
| struct | SpawnSettings |
| Where and how often a RandomSpawner makes entities appear. More... | |
| struct | Sprite |
| What an entity looks like. More... | |
| struct | Tick |
| One simulation step, as the systems see it. More... | |
| struct | Timer |
Fires every period seconds (a tower that shoots, a wave every 30 s), or once after a delay (a bomb, a message that goes away). More... | |
| struct | Transform |
| Where an entity is. More... | |
| struct | Velocity |
| How fast an entity moves. More... | |
| class | View |
Visits every entity that owns all of Components, with those components. More... | |
| class | WaveSchedule |
| Enemy waves from a data file: which entities appear, where and when. More... | |
| struct | WaveSpawn |
| One entity a WaveSchedule makes appear. More... | |
Enumerations | |
| enum class | Action : std::uint8_t { Up = 1U << 0U , Down = 1U << 1U , Left = 1U << 2U , Right = 1U << 3U , Fire = 1U << 4U } |
| Actions a player can hold, one bit each in PlayerInput. More... | |
| enum class | Painted : std::uint8_t { Walkable , Blocked } |
| What the painted cells of a tile layer are, for NavGrid::mark_tiles(). More... | |
| enum class | TargetOrder : std::uint8_t { Nearest , MostAdvanced } |
| Which targets come first in find_targets(). More... | |
Functions | |
| std::uint32_t | advance (Animation &animation, float seconds) noexcept |
| Moves an animation forward and tells which frame it shows. | |
| void | animate_sprites (Registry ®istry, const Tick &tick) |
| Advances every animation by one tick and shows its frame. | |
| void | destroy_finished_animations (Registry ®istry, std::vector< Entity > &destroyed) |
| Destroys the effects whose animation has played to the end. | |
| constexpr bool | hits (const Collider &attacker, const Collider &target) noexcept |
| Tells whether a collider can hit another one, according to their layers. | |
| void | find_contacts (const Registry ®istry, std::span< const CollisionPair > candidates, std::vector< Contact > &out) |
| Narrow phase: keeps the candidate pairs whose layers interact and boxes overlap. | |
| std::vector< Contact > | find_contacts (const Registry ®istry, const CollisionGrid &grid) |
| Broad and narrow phases on a grid built for this tick. | |
| void | apply_damage (Registry ®istry, std::span< const Contact > contacts, DamageReport &report) |
| Applies the damage of a tick's contacts to the entities' health. | |
| std::size_t | remove_casualties (Registry ®istry, const DamageReport &report) |
| Destroys the entities that died or were consumed during the tick. | |
| bool | left_for_good (Vec2 position, Vec2 half, Vec2 velocity, const Aabb &area) noexcept |
| Tells whether a moving box has left an area and will not come back. | |
| void | despawn_outside (Registry ®istry, const Aabb &area, std::vector< Entity > &despawned) |
| Destroys the entities that have left the play area for good. | |
| std::string | to_string (Entity entity) |
Text form of a handle, for logs: Entity(3, 1) (index, generation) or Entity(null). | |
| void | publish_explosions (core::EventBus &events, const Registry ®istry, const DamageReport &report) |
| Publishes an Explosion for each kill of the tick, in the order of the kills. | |
| void | publish_deaths (core::EventBus &events, std::span< const LifeLost > lost) |
| Publishes each player's death of the tick. | |
| void | handle_player_deaths (Registry ®istry, const DamageReport &report, std::vector< LifeLost > &lost) |
| Takes a life from each killed player and respawns those who have some left. | |
| std::size_t | players_left (const Registry ®istry) |
| Number of players still in the game. | |
| bool | game_over (const Registry ®istry) |
| Tells whether every player is out. | |
| std::string_view | name () noexcept |
| Name of the module, used to check that it is linked. | |
| Vec2 | direction_of (const PlayerInput &input) noexcept |
| Direction a player's input points to. | |
| void | apply_velocity (Registry ®istry, const Tick &tick) |
| Moves every entity by its velocity for one tick. | |
| Vec2 | sine_offset (const SineMotion &motion, float time) noexcept |
| Offset of a sine motion from its path at a time. | |
| void | apply_sine_motion (Registry ®istry, const Tick &tick) |
| Moves every entity with a sine motion along its wave for one tick. | |
| Vec2 | half_extent_of (const Registry ®istry, Entity entity) |
| Half size of what must stay visible of an entity. | |
| Vec2 | clamp_into (Vec2 position, Vec2 half, const Aabb &area) noexcept |
| Keeps a position inside an area, with a margin on each side. | |
| float | path_length (std::span< const Vec2 > waypoints) noexcept |
| Length of a path. | |
| Vec2 | point_along (std::span< const Vec2 > waypoints, float distance) noexcept |
| Point at a distance along a path. | |
| float | remaining (const PathFollower &follower) noexcept |
| Distance a follower still has to walk. | |
| void | follow_paths (Registry ®istry, const Tick &tick, std::vector< Entity > &arrived) |
| Moves every PathFollower along its path for one tick and lists those that arrived. | |
| void | publish_path_ends (core::EventBus &events, const Registry ®istry, std::span< const Entity > arrived) |
| Publishes a PathEnded for each entity that arrived this tick. | |
| std::optional< Entity > | shooter_of (const Registry ®istry, Entity killer) |
| Player credited for a kill made by an entity. | |
| void | credit_kills (Registry ®istry, const DamageReport &report, std::vector< Award > &awards) |
| Adds the value of each killed entity to the score of the player who killed it. | |
| template<typename Component > | |
| void | find_targets (const Registry ®istry, const CollisionGrid &grid, Vec2 center, float radius, TargetOrder order, std::vector< Entity > &out) |
| Lists the entities with a component whose center is within a radius of a point, in the order asked. | |
| std::uint32_t | advance (Timer &timer, float seconds) noexcept |
| Moves a timer forward and tells how many times it fires. | |
| void | fire_timers (Registry ®istry, const Tick &tick, std::vector< Entity > &fired) |
| Advances every Timer by one tick and lists the entities whose timer fired. | |
Variables | |
| constexpr std::string_view | plugin_start = "aegis2d_plugin_start" |
| Name of the C function a plugin exports, called once when it is loaded. | |
Entity-Component-System used by client and server.
|
strong |
Actions a player can hold, one bit each in PlayerInput.
| Enumerator | |
|---|---|
| Up | Move up (towards y = 0). |
| Down | Move down. |
| Left | Move left. |
| Right | Move right. |
| Fire | Fire. |
|
strong |
What the painted cells of a tile layer are, for NavGrid::mark_tiles().
| Enumerator | |
|---|---|
| Walkable | Painted cells can be walked, empty ones cannot (a layer that draws the path). |
| Blocked | Painted cells are blocked, empty ones can be walked (a layer of walls). |
|
strong |
Which targets come first in find_targets().
| Enumerator | |
|---|---|
| Nearest | The closest to the point first. |
| MostAdvanced | The furthest along its path first: the least PathFollower remaining(), then the closest; entities that follow no path come last. |
|
inlinenoexcept |
Moves an animation forward and tells which frame it shows.
A looping clip wraps elapsed into [0, duration()), so the time never grows without bound; a clip that does not loop stops at its end.
| animation | Animation to advance. |
| seconds | Time spent, such as tick.delta(); negative values count as 0. |
|
inlinenoexcept |
Moves a timer forward and tells how many times it fires.
A repeating timer fires once per period elapsed, so a long tick (a stall, a slow machine) does not lose fires; a period of 0 or less fires once per call. A paused or done timer does not move.
| timer | Timer to advance. |
| seconds | Time spent, such as tick.delta(); negative values count as 0. |
Advances every animation by one tick and shows its frame.
For each entity with Animation and Sprite, advance() moves the clip forward by tick.delta() and Sprite::frame becomes its frame. Run it once per tick, e.g. systems.add("animation", ecs::animate_sprites);; the frames then follow the time spent, not the tick or frame rate. Defined in the header because it reads components (see CollisionGrid::build()).
| registry | World holding the sprites. |
| tick | Step of the simulation. |
|
inline |
Applies the damage of a tick's contacts to the entities' health.
For each contact, in order, and each direction in which one entity hits the other (Contact::first_hits_second, Contact::second_hits_first), an attacker with a Damage component removes Damage::amount points from a target with a Health component. Then:
Damage::consumed_on_hit is consumed, even by a target without Health (an indestructible wall);Nothing is destroyed here: the game reads the report first (score, explosions, lives), then calls remove_casualties(). Contacts whose entities were destroyed are skipped. Defined in the header, like the collision functions, because it reads components.
| registry | World holding the Damage and Health components. |
| contacts | Contacts of the tick, usually from find_contacts(). |
| report | Cleared, then filled with what happened. |
Moves every entity with a sine motion along its wave for one tick.
For each entity with SineMotion and Transform, advances elapsed by tick.delta() and adds the change of sine_offset() to the position. Only the change is added, so the wave stacks with apply_velocity() and with anything else that moves the entity, and the offset reached depends only on the time spent, not on the tick rate: starting from elapsed = 0, the entity is sine_offset(elapsed) away from where it would be without the wave. Run it next to apply_velocity(), e.g. systems.add("sine_motion", ecs::apply_sine_motion);. Defined in the header because it reads components (see CollisionGrid::build()).
| registry | World holding the entities. |
| tick | Step of the simulation. |
Moves every entity by its velocity for one tick.
Adds velocity * tick.delta() to the position of each entity with Transform and Velocity (players, missiles, enemies), so speeds are per second whatever the tick rate. Defined in the header because it reads components.
| registry | World holding the entities. |
| tick | Step of the simulation. |
Keeps a position inside an area, with a margin on each side.
An area narrower than twice the margin keeps the position at its center on that axis.
| position | Center of the entity. |
| half | Margin: half the entity's size. |
| area | Allowed area. |
|
inline |
Adds the value of each killed entity to the score of the player who killed it.
For each Kill of the report whose victim has a ScoreValue, the player found by shooter_of() gets the points; kills without a value (a player, a wall) or without a player to credit give nothing. Call it after apply_damage() and before remove_casualties(), while the victims still exist. Scores stop at the largest std::uint32_t instead of wrapping. Defined in the header because it reads components (see CollisionGrid::build()).
| registry | World holding the scores. |
| report | Report of apply_damage() for this tick. |
| awards | Cleared, then filled with one Award per credited kill, in the order of the kills, for the HUD or the network. |
|
inline |
Destroys the entities that have left the play area for good.
Each entity with OffscreenDespawn and Transform is destroyed when left_for_good() holds for its box (see half_extent_of(): its Sprite, else its Collider, else its center), its Velocity (none counts as not moving) and area grown by its margin. Run it once per tick after the movement, e.g. last: systems.add("despawn", [area, &gone](ecs::Registry& r, const ecs::Tick&) { ecs::despawn_outside(r, area, gone); });. Defined in the header because it reads components (see CollisionGrid::build()).
| registry | World holding the entities. |
| area | Play area, usually {0, 0} to the logical resolution. |
| despawned | Cleared, then filled with the destroyed entities, so the server can tell the clients. |
|
inline |
Destroys the effects whose animation has played to the end.
Each entity with DestroyAfterAnimation and an Animation that does not loop is destroyed once Animation::finished(), so a one-shot effect such as an explosion goes away after its last frame; a looping animation is never finished, and the entity stays. Run it after animate_sprites(), e.g. systems.add("effects", [&gone](ecs::Registry& r, const ecs::Tick&) { ecs::destroy_finished_animations(r, gone); });. Defined in the header because it reads components (see CollisionGrid::build()).
| registry | World holding the effects. |
| destroyed | Cleared, then filled with the destroyed entities. |
|
inlinenoexcept |
Direction a player's input points to.
Up and down, left and right cancel each other. A diagonal is normalized, so a player moves at the same speed in all eight directions.
| input | Held actions. |
|
inline |
Broad and narrow phases on a grid built for this tick.
| registry | World holding the entities' components. |
| grid | Grid filled with CollisionGrid::build() (or insert()). |
|
inline |
Narrow phase: keeps the candidate pairs whose layers interact and boxes overlap.
For each pair, the layers are checked first (no contact if neither entity can hit the other, so enemies ignore enemies), then the boxes built from each entity's Transform and Collider. Pairs where an entity was destroyed or lost one of these components since the grid was built are skipped. Defined in the header for the same reason as CollisionGrid::build().
| registry | World holding the entities' components. |
| candidates | Pairs from the broad phase, usually CollisionGrid::pairs(). |
| out | Cleared, then filled with the contacts, in the order of candidates. |
| void aegis2d::ecs::find_targets | ( | const Registry & | registry, |
| const CollisionGrid & | grid, | ||
| Vec2 | center, | ||
| float | radius, | ||
| TargetOrder | order, | ||
| std::vector< Entity > & | out | ||
| ) |
Lists the entities with a component whose center is within a radius of a point, in the order asked.
The candidates come from CollisionGrid::query() over the square around the circle, so only the entities near the point are tested, not every entity of the world; the grid must have been built this tick (grid.build(registry)), and only entities with a Collider are in it. An entity is in range when the distance from center to its Transform is at most radius. Equal entities come in handle order, so the choice is the same on every run. A tower fires at the first one:
Defined in the header because it reads components (see CollisionGrid::build()).
| Component | Component the targets have, such as the game's Enemy. |
| registry | World the grid was built from. |
| grid | Grid built this tick. |
| center | Point to measure from, such as a tower's position. |
| radius | Range, in logical pixels; a negative or NaN radius finds nothing. |
| order | Which targets come first. |
| out | Cleared, then filled with the targets in range, the best one first. |
|
inline |
Advances every Timer by one tick and lists the entities whose timer fired.
An entity is listed once per fire, in the order of the timers, so a timer that fires twice during a long tick is listed twice. The game then does what each timer is for (fires a tower, sends a wave). Run it once per tick, e.g. systems.add("timers", [&fired](ecs::Registry& r, const ecs::Tick& t) { ecs::fire_timers(r, t, fired); });. Defined in the header because it reads components (see CollisionGrid::build()).
| registry | World holding the timers. |
| tick | Step of the simulation. |
| fired | Cleared, then filled with the entities whose timer fired this tick. |
|
inline |
Moves every PathFollower along its path for one tick and lists those that arrived.
An entity is listed once, on the tick it reaches its last waypoint; a path of one waypoint is reached on the first tick, an empty one is ignored. To send an entity on a new path (found again from where it is), set the waypoints from its position, then progress = 0 and arrived = false. Defined in the header because it reads components (see CollisionGrid::build()).
| registry | World holding the followers and their Transform. |
| tick | Step of the simulation. |
| arrived | Cleared, then filled with the entities that arrived this tick. |
|
inline |
Tells whether every player is out.
True as soon as no entity has Lives: check it only once the players have joined, after remove_casualties(). The server then tells every client the game is over (#115) and stops the simulation.
| registry | World holding the players. |
true when players_left() is 0. Half size of what must stay visible of an entity.
The Sprite if the entity has one (the whole sprite stays on screen), otherwise its Collider, otherwise nothing (the center alone is kept in the area).
| registry | World holding the entity. |
| entity | Entity to measure. |
|
inline |
Takes a life from each killed player and respawns those who have some left.
For each Kill of the report whose victim has Lives, one life is taken. While lives remain, the player is revived: Health back to Lives::health, Transform moved to Lives::spawn and Velocity set to zero (when it has them), so remove_casualties() keeps it. A player with no life left keeps its depleted health and is destroyed by remove_casualties(), like any casualty. Kills of entities without Lives are ignored.
Order of a tick: apply_damage(), credit_kills(), handle_player_deaths(), then remove_casualties(). Defined in the header because it reads components (see CollisionGrid::build()).
| registry | World holding the players. |
| report | Report of apply_damage() for this tick. |
| lost | Cleared, then filled with one event per player who lost a life, in the order of the kills. |
|
constexprnoexcept |
Tells whether a collider can hit another one, according to their layers.
attacker hits target when its mask contains one of the target's layers. The test is one-way: a player missile (mask: enemies) hits an enemy even if the enemy's mask does not contain player missiles.
true if attacker.mask & target.layer is not 0.
|
inlinenoexcept |
Tells whether a moving box has left an area and will not come back.
The box is gone when, on one axis, it lies entirely beyond an edge of the area (touching the edge from outside counts as beyond) and its velocity on that axis does not point back towards the area: moving away or not moving. A box outside but heading in, like an enemy entering from the right, is not gone.
| position | Center of the box. |
| half | Half its width and height. |
| velocity | Velocity of the entity, in logical pixels per second. |
| area | Area the entity lives in. |
true if the box will never overlap area again at this velocity.
|
noexcept |
Name of the module, used to check that it is linked.
"ecs", a view of a static string.
|
inlinenoexcept |
Length of a path.
| waypoints | Points of the path, in order. |
|
inline |
Number of players still in the game.
Counts the entities with Lives, so call it after remove_casualties(), once out players are destroyed.
| registry | World holding the players. |
Point at a distance along a path.
| waypoints | Points of the path, in order. |
| distance | Distance from the first point; clamped to the path. |
|
inline |
Publishes each player's death of the tick.
Call it after handle_player_deaths(), with the events it listed; LifeLost::out() tells the player has no life left.
| events | Bus to publish on. |
| lost | Players who lost a life this tick. |
|
inline |
Publishes an Explosion for each kill of the tick, in the order of the kills.
Every entity whose health reached zero explodes, players included. Call it right after apply_damage(): handle_player_deaths() moves a revived player back to its spawn point, and remove_casualties() destroys the dead, so the explosion would no longer be where the entity was hit.
| events | Bus to publish on. |
| registry | World holding the casualties, not yet removed. |
| report | Report of apply_damage() for this tick. |
|
inline |
Publishes a PathEnded for each entity that arrived this tick.
Call it after follow_paths(), with the entities it listed, so the game (a life lost when an enemy reaches the exit) and the sounds react to it.
| events | Bus to publish on. |
| registry | World holding the followers. |
| arrived | Entities listed by follow_paths(). |
|
inlinenoexcept |
Distance a follower still has to walk.
| follower | Follower to measure. |
|
inline |
Destroys the entities that died or were consumed during the tick.
A killed entity whose health is above zero again is kept: the game revived it (a player who still has lives). Entities already destroyed are ignored.
| registry | World to update. |
| report | Report of apply_damage() for this tick. |
|
inline |
Player credited for a kill made by an entity.
The killer's Owner if it has one (a missile and its shooter), otherwise the killer itself (a player that rammed an enemy). The result must be alive and have a Score.
| registry | World holding the Owner and Score components. |
| killer | Attacker of the killing hit (Kill::killer). |
std::nullopt.
|
inlinenoexcept |
Offset of a sine motion from its path at a time.
| motion | Amplitude and period of the wave. |
| time | Seconds into the wave. |
amplitude * sin(2π * time / period), or zero when the period is zero or less.
|
inline |
Text form of a handle, for logs: Entity(3, 1) (index, generation) or Entity(null).
A free function rather than a std::formatter: libc++ 18 rejects user formatters at compile time; log with log_info("spawned {}", to_string(entity)).
| entity | Handle to write. |
|
inlineconstexpr |
Name of the C function a plugin exports, called once when it is loaded.