diff --git a/events.hpp b/events.hpp index 82b4bee..38cc923 100644 --- a/events.hpp +++ b/events.hpp @@ -7,7 +7,7 @@ namespace Events { ATTACK, NEW_RITUAL, UPDATE_SPRITE, ENTITY_SPAWN, NOOP, LOOT_ITEM, LOOT_CONTAINER, - LOOT_CLOSE, LOOT_SELECT, INV_SELECT, AIM_CLICK, USE_ITEM + LOOT_CLOSE, LOOT_SELECT, INV_SELECT, AIM_CLICK }; struct Combat { diff --git a/gui/combat_ui.cpp b/gui/combat_ui.cpp index c5e5a16..45a0286 100644 --- a/gui/combat_ui.cpp +++ b/gui/combat_ui.cpp @@ -64,19 +64,6 @@ namespace gui { } } - auto healing_button = $gui.entity("healing_button"); - auto& inventory = $level.world->get($level.player); - - if(inventory.has("pocket_l")) { - $gui.set(healing_button, {"healing_potion_small"}); - $gui.set(healing_button, {[&](auto gui_id, auto) { - use_item(gui_id, "pocket_l"); - }}); - } else { - $gui.remove(healing_button); - $gui.remove(healing_button); - } - auto hp_gauge = $gui.entity("hp_gauge"); $gui.set(hp_gauge, {"stone_doll_cursed"}); $gui.set(hp_gauge, @@ -85,15 +72,6 @@ namespace gui { $gui.init(); } - void CombatUI::use_item(guecs::Entity gui_id, const string& slot) { - auto& inventory = $level.world->get($level.player); - dbc::check(inventory.has(slot), fmt::format( - "attempted to use an item but {} isn't in your inventory", slot)); - - auto healing_item = inventory.get(slot); - $level.world->send(Events::GUI::USE_ITEM, gui_id, {healing_item}); - } - void CombatUI::render(sf::RenderWindow& window) { $gui.render(window); } diff --git a/gui/combat_ui.hpp b/gui/combat_ui.hpp index 1d01022..e8f4a3d 100644 --- a/gui/combat_ui.hpp +++ b/gui/combat_ui.hpp @@ -20,6 +20,5 @@ namespace gui { guecs::Entity make_button(std::string name, Events::GUI event, int action, const std::string &icon_name, const std::string &sound, const std::string &effect_name); - void use_item(guecs::Entity gui_id, const string& slot); }; } diff --git a/gui/event_router.cpp b/gui/event_router.cpp index c00f886..25e96e6 100644 --- a/gui/event_router.cpp +++ b/gui/event_router.cpp @@ -15,14 +15,17 @@ namespace gui { } if(const auto* mouse = ev->getIf()) { - if(mouse->button == sf::Mouse::Button::Left) { - position = mouse->position; - event(MOUSE_DOWN); + if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) { + left_button = mouse->button == sf::Mouse::Button::Left; + position = mouse->position; + event(MOUSE_DOWN); } } else if(const auto* mouse = ev->getIf()) { - if(mouse->button == sf::Mouse::Button::Left) { - position = mouse->position; - event(MOUSE_UP); + // need to sort this out but if you don't do this it thinks you're always pressing it + if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) { + left_button = mouse->button == sf::Mouse::Button::Left; + position = mouse->position; + event(MOUSE_UP); } } else if(const auto* mouse = ev->getIf()) { position = mouse->position; diff --git a/gui/event_router.hpp b/gui/event_router.hpp index b720973..fd37ca9 100644 --- a/gui/event_router.hpp +++ b/gui/event_router.hpp @@ -28,6 +28,7 @@ namespace gui { sf::Keyboard::Scancode scancode; gui::Event $next_event = gui::Event::TICK; int move_count = 0; + bool left_button = true; int $drag_tolerance = 4; void event(Event ev); diff --git a/gui/fsm.cpp b/gui/fsm.cpp index c3f6038..4dedd71 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -192,7 +192,16 @@ namespace gui { $dnd_loot.event(Event::INV_SELECT, data); state(State::LOOTING); break; + case USE_ITEM: { + auto gui_id = std::any_cast(data); + auto& slot_name = $status_ui.$gui.name_for(gui_id); + + if(System::use_item($level, slot_name)) { + $status_ui.update(); + } + } break; case MOUSE_CLICK: + fmt::println("CLICK: {}", $router.left_button); mouse_action(false); break; case MOUSE_MOVE: @@ -200,6 +209,7 @@ namespace gui { break; case AIM_CLICK: System::pickup($level); + break; default: break; // ignore everything else } @@ -470,9 +480,13 @@ namespace gui { case eGUI::LOOT_SELECT: event(Event::LOOT_SELECT, data); break; - case eGUI::INV_SELECT: - event(Event::INV_SELECT, data); - break; + case eGUI::INV_SELECT: { + if($router.left_button) { + event(Event::INV_SELECT, data); + } else { + event(Event::USE_ITEM, data); + } + } break; case eGUI::AIM_CLICK: event(Event::AIM_CLICK); break; @@ -488,11 +502,11 @@ namespace gui { event(Event::LOOT_OPEN); } break; case eGUI::HP_STATUS: - System::player_status($level); - break; + System::player_status($level); + break; case eGUI::NEW_RITUAL: - $combat_ui.init(); - break; + $combat_ui.init(); + break; case eGUI::ATTACK: $temp_attack_id = std::any_cast(data); event(Event::ATTACK); @@ -500,14 +514,6 @@ namespace gui { case eGUI::STAIRS_DOWN: event(Event::STAIRS_DOWN); break; - case eGUI::USE_ITEM: { - auto what = std::any_cast(data); - - if(System::use_item($level, what)) { - $status_ui.update(); - } - break; - } case eGUI::DEATH: { $status_ui.update(); if(entity != player.entity) { diff --git a/gui/fsm_events.hpp b/gui/fsm_events.hpp index f63719e..6d6940a 100644 --- a/gui/fsm_events.hpp +++ b/gui/fsm_events.hpp @@ -20,13 +20,14 @@ namespace gui { LOOT_ITEM=15, LOOT_SELECT=16, INV_SELECT=17, - QUIT = 18, - MOUSE_CLICK=19, - MOUSE_MOVE=20, - MOUSE_DRAG=21, - MOUSE_DRAG_START=22, - MOUSE_DROP=23, - KEY_PRESS=24, - AIM_CLICK=25 + USE_ITEM=18, + QUIT = 19, + MOUSE_CLICK=20, + MOUSE_MOVE=21, + MOUSE_DRAG=22, + MOUSE_DRAG_START=23, + MOUSE_DROP=24, + KEY_PRESS=25, + AIM_CLICK=26 }; } diff --git a/systems.cpp b/systems.cpp index 87effc4..2ed3748 100644 --- a/systems.cpp +++ b/systems.cpp @@ -320,10 +320,7 @@ void System::pickup(GameLevel &level) { auto &collision = *level.collision; auto pos = player_position(level); - if(!collision.something_there(pos.aiming_at)) { - dbc::log("nothing there"); - return; - } + if(!collision.something_there(pos.aiming_at)) return; auto entity = level.collision->find(pos.aiming_at, [&](auto data) -> bool { return (world.has(data.entity) || @@ -336,7 +333,7 @@ void System::pickup(GameLevel &level) { } // use spatial find to find an item with inventory... - if(auto item = world.get_if(entity)) { + if(world.has(entity)) { // NOTE: this might need to be a separate system so that people can leave stuff alone remove_from_world(level, entity); @@ -352,7 +349,7 @@ void System::pickup(GameLevel &level) { // NOTE: chests are different from say a torch, maybe 2 events or the // GUI figures out which it is, then when you click either pick it up // and move it or show the loot container UI - world.send(Events::GUI::LOOT_ITEM, entity, *item); + world.send(Events::GUI::LOOT_ITEM, entity, entity); } } else if(world.has(entity)) { System::device(world, level.player, entity); @@ -591,13 +588,15 @@ void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture render.display(); } -bool System::use_item(GameLevel& level, Entity what) { +bool System::use_item(GameLevel& level, const string& slot_name) { auto& world = *level.world; auto& inventory = world.get(level.player); auto& player_combat = world.get(level.player); if(player_combat.hp >= player_combat.max_hp) return false; + auto what = inventory.get(slot_name); + if(auto curative = world.get_if(what)) { inventory.remove(what); diff --git a/systems.hpp b/systems.hpp index 040a866..24eeccf 100644 --- a/systems.hpp +++ b/systems.hpp @@ -42,5 +42,5 @@ namespace System { void render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display); void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos); - bool use_item(GameLevel& level, Entity what); + bool use_item(GameLevel& level, const std::string& slot_name); }