diff --git a/assets/scenes.json b/assets/scenes.json index d2b726e..3d7d4c4 100644 --- a/assets/scenes.json +++ b/assets/scenes.json @@ -1,58 +1,42 @@ { "DEATH": { - "components": [ - {"_type": "AnimatedScene", - "layout": [ - "[=text]" - ], - "background": "death_scene", - "actors": [ - ], - "fixtures": [ - ] - } + "layout": [ + "[=text]" + ], + "background": "death_scene", + "actors": [ + ], + "fixtures": [ ] }, "NEXT_LEVEL": { - "components": [ - {"_type": "AnimatedScene", - "layout": [ - "[=text]" - ], - "background": "next_level_scene", - "actors": [ - ], - "fixtures": [ - ] - } + "layout": [ + "[=text]" + ], + "background": "next_level_scene", + "actors": [ + ], + "fixtures": [ ] }, "STARTING": { - "components": [ - {"_type": "AnimatedScene", - "layout": [ - "[=text]" - ], - "background": "starting_scene", - "actors": [ - ], - "fixtures": [ - ] - } + "layout": [ + "[=text]" + ], + "background": "starting_scene", + "actors": [ + ], + "fixtures": [ ] }, "WIN": { - "components": [ - {"_type": "AnimatedScene", - "layout": [ - "[=text]" - ], - "background": "win_scene", - "actors": [ - ], - "fixtures": [ - ] - } + "layout": [ + "[=text]" + ], + "background": "win_scene", + "actors": [ + ], + "fixtures": [ ] } } diff --git a/src/graphics/scene.cpp b/src/graphics/scene.cpp index 7bb42c8..5c7ba6a 100644 --- a/src/graphics/scene.cpp +++ b/src/graphics/scene.cpp @@ -55,6 +55,8 @@ namespace scene { } void Engine::init() { + dbc::log($F("initialized scene with background {}", $scene.background)); + $ui.position(SCENE_VIEW_X, SCENE_VIEW_Y, SCENE_VIEW_WIDTH, SCENE_VIEW_HEIGHT); $ui.set($ui.MAIN, {$ui.$parser, guecs::THEME.TRANSPARENT}); auto& background = $ui.get($ui.MAIN); @@ -106,6 +108,9 @@ namespace scene { $camera.render(view); if(DEBUG) $ui.debug_layout(view); + + // BUG: should I do this here or let the caller do it? + view.display(); } Element& Engine::actor_config(const std::string& actor) { diff --git a/src/gui/fsm.cpp b/src/gui/fsm.cpp index 0574b87..38cf346 100644 --- a/src/gui/fsm.cpp +++ b/src/gui/fsm.cpp @@ -25,11 +25,13 @@ namespace gui { $window.setVerticalSyncEnabled(VSYNC); if(FRAME_LIMIT) $window.setFramerateLimit(FRAME_LIMIT); $window.setPosition({0,0}); + } void FSM::event(Event ev, std::any data) { switch($state) { FSM_STATE(State, START, ev); + FSM_STATE(State, START_SCREEN, ev); FSM_STATE(State, MOVING, ev); FSM_STATE(State, ATTACKING, ev, data); FSM_STATE(State, ROTATING, ev); @@ -44,13 +46,39 @@ namespace gui { $main_ui.init(); $loot_ui.init(); + for(auto& [name, scene] : $scenes) { + scene->init(); + } + // BUG: maybe this is a function on main_ui? auto cell = $main_ui.overlay_cell("left"); $debug_ui.init(cell); $status_ui.init(); run_systems(); - state(State::IDLE); + $cur_scene = $scenes["STARTING"]; + dbc::check($cur_scene != nullptr, "No STARTING scene?!"); + + state(State::START_SCREEN); + } + + void FSM::START_SCREEN(Event ev) { + using enum Event; + + switch(ev) { + case MOUSE_CLICK: + dbc::log("CLICK!"); + $cur_scene = nullptr; + state(State::IDLE); + break; + case MOUSE_MOVE: { + } break; + case TICK: + break; + default: + dbc::log($F("unhandled event: {}", int(ev))); + break; + } } void FSM::MOVING(Event ) { @@ -295,17 +323,22 @@ namespace gui { void FSM::draw_gui() { if($debug_ui.active) { debug_render(); - } else { - $main_ui.render(); } - $status_ui.render($window); - if($loot_ui.active) $loot_ui.render($window); - - if(in_state(State::LOOTING)) $dnd_loot.render(); + if($cur_scene != nullptr) { + $cur_scene->render($window); + } else { + $main_ui.render(); + $status_ui.render($window); + if($loot_ui.active) $loot_ui.render($window); + if(in_state(State::LOOTING)) $dnd_loot.render(); + } } void FSM::update() { + if($cur_scene != nullptr) { + $cur_scene->update(); + } } void FSM::render() { diff --git a/src/gui/fsm.hpp b/src/gui/fsm.hpp index 7d2ff44..3d6d6e4 100644 --- a/src/gui/fsm.hpp +++ b/src/gui/fsm.hpp @@ -11,10 +11,12 @@ #include "gui/event_router.hpp" #include "gui/dnd_loot.hpp" #include "events.hpp" +#include "gui/scene_ui.hpp" namespace gui { enum class State { START=__LINE__, + START_SCREEN=__LINE__, MOVING=__LINE__, ATTACKING=__LINE__, ROTATING=__LINE__, @@ -27,7 +29,6 @@ namespace gui { public: sf::RenderWindow $window; bool $draw_stats = false; - bool autowalking = false; DebugUI $debug_ui; MainUI $main_ui; StatusUI $status_ui{STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT}; @@ -35,17 +36,26 @@ namespace gui { gui::routing::Router $router; DNDLoot $dnd_loot; System::Registry $systems; + std::unordered_map> $scenes{ + {"STARTING", std::make_shared("STARTING")}, + {"DEATH", std::make_shared("DEATH")}, + {"WIN", std::make_shared("WIN")}, + {"NEXT_LEVEL", std::make_shared("NEXT_LEVEL")}, + }; + + std::shared_ptr $cur_scene = nullptr; FSM(); void event(game::Event ev, std::any data={}); void START(game::Event ev); + void START_SCREEN(game::Event ev); + void IDLE(game::Event ev, std::any data); void MOVING(game::Event ev); void ATTACKING(game::Event ev, std::any data); void MAPPING(game::Event ev); void ROTATING(game::Event ev); - void IDLE(game::Event ev, std::any data); void LOOTING(game::Event ev, std::any data); void END(game::Event ev); diff --git a/src/gui/scene_ui.cpp b/src/gui/scene_ui.cpp new file mode 100644 index 0000000..0cc9362 --- /dev/null +++ b/src/gui/scene_ui.cpp @@ -0,0 +1,29 @@ +#include "gui/scene_ui.hpp" +#include "game/config.hpp" + + +namespace gui { + void SceneUI::init() { + $view_sprite.setPosition({0,0}); + $scene.init(); + } + + void SceneUI::render(sf::RenderTarget &target) { + $scene.render($view_texture); + target.draw($view_sprite); + } + + void SceneUI::update() { + $scene.update(); + } + + bool SceneUI::mouse(float x, float y, guecs::Modifiers mods) { + $scene.mouse(x, y, mods); + return false; + } + + AnimatedScene SceneUI::load_scene(const std::string& name) { + auto scene_config = settings::get("scenes")[name]; + return components::convert(scene_config); + } +} diff --git a/src/gui/scene_ui.hpp b/src/gui/scene_ui.hpp new file mode 100644 index 0000000..49be65d --- /dev/null +++ b/src/gui/scene_ui.hpp @@ -0,0 +1,23 @@ +#pragma once +#include +#include "graphics/scene.hpp" + +namespace gui { + using namespace components; + + class SceneUI { + public: + std::string name; + sf::RenderTexture $view_texture{{SCENE_VIEW_WIDTH, SCENE_VIEW_HEIGHT}}; + sf::Sprite $view_sprite{$view_texture.getTexture()}; + AnimatedScene $scene_data{load_scene(name)}; + scene::Engine $scene{$scene_data}; + + AnimatedScene load_scene(const std::string& name); + + void init(); + void render(sf::RenderTarget &target); + void update(); + bool mouse(float x, float y, guecs::Modifiers mods); + }; +} diff --git a/src/main.cpp b/src/main.cpp index 164a7b9..c5c41a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,7 @@ int main(int argc, char* argv[]) { // BUG: need to sort out how to deal with this in the FSM if(main.in_state(gui::State::IDLE) + || main.in_state(gui::State::START_SCREEN) || main.in_state(gui::State::LOOTING)) { main.handle_keyboard_mouse(); diff --git a/src/meson.build b/src/meson.build index 8017508..7148c99 100644 --- a/src/meson.build +++ b/src/meson.build @@ -20,6 +20,7 @@ sources = files( 'gui/main_ui.cpp', 'gui/overlay_ui.cpp', 'gui/body_ui.cpp', + 'gui/scene_ui.cpp', # graphics 'graphics/animation.cpp',