From 6ff191958724d5e2a9213d04bc10882e99bfc8ea Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 24 Jun 2025 13:23:55 -0400 Subject: [PATCH] Cleaned up the move operation more so that I can use it in the other places that I need it. --- gui/dnd_loot.cpp | 46 +++++++++++++++++++++++++++++++++++++++------- gui/dnd_loot.hpp | 6 ++++++ gui/status_ui.cpp | 2 +- inventory.hpp | 4 ++++ systems.cpp | 2 ++ 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/gui/dnd_loot.cpp b/gui/dnd_loot.cpp index 3528a45..3dbd9c2 100644 --- a/gui/dnd_loot.cpp +++ b/gui/dnd_loot.cpp @@ -101,17 +101,18 @@ namespace gui { void DNDLoot::INV_PICKUP(Event ev, std::any data) { using enum Event; - (void)data; switch(ev) { case AIM_CLICK: { - fmt::println("IN INV_PICKUP AIM CLICK!"); - auto& grab = $status_ui.$gui.get(*$grab_source); - grab.commit(); - bool dropped = $status_ui.drop_item(grab.world_entity); - dbc::check(dropped, "DROP FAILED!"); + bool worked = throw_on_floor(); + dbc::check(worked, "Need to fix this, should be able to abort."); + END(Event::CLOSE); + } break; + case INV_SELECT: + // BUG: should I do a bool here and not transition? + commit_move($status_ui.$gui, $grab_source, data); END(Event::CLOSE); - } break; + break; default: handle_mouse(ev, $status_ui.$gui); } @@ -228,6 +229,13 @@ namespace gui { { if(!source_id) return false; auto target_id = std::any_cast(data); + fmt::println("!!!!!!!!!source_id = {} target_id = {}", + *source_id, target_id); + + dbc::check(target.has(target_id), + "gui does not have a DropTarget at that slot"); + dbc::check(source.has(*source_id), + "gui does not have a GrabSource at that slot"); auto& drop = target.get(target_id); auto& grab = source.get(*source_id); @@ -239,4 +247,28 @@ namespace gui { return false; } } + + void DNDLoot::commit_move(guecs::UI& gui, std::optional source_id, std::any data) { + auto& grab = gui.get(*source_id); + grab.commit(); + + auto drop_id = std::any_cast(data); + auto& drop = gui.get(drop_id); + drop.commit(grab.world_entity); + + // BUG: if the drop fails then need to put the grab back? + // How to confirm the drop will work before doing it? + // Or, maybe save the commit? + } + + /* + * Dropping on the ground is only possible from the + * status_ui for now. + */ + bool DNDLoot::throw_on_floor() { + dbc::check($grab_source != std::nullopt, "attempt to commit_drop but no grab_source set"); + auto& grab = $status_ui.$gui.get(*$grab_source); + grab.commit(); + return $status_ui.drop_item(grab.world_entity); + } } diff --git a/gui/dnd_loot.hpp b/gui/dnd_loot.hpp index 7c525fb..94b9057 100644 --- a/gui/dnd_loot.hpp +++ b/gui/dnd_loot.hpp @@ -45,9 +45,15 @@ namespace gui { void close(); std::optional start_grab(guecs::UI& gui, std::any data); + bool commit_drop(guecs::UI& source, guecs::UI& target, std::optional source_id, std::any data); + void commit_move(guecs::UI& gui, + std::optional source_id, std::any data); + + bool throw_on_floor(); + sf::Vector2f mouse_position(); }; } diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index 1774770..d8248e5 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -117,7 +117,6 @@ namespace gui { bool StatusUI::drop_item(DinkyECS::Entity item_id) { bool dropped = System::drop_item($level, item_id); - $level.world->not_constant(item_id); if(dropped) update(); return dropped; } @@ -128,6 +127,7 @@ namespace gui { // ground or moving from one container or another, so when loot_ui // moves to use an ECS id to a container I can have the System // do it. + dbc::log(fmt::format("removing slot: {}", slot_id)); auto& slot_name = $slot_to_name.at(slot_id); auto& inventory = $level.world->get_the(); inventory.remove(slot_name); diff --git a/inventory.hpp b/inventory.hpp index bcb7e44..f616902 100644 --- a/inventory.hpp +++ b/inventory.hpp @@ -1,6 +1,10 @@ #include "dinkyecs.hpp" #include +// BUG: this should have a bool for "permanent" or "constant" so that +// everything working with it knows to do the make_constant/not_constant +// dance when using it. Idea is the System:: ops for this would get it +// and then look at the bool and add the constant ops as needed. namespace inventory { using Slot = std::string; diff --git a/systems.cpp b/systems.cpp index 083b0f2..bf6cc9c 100644 --- a/systems.cpp +++ b/systems.cpp @@ -509,12 +509,14 @@ bool System::drop_item(GameLevel& level, Entity item) { for(matrix::box it{map.walls(), player_pos.location.x, player_pos.location.y, 1}; it.next();) { Position pos{it.x, it.y}; + if(map.can_move(pos.location) && !collision.occupied(pos.location)) { world.set(item, pos); collision.insert(pos.location, item); // BUG: really there should be another system that handles loot->inv moves if(player_inv.has(item)) player_inv.remove(item); level.world->send(Events::GUI::ENEMY_SPAWN, item, {}); + level.world->not_constant(item); return true; } }