Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
damage.hpp
Go to the documentation of this file.
1
4#pragma once
9
10#include <algorithm>
11#include <cstddef>
12#include <cstdint>
13#include <span>
14#include <vector>
15
16namespace aegis2d::ecs {
17
19struct Hit {
25 std::int32_t amount = 0;
26
28 friend constexpr bool operator==(const Hit&, const Hit&) noexcept = default;
29};
30
32struct Kill {
37
39 friend constexpr bool operator==(const Kill&, const Kill&) noexcept = default;
40};
41
47 std::vector<Hit> hits;
49 std::vector<Kill> kills;
51 std::vector<Entity> consumed;
52
54 void clear() noexcept {
55 hits.clear();
56 kills.clear();
57 consumed.clear();
58 }
59};
60
62namespace detail {
63
64inline bool listed(const std::vector<Entity>& entities, Entity entity) {
65 return std::ranges::find(entities, entity) != entities.end();
66}
67
68inline bool killed(const std::vector<Kill>& kills, Entity entity) {
69 return std::ranges::find(kills, entity, &Kill::victim) != kills.end();
70}
71
72inline bool active(const DamageReport& report, Entity entity) {
73 return !listed(report.consumed, entity) && !killed(report.kills, entity);
74}
75
76inline void strike(Registry& registry, Entity attacker, Entity target, DamageReport& report) {
77 const auto damage = registry.try_get<Damage>(attacker);
78 if (!damage) {
79 return;
80 }
81 const Damage dealt = *damage;
82 if (auto health = registry.try_get<Health>(target)) {
83 Health& remaining = *health;
84 remaining.points -= dealt.amount;
85 report.hits.push_back(Hit{.attacker = attacker, .target = target, .amount = dealt.amount});
86 if (remaining.depleted()) {
87 report.kills.push_back(Kill{.victim = target, .killer = attacker});
88 }
89 }
90 if (dealt.consumed_on_hit) {
91 report.consumed.push_back(attacker);
92 }
93}
94
95} // namespace detail
97
118inline void apply_damage(Registry& registry, std::span<const Contact> contacts,
119 DamageReport& report) {
120 report.clear();
121 for (const Contact& contact : contacts) {
122 if (!registry.alive(contact.first) || !registry.alive(contact.second) ||
123 !detail::active(report, contact.first) || !detail::active(report, contact.second)) {
124 continue;
125 }
126 if (contact.first_hits_second) {
127 detail::strike(registry, contact.first, contact.second, report);
128 }
129 if (contact.second_hits_first) {
130 detail::strike(registry, contact.second, contact.first, report);
131 }
132 }
133}
134
141inline std::size_t remove_casualties(Registry& registry, const DamageReport& report) {
142 std::size_t destroyed = 0;
143 for (const Kill& kill : report.kills) {
144 const auto health = registry.try_get<Health>(kill.victim);
145 if (health && health->get().depleted() && registry.destroy(kill.victim)) {
146 ++destroyed;
147 }
148 }
149 for (const Entity entity : report.consumed) {
150 if (registry.destroy(entity)) {
151 ++destroyed;
152 }
153 }
154 return destroyed;
155}
156
157} // 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
bool destroy(Entity entity)
Destroys an entity and removes all its components.
bool alive(Entity entity) const noexcept
Tells whether a handle designates a living entity.
Collision detection: a uniform grid finds the entities close enough to collide (broad phase),...
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
std::size_t remove_casualties(Registry &registry, const DamageReport &report)
Destroys the entities that died or were consumed during the tick.
Definition damage.hpp:141
void apply_damage(Registry &registry, std::span< const Contact > contacts, DamageReport &report)
Applies the damage of a tick's contacts to the entities' health.
Definition damage.hpp:118
float remaining(const PathFollower &follower) noexcept
Distance a follower still has to walk.
Definition path_following.hpp:91
Registry: owns the entities and one component pool per component type.
Two entities whose boxes overlap and whose layers interact.
Definition collision.hpp:85
What apply_damage() did during one tick, in the order of the contacts.
Definition damage.hpp:45
std::vector< Hit > hits
Every hit that removed health points.
Definition damage.hpp:47
std::vector< Kill > kills
Entities whose health reached zero or less during the tick, once each.
Definition damage.hpp:49
void clear() noexcept
Empties the lists and keeps their memory.
Definition damage.hpp:54
std::vector< Entity > consumed
Attackers consumed by their hit (missiles), once each.
Definition damage.hpp:51
How much damage an entity can still take.
Definition components.hpp:115
One hit that removed health points.
Definition damage.hpp:19
Entity target
Entity that lost health points (its Health component).
Definition damage.hpp:23
friend constexpr bool operator==(const Hit &, const Hit &) noexcept=default
Compares the hits.
Entity attacker
Entity that dealt the damage (its Damage component).
Definition damage.hpp:21
std::int32_t amount
Points removed by the hit.
Definition damage.hpp:25
An entity whose health reached zero, and the hit that did it.
Definition damage.hpp:32
Entity victim
Entity with no health left.
Definition damage.hpp:34
friend constexpr bool operator==(const Kill &, const Kill &) noexcept=default
Compares the kills.
Entity killer
Attacker of the last hit, e.g. the missile, to credit its shooter.
Definition damage.hpp:36