Refactored boss fight to pull out the FSM code into boss::Fight.

This commit is contained in:
Zed A. Shaw 2025-09-29 13:19:59 -04:00
parent 3b11ed0a75
commit 61a40ae7cd
11 changed files with 127 additions and 94 deletions

55
boss/fight.cpp Normal file
View file

@ -0,0 +1,55 @@
#include "boss/fight.hpp"
namespace boss {
Fight::Fight(shared_ptr<World> world, Entity boss_id) :
$world(world),
$boss_id(boss_id),
$ui(world, boss_id)
{
$ui.init();
}
bool Fight::event(gui::Event ev, std::any data) {
switch($state) {
FSM_STATE(State, START, ev, data);
FSM_STATE(State, END, ev, data);
}
return in_state(State::END);
}
void Fight::START(gui::Event ev, std::any data) {
using enum gui::Event;
switch(ev) {
// this is only if using the debug X key to skip it
case BOSS_START:
state(State::END);
break;
case MOUSE_CLICK: {
$ui.mouse(mouse_pos.x, mouse_pos.y, guecs::NO_MODS);
if($ui.boss_dead()) {
state(State::END);
}
} break;
case ATTACK: {
int attack_id = std::any_cast<int>(data);
$ui.attack(attack_id);
} break;
case MOUSE_MOVE: {
$ui.mouse(mouse_pos.x, mouse_pos.y, {1 << guecs::ModBit::hover});
} break;
case TICK:
$ui.run_systems();
break;
default:
fmt::println("BOSS_FIGHT unknown event {}", (int)ev);
}
}
void Fight::END(gui::Event ev, std::any data) {
(void)ev;
(void)data;
}
}

36
boss/fight.hpp Normal file
View file

@ -0,0 +1,36 @@
#pragma once
#include "simplefsm.hpp"
#include "dinkyecs.hpp"
#include "boss/ui.hpp"
#include "gui/fsm_events.hpp"
#include <memory>
#include <any>
namespace boss {
using std::shared_ptr;
enum class State {
START=0,
END=1
};
class Fight : public DeadSimpleFSM<State, gui::Event> {
public:
shared_ptr<World> $world = nullptr;
DinkyECS::Entity $boss_id = NONE;
boss::UI $ui;
sf::Vector2f mouse_pos{0,0};
Fight(shared_ptr<World> world, DinkyECS::Entity boss_id);
bool event(gui::Event ev, std::any data);
void START(gui::Event ev, std::any data);
void END(gui::Event ev, std::any data);
void render(sf::RenderWindow& window) {
$ui.render(window);
}
};
}

View file

@ -1,11 +0,0 @@
#include "boss/fsm.hpp"
namespace boss {
FSM::FSM() {
}
void event(Event ev, std::any data={}) {
(void)ev;
(void)data;
}
}

View file

@ -1,21 +0,0 @@
#pragma once
#include "simplefsm.hpp"
#include <any>
namespace boss {
enum class State {
START=0,
END=1
};
enum class Event {
NOP
};
class FSM : public DeadSimpleFSM<State, Event> {
FSM();
void event(Event ev, std::any data={});
};
}

View file

@ -10,7 +10,7 @@ namespace boss {
fmt::println("load it");
}
shared_ptr<boss::UI> System::create_bossfight() {
shared_ptr<boss::Fight> System::create_bossfight() {
auto& level = GameDB::current_level();
auto prev_world = GameDB::current_world();
dbc::check(prev_world != nullptr, "Starter world for boss fights can't be null.");
@ -24,6 +24,6 @@ namespace boss {
auto boss_id = world->entity();
components::configure_entity(*world, boss_id, boss_data["components"]);
return make_shared<boss::UI>(world, boss_id);
return make_shared<boss::Fight>(world, boss_id);
}
}

View file

@ -1,11 +1,11 @@
#pragma once
#include "dinkyecs.hpp"
#include <memory>
#include "boss/ui.hpp"
#include "boss/fight.hpp"
namespace boss {
namespace System {
void load_config();
std::shared_ptr<boss::UI> create_bossfight();
std::shared_ptr<boss::Fight> create_bossfight();
}
}