Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
events.hpp
Go to the documentation of this file.
1
16#pragma once
18#include <aegis2d/core/math.hpp>
22#include <aegis2d/ecs/lives.hpp>
24
25#include <span>
26
27namespace aegis2d::ecs {
28
31struct Explosion {
38
40 friend bool operator==(const Explosion&, const Explosion&) = default;
41};
42
44namespace detail {
45
46inline Vec2 position_of(const Registry& registry, Entity entity) {
47 const auto transform = registry.try_get<Transform>(entity);
48 return transform ? transform->get().position : Vec2{};
49}
50
51} // namespace detail
53
62inline void publish_explosions(core::EventBus& events, const Registry& registry,
63 const DamageReport& report) {
64 for (const Kill& kill : report.kills) {
65 events.publish(Explosion{.entity = kill.victim,
66 .killer = kill.killer,
67 .position = detail::position_of(registry, kill.victim)});
68 }
69}
70
76inline void publish_deaths(core::EventBus& events, std::span<const LifeLost> lost) {
77 for (const LifeLost& death : lost) {
78 events.publish(death);
79 }
80}
81
82} // namespace aegis2d::ecs
83
84// Moved to the shmup module; the old names stay until the games use the new ones.
86
87namespace aegis2d::ecs {
89using shmup::ShotFired;
90} // namespace aegis2d::ecs
Lets systems react to events without knowing who sends them.
Definition event_bus.hpp:31
void publish(const Event &event)
Calls every handler of type Event with event, before returning.
Definition event_bus.hpp:66
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
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.
Typed publish/subscribe event bus with immediate dispatch.
Player deaths: lives, respawn and game over.
Vector and matrix types of the engine (GLM behind engine names).
Entity-Component-System used by client and server.
Definition animation.hpp:13
void publish_deaths(core::EventBus &events, std::span< const LifeLost > lost)
Publishes each player's death of the tick.
Definition events.hpp:76
void publish_explosions(core::EventBus &events, const Registry &registry, const DamageReport &report)
Publishes an Explosion for each kill of the tick, in the order of the kills.
Definition events.hpp:62
void publish_shots(core::EventBus &events, const ecs::Registry &registry, std::span< const ecs::Entity > shooters)
Publishes a ShotFired for each entity that fires during the tick.
Definition weapons.hpp:105
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.
Weapons: fire rate enforced by the server, for players and for enemies that fire on their own,...
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 blows up: an enemy shot down, or a player that loses a life.
Definition events.hpp:31
friend bool operator==(const Explosion &, const Explosion &)=default
Compares the events.
Vec2 position
Where the entity was hit (its Transform), the origin if it has none.
Definition events.hpp:37
Entity entity
Entity that was killed (Kill::victim).
Definition events.hpp:33
Entity killer
Attacker of the last hit (Kill::killer), e.g. the missile.
Definition events.hpp:35
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
Where an entity is.
Definition components.hpp:17
Vec2 position
Center of the entity.
Definition components.hpp:19