Prep for fixing the spatialmap to allow for entities without collision to still be in the space.

This commit is contained in:
Zed A. Shaw 2025-07-29 02:13:29 -04:00
parent d93bc1615c
commit fd53f92fe6
7 changed files with 27 additions and 53 deletions

View file

@ -6,11 +6,11 @@ using namespace fmt;
using DinkyECS::Entity;
void SpatialMap::insert(Point pos, Entity ent) {
table[pos] = ent;
yes_collision.insert_or_assign(pos, ent);
}
void SpatialMap::remove(Point pos) {
table.erase(pos);
yes_collision.erase(pos);
}
void SpatialMap::move(Point from, Point to, Entity ent) {
@ -19,11 +19,11 @@ void SpatialMap::move(Point from, Point to, Entity ent) {
}
bool SpatialMap::occupied(Point at) const {
return table.contains(at);
return yes_collision.contains(at);
}
Entity SpatialMap::get(Point at) const {
return table.at(at);
return yes_collision.at(at);
}
/*
@ -50,24 +50,22 @@ FoundEntities SpatialMap::neighbors(Point cell, bool diag) const {
// just unroll the loop since we only check four directions
// this also solves the problem that it was detecting that the cell was automatically included as a "neighbor" but it's not
find_neighbor(table, result, cell, 0, 1); // north
find_neighbor(table, result, cell, 0, -1); // south
find_neighbor(table, result, cell, 1, 0); // east
find_neighbor(table, result, cell, -1, 0); // west
find_neighbor(yes_collision, result, cell, 0, 1); // north
find_neighbor(yes_collision, result, cell, 0, -1); // south
find_neighbor(yes_collision, result, cell, 1, 0); // east
find_neighbor(yes_collision, result, cell, -1, 0); // west
if(diag) {
find_neighbor(table, result, cell, 1, -1); // south east
find_neighbor(table, result, cell, -1, -1); // south west
find_neighbor(table, result, cell, 1, 1); // north east
find_neighbor(table, result, cell, -1, 1); // north west
find_neighbor(yes_collision, result, cell, 1, -1); // south east
find_neighbor(yes_collision, result, cell, -1, -1); // south west
find_neighbor(yes_collision, result, cell, 1, 1); // north east
find_neighbor(yes_collision, result, cell, -1, 1); // north west
}
return {!result.empty(), result};
}
SortedEntities SpatialMap::distance_sorted(Point from, int max_dist) {
SortedEntities sprite_distance;
inline void update_sorted(SortedEntities& sprite_distance, PointEntityMap& table, Point from, int max_dist) {
for(const auto &rec : table) {
Point sprite = rec.first;
int inside = (from.x - sprite.x) * (from.x - sprite.x) +
@ -77,6 +75,12 @@ SortedEntities SpatialMap::distance_sorted(Point from, int max_dist) {
sprite_distance.push_back({inside, rec.second});
}
}
}
SortedEntities SpatialMap::distance_sorted(Point from, int max_dist) {
SortedEntities sprite_distance;
update_sorted(sprite_distance, yes_collision, from, max_dist);
std::sort(sprite_distance.begin(), sprite_distance.end(), std::greater<>());