64inline bool listed(
const std::vector<Entity>& entities, Entity entity) {
65 return std::ranges::find(entities, entity) != entities.end();
68inline bool killed(
const std::vector<Kill>& kills, Entity entity) {
69 return std::ranges::find(kills, entity, &
Kill::victim) != kills.end();
72inline bool active(
const DamageReport& report, Entity entity) {
73 return !listed(report.consumed, entity) && !killed(report.kills, entity);
76inline void strike(Registry& registry, Entity attacker, Entity target, DamageReport& report) {
77 const auto damage = registry.try_get<Damage>(attacker);
81 const Damage dealt = *damage;
82 if (
auto health = registry.try_get<Health>(target)) {
85 report.hits.push_back(Hit{.attacker = attacker, .target = target, .amount = dealt.amount});
87 report.kills.push_back(Kill{.victim = target, .killer = attacker});
90 if (dealt.consumed_on_hit) {
91 report.consumed.push_back(attacker);
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)) {
126 if (contact.first_hits_second) {
127 detail::strike(registry, contact.first, contact.second, report);
129 if (contact.second_hits_first) {
130 detail::strike(registry, contact.second, contact.first, report);
142 std::size_t destroyed = 0;
145 if (health && health->get().depleted() && registry.
destroy(kill.victim)) {
150 if (registry.
destroy(entity)) {
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 ®istry, const DamageReport &report)
Destroys the entities that died or were consumed during the tick.
Definition damage.hpp:141
void apply_damage(Registry ®istry, 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.
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