From 3d8572117c0d32082b51d3c00c0246cee0692731 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 25 May 2026 12:00:25 -0400 Subject: [PATCH] Incorporate the systems registry to make it easier to add/remove systems. --- src/game/registry.hpp | 82 +++++++++++++++++++++++++++++++++++++++++++ src/game/systems.cpp | 15 ++++++++ src/game/systems.hpp | 2 ++ src/gui/fsm.cpp | 22 +++++------- src/gui/fsm.hpp | 2 ++ 5 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 src/game/registry.hpp diff --git a/src/game/registry.hpp b/src/game/registry.hpp new file mode 100644 index 0000000..cf5b8ee --- /dev/null +++ b/src/game/registry.hpp @@ -0,0 +1,82 @@ +#pragma once + +#include +#include "game/components.hpp" + +namespace System { + using MovingFunc = std::function; + using CombatFunc = std::function; + using RenderFunc = std::function; + using UpdateFunc = std::function; + using UseItemFunc = std::function; + using PickupFunc = std::function; + + struct Registry { + std::vector $moving; + std::vector $combat; + std::vector $render; + std::vector $update; + std::vector $use_item; + std::vector $pickup; + + void addMoving(MovingFunc action) { + $moving.emplace_back(action); + } + + void addCombat(CombatFunc action) { + $combat.emplace_back(action); + } + + void addRender(RenderFunc action) { + $render.emplace_back(action); + } + + void addUpdate(UpdateFunc action) { + $update.emplace_back(action); + } + + void addUseItem(UseItemFunc action) { + $use_item.emplace_back(action); + } + + void addPickup(PickupFunc action) { + $pickup.emplace_back(action); + } + + void runMoving(components::Position move_to) { + for(auto func : $moving) { + func(move_to); + } + } + + void runCombat(int attack_id) { + for(auto func : $combat) { + func(attack_id); + } + } + + void runRender() { + for(auto func : $render) { + func(); + } + } + + void runUpdate() { + for(auto func : $update) { + func(); + } + } + + void runUseItem(const std::string& slot_name) { + for(auto func : $use_item) { + func(slot_name); + } + } + + void runPickup() { + for(auto func : $pickup) { + func(); + } + } + }; +} diff --git a/src/game/systems.cpp b/src/game/systems.cpp index a1dcf26..f0677da 100644 --- a/src/game/systems.cpp +++ b/src/game/systems.cpp @@ -682,3 +682,18 @@ void System::spawn_attack(World& world, int attack_id, DinkyECS::Entity enemy) { drop_item(effect_id); } + +void System::init(Registry& reg) { + reg.addRender(System::clear_attack); + reg.addUseItem(System::use_item); + reg.addMoving(System::move_player); + reg.addCombat(System::combat); + reg.addPickup(System::pickup); + reg.addUpdate(System::generate_paths); + reg.addUpdate(System::enemy_ai_initialize); + reg.addUpdate(System::enemy_pathing); + reg.addUpdate(System::motion); + reg.addUpdate(System::collision); + reg.addUpdate(System::lighting); + reg.addUpdate(System::death); +} diff --git a/src/game/systems.hpp b/src/game/systems.hpp index abfb5c0..5ed39d3 100644 --- a/src/game/systems.hpp +++ b/src/game/systems.hpp @@ -4,6 +4,7 @@ #include "game/map.hpp" #include "algos/spatialmap.hpp" #include "game/level.hpp" +#include "game/registry.hpp" namespace System { using namespace components; @@ -70,4 +71,5 @@ namespace System { void clear_attack(); void spawn_attack(World& world, int attack_id, DinkyECS::Entity enemy); + void init(Registry& reg); } diff --git a/src/gui/fsm.cpp b/src/gui/fsm.cpp index 84f05c3..cb83bfd 100644 --- a/src/gui/fsm.cpp +++ b/src/gui/fsm.cpp @@ -27,6 +27,8 @@ namespace gui { $window.setVerticalSyncEnabled(VSYNC); if(FRAME_LIMIT) $window.setFramerateLimit(FRAME_LIMIT); $window.setPosition({0,0}); + // BUG: I don't like this here + System::init($systems); } void FSM::event(Event ev, std::any data) { @@ -67,7 +69,7 @@ namespace gui { void FSM::MOVING(Event ) { // this should be an optional that returns a point if(auto move_to = $main_ui.play_move()) { - System::move_player(*move_to); + $systems.runMoving(*move_to); run_systems(); $main_ui.dirty(); state(State::IDLE); @@ -79,7 +81,7 @@ namespace gui { switch(ev) { case TICK: { dbc::log("!!!!!! FIX System::combat(0) doesn't use any weapons, only first"); - System::combat(0); + $systems.runCombat(0); run_systems(); state(State::IN_COMBAT); } break; @@ -88,7 +90,7 @@ namespace gui { break; case ATTACK: { int attack_id = std::any_cast(data); - System::combat(attack_id); + $systems.runCombat(attack_id); run_systems(); } break; default: @@ -193,7 +195,7 @@ namespace gui { auto gui_id = std::any_cast(data); auto& slot_name = $status_ui.$gui.name_for(gui_id); - System::use_item(slot_name); + $systems.runUseItem(slot_name); $status_ui.update(); } break; case MOUSE_CLICK: @@ -203,7 +205,7 @@ namespace gui { mouse_action({1 << guecs::ModBit::hover}); } break; case AIM_CLICK: - System::pickup(); + $systems.runPickup(); break; default: break; // ignore everything else @@ -418,7 +420,7 @@ namespace gui { $boss_scene->render($window); } else { // this clears any attack animations, like fire - System::clear_attack(); + $systems.runRender(); // BUG: this is the render for this class, and where I add an update draw_gui(); } @@ -427,13 +429,7 @@ namespace gui { } void FSM::run_systems() { - System::generate_paths(); - System::enemy_ai_initialize(); - System::enemy_pathing(); - System::motion(); - System::collision(); - System::lighting(); - System::death(); + $systems.runUpdate(); } bool FSM::active() { diff --git a/src/gui/fsm.hpp b/src/gui/fsm.hpp index cc69765..a57a4d0 100644 --- a/src/gui/fsm.hpp +++ b/src/gui/fsm.hpp @@ -14,6 +14,7 @@ #include "gui/dnd_loot.hpp" #include "storyboard/ui.hpp" #include "events.hpp" +#include "game/registry.hpp" namespace gui { enum class State { @@ -46,6 +47,7 @@ namespace gui { LootUI $loot_ui; gui::routing::Router $router; DNDLoot $dnd_loot; + System::Registry $systems; FSM();