SpatialMap now uses unordered_multimap to allow for multiple items in a square, but they're also tagged to mark some with collision.
This commit is contained in:
parent
b193bab148
commit
f26189c696
6 changed files with 60 additions and 51 deletions
|
@ -6,46 +6,49 @@ using namespace fmt;
|
|||
using DinkyECS::Entity;
|
||||
|
||||
void SpatialMap::insert(Point pos, Entity ent, bool has_collision) {
|
||||
if(has_collision) {
|
||||
dbc::check(!yes_collision.contains(pos), "YES_collision already has entity");
|
||||
yes_collision.insert_or_assign(pos, ent);
|
||||
} else {
|
||||
dbc::check(!no_collision.contains(pos), "no_collision already has entity");
|
||||
no_collision.insert_or_assign(pos, ent);
|
||||
}
|
||||
dbc::check(!occupied(pos), "attempt to insert an entity with collision in space with collision");
|
||||
|
||||
$collision.emplace(pos, CollisionData{ent, has_collision});
|
||||
}
|
||||
|
||||
bool SpatialMap::remove(Point pos) {
|
||||
if(yes_collision.contains(pos)) {
|
||||
yes_collision.erase(pos);
|
||||
return true;
|
||||
} else {
|
||||
dbc::check(no_collision.contains(pos), "remove of entity that's not in no_collision");
|
||||
no_collision.erase(pos);
|
||||
return false;
|
||||
CollisionData SpatialMap::remove(Point pos, Entity ent) {
|
||||
auto [begin, end] = $collision.equal_range(pos);
|
||||
for(auto it = begin; it != end; ++it) {
|
||||
if(it->second.entity == ent) {
|
||||
// does the it->second go invalid after erase?
|
||||
auto copy = it->second;
|
||||
$collision.erase(it);
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
|
||||
dbc::sentinel("failed to find entity to remove");
|
||||
}
|
||||
|
||||
void SpatialMap::move(Point from, Point to, Entity ent) {
|
||||
dbc::check(!occupied(to), "attempt to move to point with an existing entity");
|
||||
bool has_collision = remove(from);
|
||||
insert(to, ent, has_collision);
|
||||
auto data = remove(from, ent);
|
||||
insert(to, ent, data.collision);
|
||||
}
|
||||
|
||||
bool SpatialMap::occupied(Point at) const {
|
||||
return yes_collision.contains(at);
|
||||
auto [begin, end] = $collision.equal_range(at);
|
||||
for(auto it = begin; it != end; ++it) {
|
||||
if(it->second.collision) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SpatialMap::something_there(Point at) const {
|
||||
return yes_collision.contains(at) || no_collision.contains(at);
|
||||
return $collision.count(at) > 0;
|
||||
}
|
||||
|
||||
Entity SpatialMap::get(Point at) const {
|
||||
if(yes_collision.contains(at)) {
|
||||
return yes_collision.at(at);
|
||||
} else {
|
||||
return no_collision.at(at);
|
||||
}
|
||||
dbc::check($collision.contains(at), "attempt to get entity when none there");
|
||||
auto [begin, end] = $collision.equal_range(at);
|
||||
return begin->second.entity;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -63,7 +66,7 @@ inline void find_neighbor(const PointEntityMap &table, EntityList &result, Point
|
|||
|
||||
auto it = table.find(cell);
|
||||
if (it != table.end()) {
|
||||
result.insert(result.end(), it->second);
|
||||
result.insert(result.end(), it->second.entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,16 +75,16 @@ 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(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
|
||||
find_neighbor($collision, result, cell, 0, 1); // north
|
||||
find_neighbor($collision, result, cell, 0, -1); // south
|
||||
find_neighbor($collision, result, cell, 1, 0); // east
|
||||
find_neighbor($collision, result, cell, -1, 0); // west
|
||||
|
||||
if(diag) {
|
||||
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
|
||||
find_neighbor($collision, result, cell, 1, -1); // south east
|
||||
find_neighbor($collision, result, cell, -1, -1); // south west
|
||||
find_neighbor($collision, result, cell, 1, 1); // north east
|
||||
find_neighbor($collision, result, cell, -1, 1); // north west
|
||||
}
|
||||
|
||||
return {!result.empty(), result};
|
||||
|
@ -94,7 +97,7 @@ inline void update_sorted(SortedEntities& sprite_distance, PointEntityMap& table
|
|||
(from.y - sprite.y) * (from.y - sprite.y);
|
||||
|
||||
if(inside < max_dist) {
|
||||
sprite_distance.push_back({inside, rec.second});
|
||||
sprite_distance.push_back({inside, rec.second.entity});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -102,8 +105,7 @@ inline void update_sorted(SortedEntities& sprite_distance, PointEntityMap& table
|
|||
SortedEntities SpatialMap::distance_sorted(Point from, int max_dist) {
|
||||
SortedEntities sprite_distance;
|
||||
|
||||
update_sorted(sprite_distance, yes_collision, from, max_dist);
|
||||
update_sorted(sprite_distance, no_collision, from, max_dist);
|
||||
update_sorted(sprite_distance, $collision, from, max_dist);
|
||||
|
||||
std::sort(sprite_distance.begin(), sprite_distance.end(), std::greater<>());
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue