Finally have inventory not crashing for most edge cases. This solves many bugs but mostly closes #58.

This commit is contained in:
Zed A. Shaw 2025-07-07 13:25:17 -04:00
parent 601f3331ed
commit f64b202ee7
9 changed files with 90 additions and 43 deletions

View file

@ -47,7 +47,6 @@ namespace gui {
for(int i = 0; i < INV_SLOTS; i++) {
auto name = fmt::format("item_{}", i);
auto id = $gui.entity(name);
$slot_to_name.insert_or_assign(id, name);
$gui.set<guecs::Rectangle>(id, {THEME.PADDING,
THEME.TRANSPARENT, THEME.LIGHT_MID });
@ -69,7 +68,7 @@ namespace gui {
for(size_t i = 0; i < INV_SLOTS; i++) {
auto id = $gui.entity("item_", int(i));
auto& slot_name = $slot_to_name.at(id);
auto& slot_name = $gui.name_for(id);
if(contents.has(slot_name)) {
auto item = contents.get(slot_name);
@ -97,7 +96,7 @@ namespace gui {
}
void LootUI::remove_slot(guecs::Entity slot_id) {
auto& name = $slot_to_name.at(slot_id);
auto& name = $gui.name_for(slot_id);
fmt::println("LootUI remove slot inv::Model id={} slot={}", $target, name);
System::remove_from_container(*$level.world, $target, name);
update();
@ -106,7 +105,8 @@ namespace gui {
bool LootUI::place_slot(guecs::Entity id, DinkyECS::Entity world_entity) {
fmt::println("LootUI target={} placing world entity {} in slot id {}",
$target, id, world_entity);
auto& name = $slot_to_name.at(id);
auto& name = $gui.name_for(id);
bool worked = System::place_in_container(*$level.world, $target, name, world_entity);
if(worked) update();
return worked;
@ -136,4 +136,18 @@ namespace gui {
bool LootUI::mouse(float x, float y, bool hover) {
return $gui.mouse(x, y, hover);
}
bool LootUI::occupied(guecs::Entity slot) {
return System::inventory_occupied($level, $target, $gui.name_for(slot));
}
void LootUI::swap(guecs::Entity gui_a, guecs::Entity gui_b) {
if(gui_a != gui_b) {
auto& a_name = $gui.name_for(gui_a);
auto& b_name = $gui.name_for(gui_b);
System::inventory_swap($level, $target, a_name, b_name);
}
update();
}
}