diff --git a/assets/trash_button.png b/assets/trash_button.png new file mode 100644 index 0000000..ba823ae Binary files /dev/null and b/assets/trash_button.png differ diff --git a/combat_ui.cpp b/combat_ui.cpp index 031afd3..8466856 100644 --- a/combat_ui.cpp +++ b/combat_ui.cpp @@ -1,6 +1,7 @@ #include "combat_ui.hpp" #include "constants.hpp" #include "color.hpp" +#include "events.hpp" namespace gui { using namespace guecs; @@ -11,7 +12,7 @@ namespace gui { $gui.position(RAY_VIEW_X, RAY_VIEW_HEIGHT, RAY_VIEW_WIDTH, SCREEN_HEIGHT - RAY_VIEW_HEIGHT); $gui.layout( "[*%(100,150)button_attack1 | *%(100,150)button_attack2 | *%(100,150)button_attack3 | *%(100,150)button_heal]" - "[ >.%(100,50)label_hp | *%.(190,50)bar_hp | _ ]"); + "[ >.%(100,50)label_hp | *%.(198,50)bar_hp | _ ]"); } void CombatUI::render(TexturePack& textures) { @@ -22,8 +23,9 @@ namespace gui { auto button = $gui.entity(name); world.set(button, cell); world.set(button, {"trash_button"}); - world.set(button, {100}); - world.set(button, {"Button"}); + world.set(button, + guecs::make_action(*$level.world, Events::GUI::ATTACK)); + world.set(button, {"Attack"}); } else if(name.starts_with("bar_")) { $meter = $gui.entity(name); world.set($meter, cell); diff --git a/events.hpp b/events.hpp index cd44ffe..0db8821 100644 --- a/events.hpp +++ b/events.hpp @@ -3,7 +3,7 @@ namespace Events { enum GUI { START, COMBAT, LOOT, DEATH, STAIRS_UP, STAIRS_DOWN, TRAP, - COMBAT_START, NO_NEIGHBORS + COMBAT_START, NO_NEIGHBORS, ATTACK }; struct Combat { diff --git a/guecs.cpp b/guecs.cpp index 09913ce..c13fdaf 100644 --- a/guecs.cpp +++ b/guecs.cpp @@ -77,10 +77,15 @@ namespace guecs { (pos.y >= cell.y && pos.y <= cell.y + cell.h)) { auto& cn = $world.get(ent); - fmt::println("clicked on entity {} with name {} and event {}", - ent, cn.name, clicked.event); + clicked.action(ent, cn.name); } }); } } + + Clickable make_action(DinkyECS::World& target, Events::GUI event) { + return {[&, event](auto ent, auto&){ + target.send(event, ent, {}); + }}; + } } diff --git a/guecs.hpp b/guecs.hpp index fc1b1c8..337c8d1 100644 --- a/guecs.hpp +++ b/guecs.hpp @@ -6,6 +6,8 @@ #include #include #include "texture.hpp" +#include +#include "events.hpp" namespace guecs { using std::shared_ptr, std::make_shared; @@ -27,7 +29,7 @@ namespace guecs { }; struct Clickable { - int event = 0; + std::function action; }; struct Sprite { @@ -88,4 +90,6 @@ namespace guecs { void render(sf::RenderWindow& window); void mouse(sf::RenderWindow &window); }; + + Clickable make_action(DinkyECS::World& target, Events::GUI event); } diff --git a/gui.cpp b/gui.cpp index 7ce55bf..007f304 100644 --- a/gui.cpp +++ b/gui.cpp @@ -108,6 +108,9 @@ namespace gui { 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)); } @@ -249,10 +252,6 @@ namespace gui { case KEY::A: event(Event::MOVE_LEFT); break; - case KEY::Space: - $rotation = 0; - event(Event::ATTACK); - break; case KEY::R: $stats.reset(); break; @@ -262,15 +261,8 @@ namespace gui { case KEY::Escape: event(Event::CLOSE); break; - case KEY::P: { - auto& debug = $level.world->get_the(); - debug.FPS = !debug.FPS; - debug.PATHS = !debug.PATHS; - auto player = $level.world->get_the(); - auto& player_combat = $level.world->get(player.entity); - player_combat.hp = player_combat.max_hp; - $combat_view.set_damage(float(player_combat.hp) / float(player_combat.max_hp)); - } break; + case KEY::P: + debug(); default: break; // ignored } @@ -278,6 +270,16 @@ namespace gui { } } + void FSM::debug() { + auto& debug = $level.world->get_the(); + debug.FPS = !debug.FPS; + debug.PATHS = !debug.PATHS; + auto player = $level.world->get_the(); + auto& player_combat = $level.world->get(player.entity); + player_combat.hp = player_combat.max_hp; + $combat_view.set_damage(float(player_combat.hp) / float(player_combat.max_hp)); + } + void FSM::draw_weapon() { return; auto weapon = $rayview.$textures.sword.sprite; @@ -426,6 +428,10 @@ namespace gui { $status_view.log("You picked up an item."); } break; + case eGUI::ATTACK: + $rotation = 0; + event(Event::ATTACK); + break; default: $status_view.log(fmt::format("INVALID EVENT! {},{}", evt, entity)); } diff --git a/gui.hpp b/gui.hpp index d761557..225a94b 100644 --- a/gui.hpp +++ b/gui.hpp @@ -81,6 +81,7 @@ namespace gui { void draw_stats(); void draw_gui(); void draw_blood(); + void debug(); void render(); void mouse(); void generate_map();