Incorporate the systems registry to make it easier to add/remove systems.
This commit is contained in:
parent
54d0a41c52
commit
3d8572117c
5 changed files with 110 additions and 13 deletions
82
src/game/registry.hpp
Normal file
82
src/game/registry.hpp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -682,3 +682,18 @@ void System::spawn_attack(World& world, int attack_id, DinkyECS::Entity enemy) {
|
||||||
|
|
||||||
drop_item(effect_id);
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include "game/map.hpp"
|
#include "game/map.hpp"
|
||||||
#include "algos/spatialmap.hpp"
|
#include "algos/spatialmap.hpp"
|
||||||
#include "game/level.hpp"
|
#include "game/level.hpp"
|
||||||
|
#include "game/registry.hpp"
|
||||||
|
|
||||||
namespace System {
|
namespace System {
|
||||||
using namespace components;
|
using namespace components;
|
||||||
|
|
@ -70,4 +71,5 @@ namespace System {
|
||||||
|
|
||||||
void clear_attack();
|
void clear_attack();
|
||||||
void spawn_attack(World& world, int attack_id, DinkyECS::Entity enemy);
|
void spawn_attack(World& world, int attack_id, DinkyECS::Entity enemy);
|
||||||
|
void init(Registry& reg);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ namespace gui {
|
||||||
$window.setVerticalSyncEnabled(VSYNC);
|
$window.setVerticalSyncEnabled(VSYNC);
|
||||||
if(FRAME_LIMIT) $window.setFramerateLimit(FRAME_LIMIT);
|
if(FRAME_LIMIT) $window.setFramerateLimit(FRAME_LIMIT);
|
||||||
$window.setPosition({0,0});
|
$window.setPosition({0,0});
|
||||||
|
// BUG: I don't like this here
|
||||||
|
System::init($systems);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::event(Event ev, std::any data) {
|
void FSM::event(Event ev, std::any data) {
|
||||||
|
|
@ -67,7 +69,7 @@ namespace gui {
|
||||||
void FSM::MOVING(Event ) {
|
void FSM::MOVING(Event ) {
|
||||||
// this should be an optional that returns a point
|
// this should be an optional that returns a point
|
||||||
if(auto move_to = $main_ui.play_move()) {
|
if(auto move_to = $main_ui.play_move()) {
|
||||||
System::move_player(*move_to);
|
$systems.runMoving(*move_to);
|
||||||
run_systems();
|
run_systems();
|
||||||
$main_ui.dirty();
|
$main_ui.dirty();
|
||||||
state(State::IDLE);
|
state(State::IDLE);
|
||||||
|
|
@ -79,7 +81,7 @@ namespace gui {
|
||||||
switch(ev) {
|
switch(ev) {
|
||||||
case TICK: {
|
case TICK: {
|
||||||
dbc::log("!!!!!! FIX System::combat(0) doesn't use any weapons, only first");
|
dbc::log("!!!!!! FIX System::combat(0) doesn't use any weapons, only first");
|
||||||
System::combat(0);
|
$systems.runCombat(0);
|
||||||
run_systems();
|
run_systems();
|
||||||
state(State::IN_COMBAT);
|
state(State::IN_COMBAT);
|
||||||
} break;
|
} break;
|
||||||
|
|
@ -88,7 +90,7 @@ namespace gui {
|
||||||
break;
|
break;
|
||||||
case ATTACK: {
|
case ATTACK: {
|
||||||
int attack_id = std::any_cast<int>(data);
|
int attack_id = std::any_cast<int>(data);
|
||||||
System::combat(attack_id);
|
$systems.runCombat(attack_id);
|
||||||
run_systems();
|
run_systems();
|
||||||
} break;
|
} break;
|
||||||
default:
|
default:
|
||||||
|
|
@ -193,7 +195,7 @@ namespace gui {
|
||||||
auto gui_id = std::any_cast<guecs::Entity>(data);
|
auto gui_id = std::any_cast<guecs::Entity>(data);
|
||||||
auto& slot_name = $status_ui.$gui.name_for(gui_id);
|
auto& slot_name = $status_ui.$gui.name_for(gui_id);
|
||||||
|
|
||||||
System::use_item(slot_name);
|
$systems.runUseItem(slot_name);
|
||||||
$status_ui.update();
|
$status_ui.update();
|
||||||
} break;
|
} break;
|
||||||
case MOUSE_CLICK:
|
case MOUSE_CLICK:
|
||||||
|
|
@ -203,7 +205,7 @@ namespace gui {
|
||||||
mouse_action({1 << guecs::ModBit::hover});
|
mouse_action({1 << guecs::ModBit::hover});
|
||||||
} break;
|
} break;
|
||||||
case AIM_CLICK:
|
case AIM_CLICK:
|
||||||
System::pickup();
|
$systems.runPickup();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break; // ignore everything else
|
break; // ignore everything else
|
||||||
|
|
@ -418,7 +420,7 @@ namespace gui {
|
||||||
$boss_scene->render($window);
|
$boss_scene->render($window);
|
||||||
} else {
|
} else {
|
||||||
// this clears any attack animations, like fire
|
// 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
|
// BUG: this is the render for this class, and where I add an update
|
||||||
draw_gui();
|
draw_gui();
|
||||||
}
|
}
|
||||||
|
|
@ -427,13 +429,7 @@ namespace gui {
|
||||||
}
|
}
|
||||||
|
|
||||||
void FSM::run_systems() {
|
void FSM::run_systems() {
|
||||||
System::generate_paths();
|
$systems.runUpdate();
|
||||||
System::enemy_ai_initialize();
|
|
||||||
System::enemy_pathing();
|
|
||||||
System::motion();
|
|
||||||
System::collision();
|
|
||||||
System::lighting();
|
|
||||||
System::death();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FSM::active() {
|
bool FSM::active() {
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
#include "gui/dnd_loot.hpp"
|
#include "gui/dnd_loot.hpp"
|
||||||
#include "storyboard/ui.hpp"
|
#include "storyboard/ui.hpp"
|
||||||
#include "events.hpp"
|
#include "events.hpp"
|
||||||
|
#include "game/registry.hpp"
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
enum class State {
|
enum class State {
|
||||||
|
|
@ -46,6 +47,7 @@ namespace gui {
|
||||||
LootUI $loot_ui;
|
LootUI $loot_ui;
|
||||||
gui::routing::Router $router;
|
gui::routing::Router $router;
|
||||||
DNDLoot $dnd_loot;
|
DNDLoot $dnd_loot;
|
||||||
|
System::Registry $systems;
|
||||||
|
|
||||||
FSM();
|
FSM();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue