Fix the last few loot bugs before actually implementing the data model for inventory and loot.

This commit is contained in:
Zed A. Shaw 2025-06-10 22:58:57 -04:00
parent 4a48910273
commit d6c5a89251
3 changed files with 25 additions and 36 deletions

View file

@ -46,21 +46,12 @@ namespace gui {
update();
}
void LootUI::remove_slot(DinkyECS::Entity slot_id) {
contents.erase(slot_id);
update();
}
void LootUI::update() {
dbc::check(contents.size() < INV_SLOTS, "too many items in loot contents, must be < 16");
for(size_t i = 0; i < INV_SLOTS; i++) {
auto id = $gui.entity("item_", int(i));
if($gui.has<guecs::Sprite>(id)) {
$gui.remove<guecs::Sprite>(id);
}
if(contents.contains(id)) {
auto item = contents.at(id);
dbc::check($level.world->has<components::Sprite>(item),
@ -73,6 +64,12 @@ namespace gui {
grabber.setSprite($gui, id);
$gui.set<guecs::GrabSource>(id, grabber);
} else {
// BUG: fix remove so it's safe to call on empty
if($gui.has<guecs::GrabSource>(id)) {
$gui.remove<guecs::Sprite>(id);
$gui.remove<guecs::GrabSource>(id);
}
$gui.set<guecs::DropTarget>(id, {
[&, id](DinkyECS::Entity world_entity) -> bool { return place_slot(id, world_entity); }
});
@ -80,6 +77,11 @@ namespace gui {
}
}
void LootUI::remove_slot(DinkyECS::Entity slot_id) {
contents.erase(slot_id);
update();
}
bool LootUI::place_slot(DinkyECS::Entity id, DinkyECS::Entity world_entity) {
if(contents.size() < INV_SLOTS && !contents.contains(id)) {
contents.try_emplace(id, world_entity);