Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
score.hpp
Go to the documentation of this file.
1
3#pragma once
8
9#include <cstdint>
10#include <limits>
11#include <optional>
12#include <vector>
13
14namespace aegis2d::ecs {
15
17struct Award {
23 std::uint32_t points = 0;
24
26 friend constexpr bool operator==(const Award&, const Award&) noexcept = default;
27};
28
35[[nodiscard]] inline std::optional<Entity> shooter_of(const Registry& registry, Entity killer) {
36 Entity player = killer;
37 if (const auto owner = registry.try_get<Owner>(killer)) {
38 player = owner->get().entity;
39 }
40 if (!registry.alive(player) || !registry.has<Score>(player)) {
41 return std::nullopt;
42 }
43 return player;
44}
45
57inline void credit_kills(Registry& registry, const DamageReport& report,
58 std::vector<Award>& awards) {
59 awards.clear();
60 for (const Kill& kill : report.kills) {
61 const auto value = registry.try_get<ScoreValue>(kill.victim);
62 const auto player = shooter_of(registry, kill.killer);
63 if (!value || !player) {
64 continue;
65 }
66 auto& score = registry.get<Score>(*player);
67 const std::uint32_t room = std::numeric_limits<std::uint32_t>::max() - score.points;
68 const std::uint32_t points = value->get().points < room ? value->get().points : room;
69 score.points += points;
70 awards.push_back(Award{.player = *player, .victim = kill.victim, .points = points});
71 }
72}
73
74} // 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
Component & get(Entity entity)
Component of an entity.
Definition registry.hpp:124
std::optional< std::reference_wrapper< Component > > try_get(Entity entity)
Component of an entity, if it has one.
Definition registry.hpp:147
bool has(Entity entity) const
Tells whether an entity has every listed component.
Definition registry.hpp:114
bool alive(Entity entity) const noexcept
Tells whether a handle designates a living entity.
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.
Entity-Component-System used by client and server.
Definition animation.hpp:13
void credit_kills(Registry &registry, const DamageReport &report, std::vector< Award > &awards)
Adds the value of each killed entity to the score of the player who killed it.
Definition score.hpp:57
std::optional< Entity > shooter_of(const Registry &registry, Entity killer)
Player credited for a kill made by an entity.
Definition score.hpp:35
Registry: owns the entities and one component pool per component type.
Points given to a player for a kill.
Definition score.hpp:17
std::uint32_t points
Points added (less than the victim's value if the score was saturated).
Definition score.hpp:23
Entity victim
Entity that was killed.
Definition score.hpp:21
Entity player
Player whose Score increased.
Definition score.hpp:19
friend constexpr bool operator==(const Award &, const Award &) noexcept=default
Compares the awards.
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
An entity whose health reached zero, and the hit that did it.
Definition damage.hpp:32
Entity that created this one, e.g. the player who fired a missile.
Definition components.hpp:137
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