Made it so you can right-click on an item to use it, but yeah it's bad. Gotta refactor.
This commit is contained in:
parent
42575ef1f5
commit
ad0069e899
9 changed files with 48 additions and 61 deletions
|
@ -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 {
|
||||
|
|
|
@ -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");
|
||||
$gui.set<Sprite>(hp_gauge, {"stone_doll_cursed"});
|
||||
$gui.set<Clickable>(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<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) {
|
||||
$gui.render(window);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -15,14 +15,17 @@ namespace gui {
|
|||
}
|
||||
|
||||
if(const auto* mouse = ev->getIf<sf::Event::MouseButtonPressed>()) {
|
||||
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<sf::Event::MouseButtonReleased>()) {
|
||||
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<sf::Event::MouseMoved>()) {
|
||||
position = mouse->position;
|
||||
|
|
|
@ -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);
|
||||
|
|
36
gui/fsm.cpp
36
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<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:
|
||||
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<int>(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<DinkyECS::Entity>(data);
|
||||
|
||||
if(System::use_item($level, what)) {
|
||||
$status_ui.update();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case eGUI::DEATH: {
|
||||
$status_ui.update();
|
||||
if(entity != player.entity) {
|
||||
|
|
|
@ -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
|
||||
};
|
||||
}
|
||||
|
|
13
systems.cpp
13
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<InventoryItem>(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<InventoryItem>(entity)) {
|
||||
if(world.has<InventoryItem>(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>(Events::GUI::LOOT_ITEM, entity, *item);
|
||||
world.send<Events::GUI>(Events::GUI::LOOT_ITEM, entity, entity);
|
||||
}
|
||||
} else if(world.has<Device>(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<inventory::Model>(level.player);
|
||||
auto& player_combat = world.get<Combat>(level.player);
|
||||
|
||||
if(player_combat.hp >= player_combat.max_hp) return false;
|
||||
|
||||
auto what = inventory.get(slot_name);
|
||||
|
||||
if(auto curative = world.get_if<Curative>(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 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…
Add table
Add a link
Reference in a new issue