Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
events.hpp
Go to the documentation of this file.
1
3#pragma once
7#include <aegis2d/script/export.hpp>
9
10#include <array>
11#include <span>
12#include <string_view>
13
14namespace aegis2d::script {
15
17struct EventField {
19 std::string_view name;
21 double value = 0.0;
22};
23
35AEGIS2D_SCRIPT_EXPORT bool emit(ScriptState& lua, std::string_view name,
36 std::span<const EventField> fields = {});
37
55public:
59 EventBridge(ScriptState& lua, core::EventBus& bus) : bus_{bus} {
60 subscriptions_ = {
61 bus.subscribe<ecs::Explosion>([&lua](const ecs::Explosion& event) {
62 const std::array fields{
63 EventField{.name = "entity", .value = static_cast<double>(event.entity.id())},
64 EventField{.name = "killer", .value = static_cast<double>(event.killer.id())},
65 EventField{.name = "x", .value = event.position.x},
66 EventField{.name = "y", .value = event.position.y},
67 };
68 emit(lua, "Explosion", fields);
69 }),
70 bus.subscribe<ecs::LifeLost>([&lua](const ecs::LifeLost& event) {
71 const std::array fields{
72 EventField{.name = "player", .value = static_cast<double>(event.player.id())},
73 EventField{.name = "lives_left",
74 .value = static_cast<double>(event.lives_left)},
75 EventField{.name = "score", .value = static_cast<double>(event.score)},
76 };
77 emit(lua, "LifeLost", fields);
78 }),
79 };
80 }
81 EventBridge(const EventBridge&) = delete;
87 for (const core::SubscriptionId id : subscriptions_) {
88 bus_.unsubscribe(id);
89 }
90 }
91
92private:
93 core::EventBus& bus_;
94 std::array<core::SubscriptionId, 2> subscriptions_{};
95};
96
97} // namespace aegis2d::script
Lets systems react to events without knowing who sends them.
Definition event_bus.hpp:31
SubscriptionId subscribe(Handler< Event > handler)
Registers a handler for events of type Event.
Definition event_bus.hpp:41
bool unsubscribe(SubscriptionId id)
Removes a handler.
Definition event_bus.hpp:51
Sends the gameplay events of a bus to the scripts of a state, while it lives.
Definition events.hpp:54
EventBridge & operator=(EventBridge &&)=delete
EventBridge & operator=(const EventBridge &)=delete
EventBridge(const EventBridge &)=delete
EventBridge(ScriptState &lua, core::EventBus &bus)
Subscribes to the gameplay events of bus.
Definition events.hpp:59
~EventBridge()
Unsubscribes from the bus.
Definition events.hpp:86
EventBridge(EventBridge &&)=delete
A Lua state for the game's scripts, sandboxed.
Definition state.hpp:52
Gameplay events: explosions and deaths, published on the core event bus.
Typed publish/subscribe event bus with immediate dispatch.
Player deaths: lives, respawn and game over.
std::uint64_t SubscriptionId
Handle returned by EventBus::subscribe(), used to unsubscribe. Never 0.
Definition event_bus.hpp:19
Entity behaviors in Lua, so that new enemies and patterns need no recompilation.
Definition components.hpp:19
AEGIS2D_SCRIPT_EXPORT std::string_view name() noexcept
Name of the module, used to check that it is linked.
AEGIS2D_SCRIPT_EXPORT bool emit(ScriptState &lua, std::string_view name, std::span< const EventField > fields={})
Sends an event to the scripts of a state.
A sandboxed Lua state: scripts run without files, processes nor debug access, and their errors never ...
An entity whose health reached zero blows up: an enemy shot down, or a player that loses a life.
Definition events.hpp:31
A player who lost a life during the tick.
Definition lives.hpp:17
A field of an event sent to scripts.
Definition events.hpp:17
double value
Its value; a whole number (such as an entity's id) reaches Lua as an integer.
Definition events.hpp:21
std::string_view name
Its name in the event's table.
Definition events.hpp:19