Standardized on using only DinkyECS:Entity for most inventory:::Model operations, then create swap based on the same entities.

This commit is contained in:
Zed A. Shaw 2025-07-02 14:25:44 -04:00
parent 8c8d6dc9e7
commit 784f753e72
6 changed files with 62 additions and 53 deletions

View file

@ -1,7 +1,7 @@
#include "inventory.hpp"
namespace inventory {
bool Model::add(const Slot &in_slot, DinkyECS::Entity ent) {
bool Model::add(const std::string &in_slot, DinkyECS::Entity ent) {
invariant();
if(by_slot.contains(in_slot)) return false;
@ -12,11 +12,11 @@ namespace inventory {
return true;
}
Slot& Model::get(DinkyECS::Entity ent) {
const std::string& Model::get(DinkyECS::Entity ent) {
return by_entity.at(ent);
}
DinkyECS::Entity Model::get(const Slot& slot) {
DinkyECS::Entity Model::get(const std::string& slot) {
return by_slot.at(slot);
}
@ -24,25 +24,14 @@ namespace inventory {
return by_entity.contains(ent);
}
bool Model::has(const Slot& slot) {
bool Model::has(const std::string& slot) {
return by_slot.contains(slot);
}
void Model::remove(const Slot& slot, DinkyECS::Entity ent) {
by_entity.erase(ent);
by_slot.erase(slot);
invariant();
}
void Model::remove(DinkyECS::Entity ent) {
auto& slot = by_entity.at(ent);
remove(slot, ent);
invariant();
}
void Model::remove(const Slot& slot) {
auto ent = by_slot.at(slot);
remove(slot, ent);
by_entity.erase(ent);
by_slot.erase(slot);
invariant();
}
@ -70,4 +59,28 @@ namespace inventory {
slot, ent, by_entity.contains(ent), by_entity.at(ent) == slot);
}
}
void Model::swap(DinkyECS::Entity a_ent, DinkyECS::Entity b_ent) {
dbc::check(by_entity.contains(a_ent), "a_entity not in inventory");
dbc::check(by_entity.contains(b_ent), "b_entity not in inventory");
if(a_ent == b_ent) return;
auto a_slot = get(a_ent);
auto b_slot = get(b_ent);
dbc::check(a_slot != b_slot, "somehow I got two different entities but they gave the same slot?");
by_slot.insert_or_assign(a_slot, b_ent);
by_entity.insert_or_assign(b_ent, a_slot);
by_slot.insert_or_assign(b_slot, a_ent);
by_entity.insert_or_assign(a_ent, b_slot);
}
size_t Model::count() {
dbc::check(by_slot.size() == by_entity.size(), "entity and slot maps have different sizes");
return by_entity.size();
}
}