Tracked down the bug that was caused by picking up an item but not removing its Position in the world, so when you go to another level it gets brought back to life causing a dupe.

This commit is contained in:
Zed A. Shaw 2025-06-22 12:50:09 -04:00
parent 2c6565c40a
commit e0588847fa
7 changed files with 62 additions and 5 deletions

View file

@ -102,6 +102,8 @@ void System::enemy_pathing(GameLevel &level) {
}
void System::init_positions(World &world, SpatialMap &collider) {
auto& inv = world.get_the<inventory::Model>();
world.query<Position>([&](auto ent, auto &pos) {
if(world.has<Combat>(ent)) {
const auto& combat = world.get<Combat>(ent);
@ -109,6 +111,10 @@ void System::init_positions(World &world, SpatialMap &collider) {
collider.insert(pos.location, ent);
}
} else {
fmt::println("System::init_positions for ent={}", ent);
dbc::check(!inv.has(ent),
fmt::format("!!! Entity {} is in player inventory and _also_ has a position in the world.", ent));
collider.insert(pos.location, ent);
}
});
@ -326,6 +332,9 @@ void System::pickup(GameLevel &level, Entity entity) {
auto& item_pos = world.get<Position>(entity);
level.collision->remove(item_pos.location);
world.remove<Tile>(entity);
// if you don't do this you get the bug that you can pickup
// an item and it'll also be in your inventory
world.remove<Position>(entity);
if(world.has<ritual::JunkPile>(entity)) {
auto& pile = world.get<ritual::JunkPile>(entity);
@ -499,16 +508,21 @@ bool System::drop_item(GameLevel& level, Entity item) {
return false;
}
// NOTE: I tink pickup and this need to be different
bool System::place_in_container(World& world, Entity cont_id, const std::string& name, Entity world_entity) {
auto& container = world.get<inventory::Model>(cont_id);
if(container.has(world_entity)) {
fmt::println("container {} already has entity {}, skip", cont_id, world_entity);
// NOTE: I think this would be a move?!
return false;
} else if(container.has(name)) {
// this is an already occupied slot
fmt::println("container {} already has SLOT {}, skip", cont_id, name);
return false;
} else {
// this should only apply to the player's inventory
fmt::println("adding {} entity to loot with name {}", world_entity, name);
container.add(name, world_entity);
return true;
}