diff --git a/assets/bosses.json b/assets/bosses.json index 3497df3..30bf829 100644 --- a/assets/bosses.json +++ b/assets/bosses.json @@ -4,15 +4,15 @@ {"_type": "BossFight", "background": "test_background", "floor": "test_floor", - "floor_pos": "floor4", + "floor_pos": "floor2", "player": { "sprite": "test_player", - "start_pos": "player1", + "start_pos": "player2", "scale": 0.5, "mid_cell": false }, "boss": { - "start_pos": "boss2", + "start_pos": "boss3", "scale": 0.7, "mid_cell": true } diff --git a/boss/fight.cpp b/boss/fight.cpp new file mode 100644 index 0000000..9b4e96b --- /dev/null +++ b/boss/fight.cpp @@ -0,0 +1,55 @@ +#include "boss/fight.hpp" + +namespace boss { + Fight::Fight(shared_ptr 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(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; + } +} diff --git a/boss/fight.hpp b/boss/fight.hpp new file mode 100644 index 0000000..6917b4c --- /dev/null +++ b/boss/fight.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "simplefsm.hpp" +#include "dinkyecs.hpp" +#include "boss/ui.hpp" +#include "gui/fsm_events.hpp" +#include +#include + +namespace boss { + using std::shared_ptr; + + enum class State { + START=0, + END=1 + }; + + class Fight : public DeadSimpleFSM { + public: + shared_ptr $world = nullptr; + DinkyECS::Entity $boss_id = NONE; + boss::UI $ui; + sf::Vector2f mouse_pos{0,0}; + + Fight(shared_ptr 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); + } + }; +} diff --git a/boss/fsm.cpp b/boss/fsm.cpp deleted file mode 100644 index d318c45..0000000 --- a/boss/fsm.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "boss/fsm.hpp" - -namespace boss { - FSM::FSM() { - } - - void event(Event ev, std::any data={}) { - (void)ev; - (void)data; - } -} diff --git a/boss/fsm.hpp b/boss/fsm.hpp deleted file mode 100644 index b04d4af..0000000 --- a/boss/fsm.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include "simplefsm.hpp" -#include - -namespace boss { - enum class State { - START=0, - END=1 - }; - - enum class Event { - NOP - }; - - class FSM : public DeadSimpleFSM { - FSM(); - - void event(Event ev, std::any data={}); - }; -} diff --git a/boss/system.cpp b/boss/system.cpp index 718bf72..135ff57 100644 --- a/boss/system.cpp +++ b/boss/system.cpp @@ -10,7 +10,7 @@ namespace boss { fmt::println("load it"); } - shared_ptr System::create_bossfight() { + shared_ptr 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(world, boss_id); + return make_shared(world, boss_id); } } diff --git a/boss/system.hpp b/boss/system.hpp index d829057..fb9b621 100644 --- a/boss/system.hpp +++ b/boss/system.hpp @@ -1,11 +1,11 @@ #pragma once #include "dinkyecs.hpp" #include -#include "boss/ui.hpp" +#include "boss/fight.hpp" namespace boss { namespace System { void load_config(); - std::shared_ptr create_bossfight(); + std::shared_ptr create_bossfight(); } } diff --git a/gui/event_router.hpp b/gui/event_router.hpp index fd37ca9..b171033 100644 --- a/gui/event_router.hpp +++ b/gui/event_router.hpp @@ -1,6 +1,6 @@ #pragma once #include "events.hpp" -#include "fsm_events.hpp" +#include "gui/fsm_events.hpp" #include "simplefsm.hpp" #include diff --git a/gui/fsm.cpp b/gui/fsm.cpp index 708d334..273eb5d 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -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(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(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(); diff --git a/gui/fsm.hpp b/gui/fsm.hpp index d0db3a7..c7fe909 100644 --- a/gui/fsm.hpp +++ b/gui/fsm.hpp @@ -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_fight_ui = nullptr; + std::shared_ptr $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); diff --git a/meson.build b/meson.build index 26c21c1..0c3b3e5 100644 --- a/meson.build +++ b/meson.build @@ -86,7 +86,7 @@ sources = [ 'autowalker.cpp', 'backend.cpp', 'battle.cpp', - 'boss/fsm.cpp', + 'boss/fight.cpp', 'boss/system.cpp', 'boss/ui.cpp', 'combat.cpp',