Incorporate the systems registry to make it easier to add/remove systems.

This commit is contained in:
Zed A. Shaw 2026-05-25 12:00:25 -04:00
parent 54d0a41c52
commit 3d8572117c
5 changed files with 110 additions and 13 deletions

82
src/game/registry.hpp Normal file
View file

@ -0,0 +1,82 @@
#pragma once
#include <functional>
#include "game/components.hpp"
namespace System {
using MovingFunc = std::function<void(components::Position& move_to)>;
using CombatFunc = std::function<void(int attack_id)>;
using RenderFunc = std::function<void()>;
using UpdateFunc = std::function<void()>;
using UseItemFunc = std::function<void(const std::string& slot_name)>;
using PickupFunc = std::function<void()>;
struct Registry {
std::vector<MovingFunc> $moving;
std::vector<CombatFunc> $combat;
std::vector<RenderFunc> $render;
std::vector<UpdateFunc> $update;
std::vector<UseItemFunc> $use_item;
std::vector<PickupFunc> $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();
}
}
};
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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<int>(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<guecs::Entity>(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() {

View file

@ -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();