Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
lives.hpp
Go to the documentation of this file.
1
3#pragma once
9
10#include <cstddef>
11#include <cstdint>
12#include <vector>
13
14namespace aegis2d::ecs {
15
17struct LifeLost {
21 std::uint32_t lives_left = 0;
24 std::uint32_t score = 0;
25
28 [[nodiscard]] constexpr bool out() const noexcept { return lives_left == 0; }
29
31 friend constexpr bool operator==(const LifeLost&, const LifeLost&) noexcept = default;
32};
33
48inline void handle_player_deaths(Registry& registry, const DamageReport& report,
49 std::vector<LifeLost>& lost) {
50 lost.clear();
51 for (const Kill& kill : report.kills) {
52 const auto lives = registry.try_get<Lives>(kill.victim);
53 if (!lives) {
54 continue;
55 }
56 Lives& player = *lives;
57 if (player.count > 0) {
58 --player.count;
59 }
60 if (player.count > 0) {
61 if (auto health = registry.try_get<Health>(kill.victim)) {
62 health->get().points = player.health;
63 }
64 if (auto transform = registry.try_get<Transform>(kill.victim)) {
65 transform->get().position = player.spawn;
66 }
67 if (auto velocity = registry.try_get<Velocity>(kill.victim)) {
68 velocity->get().value = Vec2{};
69 }
70 }
71 const auto score = registry.try_get<Score>(kill.victim);
72 lost.push_back(LifeLost{.player = kill.victim,
73 .lives_left = player.count,
74 .score = score ? score->get().points : 0U});
75 }
76}
77
83[[nodiscard]] inline std::size_t players_left(const Registry& registry) {
84 return registry.count<Lives>();
85}
86
93[[nodiscard]] inline bool game_over(const Registry& registry) {
94 return players_left(registry) == 0;
95}
96
97} // namespace aegis2d::ecs
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
std::optional< std::reference_wrapper< Component > > try_get(Entity entity)
Component of an entity, if it has one.
Definition registry.hpp:147
std::size_t count() const
Number of entities that have a component.
Definition registry.hpp:170
Damage on hit: turns collision contacts into lost health, kills and spent missiles.
Components shared by the client and the server.
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
std::size_t players_left(const Registry &registry)
Number of players still in the game.
Definition lives.hpp:83
bool game_over(const Registry &registry)
Tells whether every player is out.
Definition lives.hpp:93
void handle_player_deaths(Registry &registry, const DamageReport &report, std::vector< LifeLost > &lost)
Takes a life from each killed player and respawns those who have some left.
Definition lives.hpp:48
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Registry: owns the entities and one component pool per component type.
What apply_damage() did during one tick, in the order of the contacts.
Definition damage.hpp:45
std::vector< Kill > kills
Entities whose health reached zero or less during the tick, once each.
Definition damage.hpp:49
How much damage an entity can still take.
Definition components.hpp:115
An entity whose health reached zero, and the hit that did it.
Definition damage.hpp:32
A player who lost a life during the tick.
Definition lives.hpp:17
std::uint32_t score
The player's Score at that moment (0 without a Score), kept for the end screen since an out player is...
Definition lives.hpp:24
friend constexpr bool operator==(const LifeLost &, const LifeLost &) noexcept=default
Compares the events.
std::uint32_t lives_left
Lives left after this death; 0 means the player is out.
Definition lives.hpp:21
Entity player
The player.
Definition lives.hpp:19
constexpr bool out() const noexcept
Tells whether the player has no life left.
Definition lives.hpp:28
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
Points a player has earned.
Definition components.hpp:143
Where an entity is.
Definition components.hpp:17
How fast an entity moves.
Definition components.hpp:25