Now using a simple collision map to track entities and then determine if they're near the player for attacking.
This commit is contained in:
parent
743f906bc7
commit
ec1ed23c52
6 changed files with 72 additions and 48 deletions
35
collider.cpp
35
collider.cpp
|
@ -15,23 +15,32 @@ void SpatialHashTable::move(Point from, Point to, Entity ent) {
|
|||
insert(to, ent);
|
||||
}
|
||||
|
||||
bool SpatialHashTable::occupied(Point at) {
|
||||
return table[at];
|
||||
bool SpatialHashTable::occupied(Point at) const {
|
||||
return table.contains(at);
|
||||
}
|
||||
|
||||
std::tuple<bool, FoundList> SpatialHashTable::neighbors(Point cell) {
|
||||
inline void find_neighbor(const PointEntityMap &table, FoundList &result, Point at) {
|
||||
auto it = table.find(at);
|
||||
if (it != table.end()) {
|
||||
result.insert(result.end(), it->second);
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<bool, FoundList> SpatialHashTable::neighbors(Point cell, bool diag) const {
|
||||
FoundList result;
|
||||
|
||||
// Check the current cell and its 8 neighbors
|
||||
// BUG: this can sign underflow, assert it won't
|
||||
for (size_t x = cell.x - 1; x <= cell.x + 1; x++) {
|
||||
for (size_t y = cell.y - 1; y <= cell.y + 1; y++) {
|
||||
Point neighborCell = {x, y};
|
||||
auto it = table.find(neighborCell);
|
||||
if (it != table.end()) {
|
||||
result.insert(result.end(), it->second);
|
||||
}
|
||||
}
|
||||
// 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.x, cell.y+1}); // north
|
||||
find_neighbor(table, result, {cell.x, cell.y-1}); // south
|
||||
find_neighbor(table, result, {cell.x+1, cell.y}); // east
|
||||
find_neighbor(table, result, {cell.x-1, cell.y}); // west
|
||||
find_neighbor(table, result, {cell.x+1, cell.y-1}); // south east
|
||||
|
||||
if(diag) {
|
||||
find_neighbor(table, result, {cell.x-1, cell.y-1}); // south west
|
||||
find_neighbor(table, result, {cell.x+1, cell.y+1}); // north east
|
||||
find_neighbor(table, result, {cell.x-1, cell.y+1}); // north west
|
||||
}
|
||||
|
||||
return std::tuple(!result.empty(), result);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue