|
|
@ -6,11 +6,11 @@ using namespace fmt; |
|
|
|
using DinkyECS::Entity; |
|
|
|
using DinkyECS::Entity; |
|
|
|
|
|
|
|
|
|
|
|
void SpatialMap::insert(Point pos, Entity ent) { |
|
|
|
void SpatialMap::insert(Point pos, Entity ent) { |
|
|
|
table[pos] = ent; |
|
|
|
yes_collision.insert_or_assign(pos, ent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SpatialMap::remove(Point pos) { |
|
|
|
void SpatialMap::remove(Point pos) { |
|
|
|
table.erase(pos); |
|
|
|
yes_collision.erase(pos); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SpatialMap::move(Point from, Point to, Entity ent) { |
|
|
|
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 { |
|
|
|
bool SpatialMap::occupied(Point at) const { |
|
|
|
return table.contains(at); |
|
|
|
return yes_collision.contains(at); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Entity SpatialMap::get(Point at) const { |
|
|
|
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
|
|
|
|
// 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
|
|
|
|
// 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(yes_collision, result, cell, 0, 1); // north
|
|
|
|
find_neighbor(table, result, cell, 0, -1); // south
|
|
|
|
find_neighbor(yes_collision, result, cell, 0, -1); // south
|
|
|
|
find_neighbor(table, result, cell, 1, 0); // east
|
|
|
|
find_neighbor(yes_collision, result, cell, 1, 0); // east
|
|
|
|
find_neighbor(table, result, cell, -1, 0); // west
|
|
|
|
find_neighbor(yes_collision, result, cell, -1, 0); // west
|
|
|
|
|
|
|
|
|
|
|
|
if(diag) { |
|
|
|
if(diag) { |
|
|
|
find_neighbor(table, result, cell, 1, -1); // south east
|
|
|
|
find_neighbor(yes_collision, result, cell, 1, -1); // south east
|
|
|
|
find_neighbor(table, result, cell, -1, -1); // south west
|
|
|
|
find_neighbor(yes_collision, result, cell, -1, -1); // south west
|
|
|
|
find_neighbor(table, result, cell, 1, 1); // north east
|
|
|
|
find_neighbor(yes_collision, result, cell, 1, 1); // north east
|
|
|
|
find_neighbor(table, result, cell, -1, 1); // north west
|
|
|
|
find_neighbor(yes_collision, result, cell, -1, 1); // north west
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return {!result.empty(), result}; |
|
|
|
return {!result.empty(), result}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SortedEntities SpatialMap::distance_sorted(Point from, int max_dist) { |
|
|
|
inline void update_sorted(SortedEntities& sprite_distance, PointEntityMap& table, Point from, int max_dist) { |
|
|
|
SortedEntities sprite_distance; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(const auto &rec : table) { |
|
|
|
for(const auto &rec : table) { |
|
|
|
Point sprite = rec.first; |
|
|
|
Point sprite = rec.first; |
|
|
|
int inside = (from.x - sprite.x) * (from.x - sprite.x) + |
|
|
|
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}); |
|
|
|
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<>()); |
|
|
|
std::sort(sprite_distance.begin(), sprite_distance.end(), std::greater<>()); |
|
|
|
|
|
|
|
|
|
|
|