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

View file

@ -4,15 +4,15 @@
{"_type": "BossFight", {"_type": "BossFight",
"background": "test_background", "background": "test_background",
"floor": "test_floor", "floor": "test_floor",
"floor_pos": "floor4", "floor_pos": "floor2",
"player": { "player": {
"sprite": "test_player", "sprite": "test_player",
"start_pos": "player1", "start_pos": "player2",
"scale": 0.5, "scale": 0.5,
"mid_cell": false "mid_cell": false
}, },
"boss": { "boss": {
"start_pos": "boss2", "start_pos": "boss3",
"scale": 0.7, "scale": 0.7,
"mid_cell": true "mid_cell": true
} }

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"); fmt::println("load it");
} }
shared_ptr<boss::UI> System::create_bossfight() { shared_ptr<boss::Fight> System::create_bossfight() {
auto& level = GameDB::current_level(); auto& level = GameDB::current_level();
auto prev_world = GameDB::current_world(); auto prev_world = GameDB::current_world();
dbc::check(prev_world != nullptr, "Starter world for boss fights can't be null."); 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(); auto boss_id = world->entity();
components::configure_entity(*world, boss_id, boss_data["components"]); 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 #pragma once
#include "dinkyecs.hpp" #include "dinkyecs.hpp"
#include <memory> #include <memory>
#include "boss/ui.hpp" #include "boss/fight.hpp"
namespace boss { namespace boss {
namespace System { namespace System {
void load_config(); void load_config();
std::shared_ptr<boss::UI> create_bossfight(); std::shared_ptr<boss::Fight> create_bossfight();
} }
} }

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "events.hpp" #include "events.hpp"
#include "fsm_events.hpp" #include "gui/fsm_events.hpp"
#include "simplefsm.hpp" #include "simplefsm.hpp"
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>

View file

@ -32,14 +32,14 @@ namespace gui {
switch($state) { switch($state) {
FSM_STATE(State, START, ev); FSM_STATE(State, START, ev);
FSM_STATE(State, MOVING, ev); FSM_STATE(State, MOVING, ev);
FSM_STATE(State, ATTACKING, ev); FSM_STATE(State, ATTACKING, ev, data);
FSM_STATE(State, ROTATING, ev); FSM_STATE(State, ROTATING, ev);
FSM_STATE(State, IDLE, ev, data); FSM_STATE(State, IDLE, ev, data);
FSM_STATE(State, IN_COMBAT, ev); FSM_STATE(State, IN_COMBAT, ev);
FSM_STATE(State, COMBAT_ROTATE, ev); FSM_STATE(State, COMBAT_ROTATE, ev);
FSM_STATE(State, BOSS_FIGHT, ev, data); FSM_STATE(State, BOSS_FIGHT, ev, data);
FSM_STATE(State, END, ev);
FSM_STATE(State, LOOTING, ev, data); FSM_STATE(State, LOOTING, ev, data);
FSM_STATE(State, END, ev);
} }
} }
@ -72,20 +72,23 @@ namespace gui {
} }
} }
void FSM::ATTACKING(Event ev) { void FSM::ATTACKING(Event ev, std::any data) {
using enum Event; using enum Event;
switch(ev) { switch(ev) {
case TICK: { case TICK: {
System::combat($temp_attack_id); dbc::log("!!!!!! FIX System::combat");
System::combat(0);
run_systems(); run_systems();
state(State::IN_COMBAT); state(State::IN_COMBAT);
} break; } break;
case STOP_COMBAT: case STOP_COMBAT:
state(State::IDLE); state(State::IDLE);
break; break;
case ATTACK: case ATTACK: {
// ignore these since they're just from SFML not having discrete events int attack_id = std::any_cast<int>(data);
break; System::combat(attack_id);
run_systems();
} break;
default: default:
dbc::log(fmt::format("In ATTACKING state, unhandled event {}", (int)ev)); dbc::log(fmt::format("In ATTACKING state, unhandled event {}", (int)ev));
state(State::IDLE); state(State::IDLE);
@ -207,39 +210,13 @@ namespace gui {
} }
void FSM::BOSS_FIGHT(Event ev, std::any data) { void FSM::BOSS_FIGHT(Event ev, std::any data) {
dbc::check($boss_fight_ui != nullptr, "$boss_fight_ui not initialized"); dbc::check($boss_fight != nullptr, "$boss_fight not initialized");
using enum Event; $boss_fight->mouse_pos = mouse_position();
(void)data; ///// !!!!!!!!!!!!!!!!!!!!!!! DIE this sucks
switch(ev) { if($boss_fight->event(ev, data)) {
// this is only if using the debug X key to skip it sound::play("ambient");
case BOSS_START: next_level(false);
case BOSS_END: state(State::IDLE);
sound::play("ambient");
next_level(false);
state(State::IDLE);
break;
case MOUSE_CLICK: {
sf::Vector2f pos = mouse_position();
$boss_fight_ui->mouse(pos.x, pos.y, guecs::NO_MODS);
if($boss_fight_ui->boss_dead()) {
event(Event::BOSS_END);
}
} break;
case ATTACK: {
// BUG: get rid of temp attack id and pass the data
$boss_fight_ui->attack($temp_attack_id);
} break;
case MOUSE_MOVE: {
sf::Vector2f pos = mouse_position();
$boss_fight_ui->mouse(pos.x, pos.y, {1 << guecs::ModBit::hover});
} break;
case TICK:
$boss_fight_ui->run_systems();
break;
default:
fmt::println("BOSS_FIGHT unknown event {}", (int)ev);
} }
} }
@ -399,7 +376,7 @@ namespace gui {
void FSM::draw_gui() { void FSM::draw_gui() {
if(in_state(State::BOSS_FIGHT)) { if(in_state(State::BOSS_FIGHT)) {
$boss_fight_ui->render($window); $boss_fight->render($window);
} else { } else {
if($debug_ui.active) { if($debug_ui.active) {
debug_render(); debug_render();
@ -422,7 +399,7 @@ namespace gui {
void FSM::render() { void FSM::render() {
if(in_state(State::BOSS_FIGHT)) { if(in_state(State::BOSS_FIGHT)) {
$window.clear(); $window.clear();
$boss_fight_ui->render($window); $boss_fight->render($window);
} else { } else {
draw_gui(); draw_gui();
} }
@ -519,8 +496,7 @@ namespace gui {
$combat_ui.init(COMBAT_UI_X, COMBAT_UI_Y, COMBAT_UI_WIDTH, COMBAT_UI_HEIGHT); $combat_ui.init(COMBAT_UI_X, COMBAT_UI_Y, COMBAT_UI_WIDTH, COMBAT_UI_HEIGHT);
break; break;
case eGUI::ATTACK: case eGUI::ATTACK:
$temp_attack_id = std::any_cast<int>(data); event(Event::ATTACK, data);
event(Event::ATTACK);
break; break;
case eGUI::STAIRS_DOWN: case eGUI::STAIRS_DOWN:
event(Event::BOSS_START); event(Event::BOSS_START);
@ -557,8 +533,7 @@ namespace gui {
void FSM::next_level(bool bossfight) { void FSM::next_level(bool bossfight) {
if(bossfight) { if(bossfight) {
$boss_fight_ui = boss::System::create_bossfight(); $boss_fight = boss::System::create_bossfight();
$boss_fight_ui->init();
} else { } else {
GameDB::create_level(); GameDB::create_level();
$status_ui.update_level(); $status_ui.update_level();

View file

@ -7,7 +7,7 @@
#include "gui/combat_ui.hpp" #include "gui/combat_ui.hpp"
#include "gui/status_ui.hpp" #include "gui/status_ui.hpp"
#include "gui/loot_ui.hpp" #include "gui/loot_ui.hpp"
#include "boss/ui.hpp" #include "boss/fight.hpp"
#include "gui/map_view.hpp" #include "gui/map_view.hpp"
#include "events.hpp" #include "events.hpp"
#include "gui/event_router.hpp" #include "gui/event_router.hpp"
@ -33,10 +33,9 @@ namespace gui {
bool $draw_stats = false; bool $draw_stats = false;
bool autowalking = false; bool autowalking = false;
bool $map_open = false; bool $map_open = false;
int $temp_attack_id = 0;
DebugUI $debug_ui; DebugUI $debug_ui;
MainUI $main_ui; MainUI $main_ui;
std::shared_ptr<boss::UI> $boss_fight_ui = nullptr; std::shared_ptr<boss::Fight> $boss_fight = nullptr;
CombatUI $combat_ui; CombatUI $combat_ui;
StatusUI $status_ui; StatusUI $status_ui;
MapViewUI $map_ui; MapViewUI $map_ui;
@ -51,11 +50,11 @@ namespace gui {
void autowalk(); void autowalk();
void start_autowalk(double rot_speed); void start_autowalk(double rot_speed);
void START(Event ); void START(Event ev);
void MOVING(Event ); void MOVING(Event ev);
void ATTACKING(Event ); void ATTACKING(Event ev, std::any data);
void MAPPING(Event); void MAPPING(Event ev);
void ROTATING(Event ); void ROTATING(Event ev);
void IDLE(Event ev, std::any data); void IDLE(Event ev, std::any data);
void IN_COMBAT(Event ev); void IN_COMBAT(Event ev);
void COMBAT_ROTATE(Event ev); void COMBAT_ROTATE(Event ev);

View file

@ -86,7 +86,7 @@ sources = [
'autowalker.cpp', 'autowalker.cpp',
'backend.cpp', 'backend.cpp',
'battle.cpp', 'battle.cpp',
'boss/fsm.cpp', 'boss/fight.cpp',
'boss/system.cpp', 'boss/system.cpp',
'boss/ui.cpp', 'boss/ui.cpp',
'combat.cpp', 'combat.cpp',