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
|
|
@ -3,7 +3,9 @@
|
|||
#include "game/systems.hpp"
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
#include "game/registry.hpp"
|
||||
|
||||
using components::Position;
|
||||
|
||||
TEST_CASE("figure out best rotation direction", "[systems-rotate]") {
|
||||
Matrix map = matrix::make(3, 3);
|
||||
|
|
@ -35,104 +37,45 @@ TEST_CASE("figure out best rotation direction", "[systems-rotate]") {
|
|||
}
|
||||
}
|
||||
|
||||
using MovingFunc = std::function<void()>;
|
||||
using CombatFunc = std::function<void(bool)>;
|
||||
using RenderFunc = std::function<void()>;
|
||||
using UpdateFunc = std::function<void()>;
|
||||
using UseFunc = std::function<void()>;
|
||||
using PickupFunc = std::function<void()>;
|
||||
|
||||
struct Engine {
|
||||
std::vector<MovingFunc> $moving;
|
||||
std::vector<CombatFunc> $combat;
|
||||
std::vector<RenderFunc> $render;
|
||||
std::vector<UpdateFunc> $update;
|
||||
std::vector<UseFunc> $use;
|
||||
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 addUse(UseFunc action) {
|
||||
$use.emplace_back(action);
|
||||
}
|
||||
|
||||
void addPickup(PickupFunc action) {
|
||||
$pickup.emplace_back(action);
|
||||
}
|
||||
|
||||
void runMoving() {
|
||||
for(auto func : $moving) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
void runCombat(bool attr) {
|
||||
for(auto func : $combat) {
|
||||
func(attr);
|
||||
}
|
||||
}
|
||||
|
||||
void runRender() {
|
||||
for(auto func : $render) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
void runUpdate() {
|
||||
for(auto func : $update) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
void runUse() {
|
||||
for(auto func : $use) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
void runPickup() {
|
||||
for(auto func : $pickup) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void test_system_1() {
|
||||
fmt::println("TEST 1");
|
||||
void test_moving(Position& move_to) {
|
||||
fmt::println("MOVING: {},{}", move_to.location.x, move_to.location.y);
|
||||
}
|
||||
|
||||
void test_combat(bool what) {
|
||||
fmt::println("TEST 2: {}", what);
|
||||
void test_combat(int attack_id) {
|
||||
fmt::println("ATTACK: {}", attack_id);
|
||||
}
|
||||
|
||||
void test_render() {
|
||||
fmt::println("RENDER");
|
||||
}
|
||||
|
||||
void test_update() {
|
||||
fmt::println("UPDATE");
|
||||
}
|
||||
|
||||
void test_use_item(const std::string& slot_name) {
|
||||
fmt::println("USE ITEM {}", slot_name);
|
||||
}
|
||||
|
||||
void test_pickup() {
|
||||
fmt::println("PICKUP");
|
||||
}
|
||||
|
||||
TEST_CASE("new system running engine thing", "[systems-engine]") {
|
||||
Engine systems;
|
||||
System::Registry systems;
|
||||
|
||||
systems.addMoving(test_system_1);
|
||||
systems.addMoving(test_moving);
|
||||
systems.addCombat(test_combat);
|
||||
systems.addRender(test_system_1);
|
||||
systems.addUpdate(test_system_1);
|
||||
systems.addUse(test_system_1);
|
||||
systems.addPickup(test_system_1);
|
||||
systems.addRender(test_render);
|
||||
systems.addUpdate(test_update);
|
||||
systems.addUseItem(test_use_item);
|
||||
systems.addPickup(test_pickup);
|
||||
|
||||
systems.runCombat(false);
|
||||
systems.runMoving();
|
||||
systems.runCombat(1);
|
||||
systems.runMoving({1,1});
|
||||
systems.runRender();
|
||||
systems.runUpdate();
|
||||
systems.runUse();
|
||||
systems.runUseItem("hand_l");
|
||||
systems.runPickup();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue