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

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

View file

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

View file

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