Big BIG refactor to make inventory use a model that's placed into the world, following a more sane MVC style.

This commit is contained in:
Zed A. Shaw 2025-06-20 13:17:12 -04:00
parent 119b3ed11d
commit a0eff927b6
21 changed files with 270 additions and 123 deletions

View file

@ -40,7 +40,10 @@ namespace gui {
make_button("destroy", L"DESTROY", Events::GUI::LOOT_CLOSE);
for(int i = 0; i < INV_SLOTS; i++) {
auto id = $gui.entity("item_", 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 });
$gui.set<guecs::Effect>(id, {0.4f, "ui_shader"});
@ -54,13 +57,12 @@ namespace gui {
}
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));
auto& slot_name = $slot_to_name.at(id);
if(contents.contains(id)) {
auto item = contents.at(id);
if(contents.has(slot_name)) {
auto item = contents.get(slot_name);
dbc::check($level.world->has<components::Sprite>(item),
"item in inventory UI doesn't exist in world. New level?");
auto& sprite = $level.world->get<components::Sprite>(item);
@ -85,13 +87,16 @@ namespace gui {
}
void LootUI::remove_slot(DinkyECS::Entity slot_id) {
contents.erase(slot_id);
auto& name = $slot_to_name.at(slot_id);
contents.remove(name);
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);
bool LootUI::place_slot(guecs::Entity id, DinkyECS::Entity world_entity) {
auto& name = $slot_to_name.at(id);
if(!contents.has(name)) {
contents.add(name, world_entity);
update();
return true;
} else {
@ -105,7 +110,6 @@ namespace gui {
void LootUI::update_level(GameLevel &level) {
$level = level;
contents.clear();
init();
}