diff --git a/events.hpp b/events.hpp index 5ac0289..032025e 100644 --- a/events.hpp +++ b/events.hpp @@ -2,11 +2,15 @@ namespace Events { enum GUI { - START, COMBAT, LOOT + START, COMBAT, LOOT, DEATH }; struct Combat { int player_did; int enemy_did; }; + + struct Death { + int placeholder = 0; + }; } diff --git a/gui.cpp b/gui.cpp index 3985df3..eabd269 100644 --- a/gui.cpp +++ b/gui.cpp @@ -30,6 +30,21 @@ using namespace std::chrono_literals; using namespace ftxui; using namespace components; +void DeathUI::create_render() { + has_border = true; + $exit_button = Button("EXIT", []{ std::exit(0); }); + + $render = Renderer([&] { + return vflow({ + paragraph($quip) | border, + $exit_button->Render() + }) | flex; + }); + + set_renderer($render); + add($exit_button); +} + void InventoryUI::create_render() { has_border = true; MenuOption option; @@ -196,9 +211,9 @@ void GUI::create_renderer() { $map_view.create_render(); $status_ui.create_render(); $inventory_ui.create_render(); - // don't activate this one + $death_ui.create_render(); - $panels = {&$map_view, &$status_ui}; + $active_panels = {&$map_view, &$status_ui}; } void GUI::handle_world_events() { @@ -227,6 +242,15 @@ void GUI::handle_world_events() { $sounds.play("combat_miss"); $status_ui.log("You MISSED the enemy."); } + } + case eGUI::DEATH: { + // auto &dead_data = std::any_cast(data); + println("PLAYER DEAD!"); + auto player_combat = $world.get(entity); + if(player_combat.dead) { + toggle_modal(&$death_ui, $player_died); + println("PLAYER DEAD show UI, after is {}", $player_died); + } } break; case eGUI::LOOT: { auto &item = std::any_cast(data); @@ -279,15 +303,7 @@ bool GUI::handle_ui_events() { auto &debug = $world.get_the(); debug.LIGHT = !debug.LIGHT; } else if(KB::isKeyPressed(KB::I)) { - // yes, using an if to avoid double grabbing screen - if($show_modal) { - $panels = {&$map_view, &$status_ui}; - $show_modal = false; - } else { - pause_screen(); - $panels = {&$inventory_ui}; - $show_modal = true; - } + toggle_modal(&$inventory_ui, $inventory_open); } else if(KB::isKeyPressed(KB::P)) { auto &debug = $world.get_the(); debug.PATHS = !debug.PATHS; @@ -299,7 +315,7 @@ bool GUI::handle_ui_events() { $status_ui.key_press(Event::Return); } } else { - for(Panel *panel : $panels) { + for(Panel *panel : $active_panels) { if($renderer.mouse_position(*panel, pos)) { if(MOUSE::isButtonPressed(MOUSE::Left)) { panel->mouse_click(Mouse::Button::Left, pos); @@ -355,13 +371,28 @@ void GUI::shake() { } } +void GUI::toggle_modal(Panel *panel, bool &is_open_out) { + if(is_open_out) { + $active_panels = {&$map_view, &$status_ui}; + is_open_out = false; + } else { + pause_screen(); + $active_panels = {panel}; + is_open_out = true; + } +} + void GUI::render_scene() { $renderer.clear(); - if($show_modal) { + if($inventory_open) { draw_paused(); $inventory_ui.render(); $renderer.draw($inventory_ui); + } else if($player_died) { + draw_paused(); + $death_ui.render(); + $renderer.draw($death_ui); } else { $map_view.render(); $renderer.draw($map_view); diff --git a/gui.hpp b/gui.hpp index 9a727fd..da161b8 100644 --- a/gui.hpp +++ b/gui.hpp @@ -47,6 +47,17 @@ struct UnDumbTSS { } }; +class DeathUI : public Panel { + public: + Component $render = nullptr; + Component $exit_button = nullptr; + std::string $quip = "You died like a dog."; + + DeathUI() : + Panel(INVENTORY_PIXEL_X, INVENTORY_PIXEL_Y, INVENTORY_WIDTH, INVENTORY_HEIGHT) {} + + void create_render(); +}; class InventoryUI : public Panel { public: @@ -105,13 +116,15 @@ class GUI { LightRender $lights; MapViewUI $map_view; InventoryUI $inventory_ui; + DeathUI $death_ui; Canvas $canvas; - bool $show_modal = false; + bool $inventory_open = false; + bool $player_died = false; Component $test_button; SoundManager $sounds; SFMLRender $renderer; UnDumbTSS $paused; - std::vector $panels; + std::vector $active_panels; public: GUI(DinkyECS::World& world, Map& game_map); @@ -133,4 +146,5 @@ public: void pause_screen(); void draw_paused(); void init_shaders(); + void toggle_modal(Panel *panel, bool &is_open_out); }; diff --git a/systems.cpp b/systems.cpp index 39e9fd9..803d2c8 100644 --- a/systems.cpp +++ b/systems.cpp @@ -99,6 +99,7 @@ void System::death(DinkyECS::World &world) { // BUG: maybe that can be allowed and looting just shows // BUG: all dead things there? auto &collider = world.get_the(); + auto player = world.get_the(); world.query([&](const auto &ent, auto &position, auto &combat) { // bring out yer dead @@ -107,8 +108,12 @@ void System::death(DinkyECS::World &world) { // take them out of collision map collider.remove(position.location); - // remove their motion so they're dead - world.remove(ent); + if(ent == player.entity) { + world.send(Events::GUI::DEATH, ent, {}); + } else { + // remove their motion so they're dead + world.remove(ent); + } } }); } diff --git a/worldbuilder.cpp b/worldbuilder.cpp index 377f374..85eb40e 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -215,7 +215,6 @@ void WorldBuilder::place_entities(DinkyECS::World &world) { // configure player in the world Player player{player_ent}; world.set_the(player); - world.set(player.entity, {100, 10}); world.set(player.entity, {50,1.0}); world.set(player.entity, {5});