Beginning state machine for controlling the boss fight UI is up.

This commit is contained in:
Zed A. Shaw 2025-12-07 00:21:07 -05:00
parent 1537a81aac
commit 9739441a9c
8 changed files with 173 additions and 135 deletions

View file

@ -1,3 +1,4 @@
#define FSM_DEBUG 1
#include "boss/fight.hpp"
#include "boss/system.hpp"
#include "animation.hpp"
@ -7,7 +8,8 @@ namespace boss {
$world(world),
$boss_id(boss_id),
$battle(System::create_battle($world, $boss_id)),
$ui(world, boss_id, player_id)
$ui(world, boss_id, player_id),
$player_id(player_id)
{
$ui.init();
}
@ -18,8 +20,9 @@ namespace boss {
switch($state) {
FSM_STATE(State, START, ev, data);
FSM_STATE(State, BOSS_TURN, ev, data);
FSM_STATE(State, PLAYER_TURN, ev, data);
FSM_STATE(State, PLAYER_REQUESTS, ev, data);
FSM_STATE(State, PLAN_BATTLE, ev, data);
FSM_STATE(State, EXEC_PLAN, ev, data);
FSM_STATE(State, END, ev, data);
}
@ -28,6 +31,97 @@ namespace boss {
return in_state(State::END);
}
void Fight::START(gui::Event ev, std::any data) {
(void)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 TICK:
$ui.status(L"PLAYER REQUESTS");
state(State::PLAYER_REQUESTS);
break;
default:
break;
}
}
void Fight::PLAYER_REQUESTS(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 START_COMBAT:
$ui.status(L"PLANNING BATTLE");
state(State::PLAN_BATTLE);
case TICK:
break; // ignore tick
default:
break;
}
}
void Fight::PLAN_BATTLE(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 START_COMBAT:
$ui.status(L"EXEC PLAN");
state(State::EXEC_PLAN);
break;
case TICK:
break; // ignore tick
default:
break;
}
}
void Fight::EXEC_PLAN(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 TICK:
if(player_dead()) {
$ui.status(L"YOU DIED");
state(State::END);
} else {
$ui.status(L"PLAYER REQUESTS");
state(State::PLAYER_REQUESTS);
}
break; // ignore tick
default:
break;
}
}
void Fight::END(gui::Event ev, std::any) {
fmt::println("BOSS_FIGHT:END event {}", (int)ev);
}
void Fight::run_systems() {
run++;
}
void Fight::render(sf::RenderWindow& window) {
window.clear();
$ui.play_animations();
$ui.render(window);
}
bool Fight::handle_mouse(gui::Event ev) {
using enum gui::Event;
@ -55,105 +149,8 @@ namespace boss {
return true;
}
void Fight::START(gui::Event ev, std::any data) {
(void)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 KEY_PRESS:
fmt::println("KEY_PRESS");
break;
case ATTACK:
$ui.status(L"PLAYER TURN");
state(State::PLAYER_TURN);
break;
case TICK:
break; // ignore tick
default:
fmt::println("BOSS_FIGHT:START unknown event {}", (int)ev);
break;
}
}
void Fight::BOSS_TURN(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 KEY_PRESS:
fmt::println("KEY_PRESS");
break;
case ATTACK: {
int attack_id = std::any_cast<int>(data);
fmt::println("Player attack: {}", attack_id);
$ui.status(L"PLAYER TURN");
const std::string& player_pos = run % 10 < 5 ? "player1" : "player2";
$ui.move_actor("player", player_pos);
boss::System::plan_battle($battle, $world, $boss_id);
while(auto action = $battle.next()) {
fmt::println("*** combat turn run: eid={}",
action->enemy.entity);
boss::System::combat(*action, $world, $boss_id, attack_id);
}
$ui.update_stats();
state(State::PLAYER_TURN);
} break;
case TICK:
break; // ignore tick
default:
fmt::println("BOSS_FIGHT:BOSS_TURN unknown event {}", (int)ev);
break;
}
}
void Fight::PLAYER_TURN(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 KEY_PRESS:
fmt::println("KEY_PRESS");
break;
case ATTACK: {
$ui.status(L"BOSS TURN");
const std::string &boss_at = run % 10 < 5 ? "boss5" : "boss6";
$ui.move_actor("boss", boss_at);
$ui.animate_actor("boss");
int attack_id = std::any_cast<int>(data);
state(State::BOSS_TURN);
} break;
case TICK:
break; // ignore tick
default:
fmt::println("BOSS_FIGHT:PLAYER_TURN unknown event {}", (int)ev);
break;
}
}
void Fight::END(gui::Event ev, std::any) {
fmt::println("BOSS_FIGHT:END event {}", (int)ev);
}
void Fight::run_systems() {
run++;
}
void Fight::render(sf::RenderWindow& window) {
window.clear();
$ui.play_animations();
$ui.render(window);
bool Fight::player_dead() {
auto& combat = $world->get<components::Combat>($player_id);
return combat.hp <= 0;
}
}