Made it so you can right-click on an item to use it, but yeah it's bad. Gotta refactor.

master
Zed A. Shaw 2 days ago
parent 42575ef1f5
commit ad0069e899
  1. 2
      events.hpp
  2. 22
      gui/combat_ui.cpp
  3. 1
      gui/combat_ui.hpp
  4. 15
      gui/event_router.cpp
  5. 1
      gui/event_router.hpp
  6. 36
      gui/fsm.cpp
  7. 17
      gui/fsm_events.hpp
  8. 13
      systems.cpp
  9. 2
      systems.hpp

@ -7,7 +7,7 @@ namespace Events {
ATTACK, NEW_RITUAL, ATTACK, NEW_RITUAL,
UPDATE_SPRITE, ENTITY_SPAWN, NOOP, UPDATE_SPRITE, ENTITY_SPAWN, NOOP,
LOOT_ITEM, LOOT_CONTAINER, LOOT_ITEM, LOOT_CONTAINER,
LOOT_CLOSE, LOOT_SELECT, INV_SELECT, AIM_CLICK, USE_ITEM LOOT_CLOSE, LOOT_SELECT, INV_SELECT, AIM_CLICK
}; };
struct Combat { struct Combat {

@ -64,19 +64,6 @@ namespace gui {
} }
} }
auto healing_button = $gui.entity("healing_button");
auto& inventory = $level.world->get<inventory::Model>($level.player);
if(inventory.has("pocket_l")) {
$gui.set<Icon>(healing_button, {"healing_potion_small"});
$gui.set<Clickable>(healing_button, {[&](auto gui_id, auto) {
use_item(gui_id, "pocket_l");
}});
} else {
$gui.remove<Icon>(healing_button);
$gui.remove<Clickable>(healing_button);
}
auto hp_gauge = $gui.entity("hp_gauge"); auto hp_gauge = $gui.entity("hp_gauge");
$gui.set<Sprite>(hp_gauge, {"stone_doll_cursed"}); $gui.set<Sprite>(hp_gauge, {"stone_doll_cursed"});
$gui.set<Clickable>(hp_gauge, $gui.set<Clickable>(hp_gauge,
@ -85,15 +72,6 @@ namespace gui {
$gui.init(); $gui.init();
} }
void CombatUI::use_item(guecs::Entity gui_id, const string& slot) {
auto& inventory = $level.world->get<inventory::Model>($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>(Events::GUI::USE_ITEM, gui_id, {healing_item});
}
void CombatUI::render(sf::RenderWindow& window) { void CombatUI::render(sf::RenderWindow& window) {
$gui.render(window); $gui.render(window);
} }

@ -20,6 +20,5 @@ namespace gui {
guecs::Entity make_button(std::string name, Events::GUI event, guecs::Entity make_button(std::string name, Events::GUI event,
int action, const std::string &icon_name, int action, const std::string &icon_name,
const std::string &sound, const std::string &effect_name); const std::string &sound, const std::string &effect_name);
void use_item(guecs::Entity gui_id, const string& slot);
}; };
} }

