Refactored the FSM so that it uses a generic registry of systems to do what it needs.
This commit is contained in:
parent
cbff127b40
commit
e742b8772d
7 changed files with 150 additions and 118 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();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -474,14 +474,14 @@ bool System::inventory_occupied(Entity container_id, const std::string& name) {
|
|||
return inventory.has(name);
|
||||
}
|
||||
|
||||
bool System::use_item(const string& slot_name) {
|
||||
void System::use_item(const string& slot_name) {
|
||||
auto& level = GameDB::current_level();
|
||||
auto& world = *level.world;
|
||||
auto& inventory = world.get<inventory::Model>(level.player);
|
||||
auto& player_combat = world.get<Combat>(level.player);
|
||||
|
||||
if(player_combat.hp >= player_combat.max_hp) return false;
|
||||
if(!inventory.has(slot_name)) return false;
|
||||
if(player_combat.hp >= player_combat.max_hp) return;
|
||||
if(!inventory.has(slot_name)) return;
|
||||
|
||||
auto what = inventory.get(slot_name);
|
||||
|
||||
|
|
@ -498,10 +498,8 @@ bool System::use_item(const string& slot_name) {
|
|||
player_combat.hp));
|
||||
|
||||
world.remove<Curative>(what);
|
||||
return true;
|
||||
} else {
|
||||
dbc::log($F("no usable item at {}", what));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -541,3 +539,19 @@ void System::clear_attack() {
|
|||
|
||||
void System::spawn_attack(World& world, int attack_id, DinkyECS::Entity enemy) {
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@
|
|||
#include "algos/spatialmap.hpp"
|
||||
#include "game/level.hpp"
|
||||
#include "events.hpp"
|
||||
#include "game/registry.hpp"
|
||||
|
||||
namespace System {
|
||||
|
||||
using namespace components;
|
||||
using namespace DinkyECS;
|
||||
using std::string, matrix::Matrix;
|
||||
|
|
@ -32,15 +34,15 @@ namespace System {
|
|||
|
||||
void pickup();
|
||||
|
||||
// BUG: these might need to go somewhere else....
|
||||
bool place_in_container(Entity cont_id, const string& name, Entity world_entity);
|
||||
|
||||
void remove_from_container(Entity cont_id, const std::string& name);
|
||||
void remove_from_world(Entity entity);
|
||||
void inventory_swap(Entity container_id, const std::string& a_name, const std::string &b_name);
|
||||
bool inventory_occupied(Entity container_id, const std::string& name);
|
||||
|
||||
void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos);
|
||||
bool use_item(const std::string& slot_name);
|
||||
void use_item(const std::string& slot_name);
|
||||
|
||||
game::Event shortest_rotate(Point player_at, Point aiming_at, Point turning_to);
|
||||
|
||||
|
|
@ -67,4 +69,6 @@ namespace System {
|
|||
|
||||
void clear_attack();
|
||||
void spawn_attack(World& world, int attack_id, DinkyECS::Entity enemy);
|
||||
|
||||
void init(Registry& reg);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue