#include "gui/fsm.hpp" #include #include #include #include #include "components.hpp" #include #include "systems.hpp" #include "events.hpp" #include "sound.hpp" #include "shaders.hpp" #include namespace gui { using namespace components; FSM::FSM() : $window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"), $debug_ui($levels), $main_ui($window), $level($levels.current()), $combat_ui($level), $status_ui($level), $map_ui($level), $mini_map($level), $loot_ui($level), $font{FONT_FILE_NAME} { } void FSM::event(Event ev) { switch($state) { FSM_STATE(State, START, ev); FSM_STATE(State, MOVING, ev); FSM_STATE(State, ATTACKING, ev); FSM_STATE(State, ROTATING, ev); FSM_STATE(State, IDLE, ev); FSM_STATE(State, IN_COMBAT, ev); FSM_STATE(State, COMBAT_ROTATE, ev); FSM_STATE(State, NEXT_LEVEL, ev); FSM_STATE(State, END, ev); FSM_STATE(State, LOOTING, ev, std::make_any(true)); } } void FSM::event(Event ev, std::any data) { switch($state) { FSM_STATE(State, LOOTING, ev, data); default: dbc::log(fmt::format("event given with data but event={} is not handled", int(ev))); } } void FSM::START(Event ) { $main_ui.update_level($level); $main_ui.init(); $loot_ui.init(); // BUG: maybe this is a function on main_ui? auto cell = $main_ui.$overlay_ui.$gui.cell_for("left"); $debug_ui.init(cell); $combat_ui.init(); $status_ui.init(); $boss_fight_ui = $levels.create_bossfight($level.world); $boss_fight_ui->init(); $map_ui.init(); $map_ui.log(L"Welcome to the game!"); $mini_map.init($main_ui.$overlay_ui.$gui); run_systems(); state(State::IDLE); } void FSM::MOVING(Event ) { // this should be an optional that returns a point if(auto move_to = $main_ui.play_move()) { System::plan_motion(*$level.world, *move_to); run_systems(); $main_ui.dirty(); state(State::IDLE); } } void FSM::ATTACKING(Event ev) { using enum Event; switch(ev) { case TICK: { System::combat($level, $temp_attack_id); run_systems(); state(State::IN_COMBAT); } break; case STOP_COMBAT: dbc::log("Exiting ATTACKING STATE"); state(State::IDLE); break; case ATTACK: // ignore these since they're just from SFML not having discrete events break; default: dbc::log(fmt::format("In ATTACKING state, unhandled event {}", (int)ev)); state(State::IDLE); } } void FSM::ROTATING(Event ) { if($main_ui.play_rotate()) { state(State::IDLE); } } void FSM::COMBAT_ROTATE(Event ) { if($main_ui.play_rotate()) { state(State::IN_COMBAT); } } void FSM::LOOTING(Event ev, std::any data) { using enum Event; switch(ev) { case LOOT_OPEN: $loot_ui.active = false; state(State::IDLE); break; case LOOT_SELECT: { fmt::println("loot is selected"); int slot_id = std::any_cast(data); if(auto entity = $loot_ui.select_slot(slot_id)) { fmt::println("LOOT SELECT slot={} has entity={}", slot_id, entity.value()); $status_ui.select_slot(slot_id, entity.value()); } } break; case LOOT_PLACE: { std::string slot_name = std::any_cast(data); int slot_id = $status_ui.place_slot(slot_name); // BUG: fix this bullshit if(slot_id != -1) { $loot_ui.remove_slot(slot_id); } } break; case MOUSE_CLICK: mouse_action(false); break; case MOUSE_MOVE: mouse_action(true); break; case MOUSE_DRAG_START: mouse_action(false); break; case MOUSE_DRAG: mouse_action(true); break; case MOUSE_DROP: mouse_action(false); break; case TICK: // do nothing break; default: state(State::LOOTING); } } void FSM::IDLE(Event ev) { using enum Event; sound::stop("walk"); switch(ev) { case QUIT: $window.close(); state(State::END); return; // done case MOVE_FORWARD: try_move(1, false); break; case MOVE_BACK: try_move(-1, false); break; case MOVE_LEFT: try_move(-1, true); break; case MOVE_RIGHT: try_move(1, true); break; case ROTATE_LEFT: $main_ui.plan_rotate(-1, 0.25f); state(State::ROTATING); break; case ROTATE_RIGHT: $main_ui.plan_rotate(1, 0.25f); state(State::ROTATING); break; case MAP_OPEN: $map_open = !$map_open; break; case ATTACK: state(State::ATTACKING); break; case START_COMBAT: $map_open = false; state(State::IN_COMBAT); break; case CLOSE: dbc::log("Nothing to close."); break; case STAIRS_DOWN: sound::stop("ambient"); state(State::NEXT_LEVEL); break; case STOP_COMBAT: case TICK: // do nothing break; case LOOT_OPEN: { dbc::log("IDLE LOOT OPEN!"); Config items("assets/items.json"); auto& data = items["TORCH_BAD"]; auto torch = $level.world->entity(); components::configure_entity(*$level.world, torch, data["components"]); $loot_ui.contents.push_back(torch); $loot_ui.update(); $loot_ui.active = true; state(State::LOOTING); } break; case LOOT_PLACE: // ignored break; case MOUSE_CLICK: mouse_action(false); break; case MOUSE_MOVE: mouse_action(true); break; case MOUSE_DRAG: // ignored case MOUSE_DRAG_START: // ignored case MOUSE_DROP: // ignored break; default: dbc::sentinel("unhandled event in IDLE"); } } void FSM::NEXT_LEVEL(Event ev) { using enum Event; switch(ev) { case STAIRS_DOWN: sound::play("ambient"); next_level(); state(State::IDLE); break; case MOUSE_CLICK: { sf::Vector2f pos = mouse_position(); $boss_fight_ui->mouse(pos.x, pos.y, false); if($boss_fight_ui->boss_dead()) { event(Event::STAIRS_DOWN); } } break; default: break; // do nothing for now } } void FSM::IN_COMBAT(Event ev) { using enum Event; switch(ev) { case MOUSE_CLICK: mouse_action(false); break; case MOUSE_MOVE: mouse_action(true); break; case ATTACK: $main_ui.dirty(); sound::play("Sword_Hit_1"); state(State::ATTACKING); break; case ROTATE_LEFT: $main_ui.plan_rotate(-1, DEFAULT_ROTATE); state(State::COMBAT_ROTATE); break; case ROTATE_RIGHT: $main_ui.plan_rotate(1, DEFAULT_ROTATE); state(State::COMBAT_ROTATE); break; case STOP_COMBAT: $main_ui.$overlay_ui.close_sprite("top_right"); state(State::IDLE); break; case QUIT: $window.close(); state(State::END); return; default: break; } } void FSM::try_move(int dir, bool strafe) { using enum State; // prevent moving into occupied space Point move_to = $main_ui.plan_move(dir, strafe); if($level.map->can_move(move_to) && !$level.collision->occupied(move_to)) { sound::play("walk"); state(MOVING); } else { state(IDLE); $main_ui.abort_plan(); } } void FSM::END(Event ev) { dbc::log(fmt::format("END: received event after done: {}", int(ev))); } sf::Vector2f FSM::mouse_position() { return $window.mapPixelToCoords($router.position); } void FSM::mouse_action(bool hover) { sf::Vector2f pos = mouse_position(); if($debug_ui.active) $debug_ui.mouse(pos.x, pos.y, hover); $combat_ui.mouse(pos.x, pos.y, hover); $status_ui.mouse(pos.x, pos.y, hover); $main_ui.mouse(pos.x, pos.y, hover); if($loot_ui.active) $loot_ui.mouse(pos.x, pos.y, hover); } void FSM::handle_keyboard_mouse() { while(const auto ev = $window.pollEvent()) { auto gui_ev = $router.process_event(ev); if(gui_ev == Event::KEY_PRESS) { using KEY = sf::Keyboard::Scan; switch($router.scancode) { case KEY::W: event(Event::MOVE_FORWARD); break; case KEY::S: event(Event::MOVE_BACK); break; case KEY::Q: event(Event::ROTATE_LEFT); break; case KEY::E: event(Event::ROTATE_RIGHT); break; case KEY::D: event(Event::MOVE_RIGHT); break; case KEY::A: event(Event::MOVE_LEFT); break; case KEY::R: dbc::log("HEY! DIPSHIT! You need to move debug ui so you can rest stats."); break; case KEY::M: event(Event::MAP_OPEN); break; case KEY::Escape: event(Event::CLOSE); break; case KEY::Space: event(Event::ATTACK); break; case KEY::P: sound::mute(false); if(!sound::playing("ambient_1")) sound::play("ambient_1", true); $debug_ui.debug(); shaders::reload(); $map_ui.save_map("map.txt", $main_ui.$compass_dir); break; case KEY::O: autowalking = true; break; case KEY::L: event(Event::LOOT_OPEN); break; case KEY::X: event(Event::STAIRS_DOWN); break; default: break; // ignored } } else { event(gui_ev); } } } void FSM::debug_render() { auto start = $debug_ui.time_start(); $main_ui.render(); $debug_ui.sample_time(start); $debug_ui.render($window); } void FSM::draw_gui() { if(in_state(State::NEXT_LEVEL)) { $boss_fight_ui->render($window); } else { if($debug_ui.active) { debug_render(); } else { $main_ui.render(); } $status_ui.render($window); $combat_ui.render($window); if($loot_ui.active) $loot_ui.render($window); if($map_open) { $map_ui.render($window, $main_ui.$compass_dir); } else { // $mini_map.render($window, $main_ui.$compass_dir); } } } void FSM::render() { if(in_state(State::NEXT_LEVEL)) { $window.clear(); $boss_fight_ui->render($window); } else { draw_gui(); } $window.display(); } void FSM::run_systems() { System::generate_paths($level); System::enemy_ai_initialize($level); System::enemy_pathing($level); System::collision($level); System::motion($level); System::lighting($level); System::death($level); } bool FSM::active() { return !in_state(State::END); } void FSM::handle_world_events() { using eGUI = Events::GUI; auto& world = *$level.world; while(world.has_event()) { auto [evt, entity, data] = world.recv(); auto player = world.get_the(); switch(evt) { case eGUI::COMBAT: { auto &damage = std::any_cast(data); if(damage.enemy_did > 0) { $map_ui.log(fmt::format(L"Enemy HIT YOU for {} damage!", damage.enemy_did)); } else { $map_ui.log(L"Enemy MISSED YOU."); } if(damage.player_did > 0) { $map_ui.log(fmt::format(L"You HIT enemy for {} damage!", damage.player_did)); } else { $map_ui.log(L"You MISSED the enemy."); } } break; case eGUI::COMBAT_START: event(Event::START_COMBAT); break; case eGUI::ENEMY_SPAWN: $main_ui.update_level($level); run_systems(); break; case eGUI::NO_NEIGHBORS: event(Event::STOP_COMBAT); break; case eGUI::LOOT_CLOSE: // BUG: need to resolve GUI events vs. FSM events better event(Event::LOOT_OPEN); break; case eGUI::LOOT_SELECT: event(Event::LOOT_SELECT, data); break; case eGUI::LOOT_PLACE: event(Event::LOOT_PLACE, data); break; case eGUI::LOOT: { $map_ui.log(L"You picked up an item."); } break; case eGUI::HP_STATUS: System::player_status($level); break; case eGUI::NEW_RITUAL: $combat_ui.init(); break; case eGUI::EVADE: case eGUI::BLOCK: dbc::log("YOU NEED TO IMPLEMENT THIS!!!!!"); break; case eGUI::ATTACK: $temp_attack_id = std::any_cast(data); event(Event::ATTACK); break; case eGUI::STAIRS_DOWN: event(Event::STAIRS_DOWN); break; case eGUI::DEATH: { $status_ui.update(); if(entity != player.entity) { $main_ui.dead_entity(entity); } } break; case eGUI::NOOP: { if(data.type() == typeid(std::string)) { auto name = std::any_cast(data); $map_ui.log(fmt::format(L"NOOP EVENT! {},{}", evt, entity)); } } break; default: $map_ui.log(fmt::format(L"INVALID EVENT! {},{}", evt, entity)); } } } void FSM::next_level() { $levels.create_level($level.world); $level = $levels.next(); $status_ui.update_level($level); $map_ui.update_level($level); $mini_map.update_level($level); $combat_ui.update_level($level); $main_ui.update_level($level); $loot_ui.update_level($level); $boss_fight_ui = $levels.create_bossfight($level.world); $boss_fight_ui->init(); run_systems(); } }