@ -15,14 +15,17 @@ namespace gui {
} }
if(const auto* mouse = ev->getIf<sf::Event::MouseButtonPressed>()) { if(const auto* mouse = ev->getIf<sf::Event::MouseButtonPressed>()) {
if(mouse->button == sf::Mouse::Button::Left) { if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) {
position = mouse->position; left_button = mouse->button == sf::Mouse::Button::Left;
event(MOUSE_DOWN); position = mouse->position;
event(MOUSE_DOWN);
} }
} else if(const auto* mouse = ev->getIf<sf::Event::MouseButtonReleased>()) { } else if(const auto* mouse = ev->getIf<sf::Event::MouseButtonReleased>()) {
if(mouse->button == sf::Mouse::Button::Left) { // need to sort this out but if you don't do this it thinks you're always pressing it
position = mouse->position; if(mouse->button == sf::Mouse::Button::Left || mouse->button == sf::Mouse::Button::Right) {
event(MOUSE_UP); left_button = mouse->button == sf::Mouse::Button::Left;
position = mouse->position;
event(MOUSE_UP);
} }
} else if(const auto* mouse = ev->getIf<sf::Event::MouseMoved>()) { } else if(const auto* mouse = ev->getIf<sf::Event::MouseMoved>()) {
position = mouse->position; position = mouse->position;

@ -28,6 +28,7 @@ namespace gui {
sf::Keyboard::Scancode scancode; sf::Keyboard::Scancode scancode;
gui::Event $next_event = gui::Event::TICK; gui::Event $next_event = gui::Event::TICK;
int move_count = 0; int move_count = 0;
bool left_button = true;
int $drag_tolerance = 4; int $drag_tolerance = 4;
void event(Event ev); void event(Event ev);

@ -192,7 +192,16 @@ namespace gui {
$dnd_loot.event(Event::INV_SELECT, data); $dnd_loot.event(Event::INV_SELECT, data);
state(State::LOOTING); state(State::LOOTING);
break; break;
case USE_ITEM: {
auto gui_id = std::any_cast<guecs::Entity>(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: case MOUSE_CLICK:
fmt::println("CLICK: {}", $router.left_button);
mouse_action(false); mouse_action(false);
break; break;
case MOUSE_MOVE: case MOUSE_MOVE:
@ -200,6 +209,7 @@ namespace gui {
break; break;
case AIM_CLICK: case AIM_CLICK:
System::pickup($level); System::pickup($level);
break;
default: default:
break; // ignore everything else break; // ignore everything else
} }
@ -470,9 +480,13 @@ namespace gui {
case eGUI::LOOT_SELECT: case eGUI::LOOT_SELECT:
event(Event::LOOT_SELECT, data); event(Event::LOOT_SELECT, data);
break; break;
case eGUI::INV_SELECT: case eGUI::INV_SELECT: {
event(Event::INV_SELECT, data); if($router.left_button) {
break; event(Event::INV_SELECT, data);
} else {
event(Event::USE_ITEM, data);
}
} break;
case eGUI::AIM_CLICK: case eGUI::AIM_CLICK:
event(Event::AIM_CLICK); event(Event::AIM_CLICK);
break; break;
@ -488,11 +502,11 @@ namespace gui {
event(Event::LOOT_OPEN); event(Event::LOOT_OPEN);
} break; } break;
case eGUI::HP_STATUS: case eGUI::HP_STATUS:
System::player_status($level); System::player_status($level);
break; break;
case eGUI::NEW_RITUAL: case eGUI::NEW_RITUAL:
$combat_ui.init(); $combat_ui.init();
break; break;
case eGUI::ATTACK: case eGUI::ATTACK:
$temp_attack_id = std::any_cast<int>(data); $temp_attack_id = std::any_cast<int>(data);
event(Event::ATTACK); event(Event::ATTACK);
@ -500,14 +514,6 @@ namespace gui {
case eGUI::STAIRS_DOWN: case eGUI::STAIRS_DOWN:
event(Event::STAIRS_DOWN); event(Event::STAIRS_DOWN);
break; break;
case eGUI::USE_ITEM: {
auto what = std::any_cast<DinkyECS::Entity>(data);
if(System::use_item($level, what)) {
$status_ui.update();
}
break;
}
case eGUI::DEATH: { case eGUI::DEATH: {
$status_ui.update(); $status_ui.update();
if(entity != player.entity) { if(entity != player.entity) {

@ -20,13 +20,14 @@ namespace gui {
LOOT_ITEM=15, LOOT_ITEM=15,
LOOT_SELECT=16, LOOT_SELECT=16,
INV_SELECT=17, INV_SELECT=17,
QUIT = 18, USE_ITEM=18,
MOUSE_CLICK=19, QUIT = 19,
MOUSE_MOVE=20, MOUSE_CLICK=20,
MOUSE_DRAG=21, MOUSE_MOVE=21,
MOUSE_DRAG_START=22, MOUSE_DRAG=22,
MOUSE_DROP=23, MOUSE_DRAG_START=23,
KEY_PRESS=24, MOUSE_DROP=24,
AIM_CLICK=25 KEY_PRESS=25,
AIM_CLICK=26
}; };
} }

@ -320,10 +320,7 @@ void System::pickup(GameLevel &level) {
auto &collision = *level.collision; auto &collision = *level.collision;
auto pos = player_position(level); auto pos = player_position(level);
if(!collision.something_there(pos.aiming_at)) { if(!collision.something_there(pos.aiming_at)) return;
dbc::log("nothing there");
return;
}
auto entity = level.collision->find(pos.aiming_at, [&](auto data) -> bool { auto entity = level.collision->find(pos.aiming_at, [&](auto data) -> bool {
return (world.has<InventoryItem>(data.entity) || return (world.has<InventoryItem>(data.entity) ||
@ -336,7 +333,7 @@ void System::pickup(GameLevel &level) {
} }
// use spatial find to find an item with inventory... // use spatial find to find an item with inventory...
if(auto item = world.get_if<InventoryItem>(entity)) { if(world.has<InventoryItem>(entity)) {
// NOTE: this might need to be a separate system so that people can leave stuff alone // NOTE: this might need to be a separate system so that people can leave stuff alone
remove_from_world(level, entity); 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 // 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 // GUI figures out which it is, then when you click either pick it up
// and move it or show the loot container UI // and move it or show the loot container UI
world.send<Events::GUI>(Events::GUI::LOOT_ITEM, entity, *item); world.send<Events::GUI>(Events::GUI::LOOT_ITEM, entity, entity);
} }
} else if(world.has<Device>(entity)) { } else if(world.has<Device>(entity)) {
System::device(world, level.player, entity); System::device(world, level.player, entity);
@ -591,13 +588,15 @@ void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture
render.display(); 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& world = *level.world;
auto& inventory = world.get<inventory::Model>(level.player); auto& inventory = world.get<inventory::Model>(level.player);
auto& player_combat = world.get<Combat>(level.player); auto& player_combat = world.get<Combat>(level.player);
if(player_combat.hp >= player_combat.max_hp) return false; if(player_combat.hp >= player_combat.max_hp) return false;
auto what = inventory.get(slot_name);
if(auto curative = world.get_if<Curative>(what)) { if(auto curative = world.get_if<Curative>(what)) {
inventory.remove(what); inventory.remove(what);

@ -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 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); 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);
} }

Loading…
Cancel
Save