Implemented a simple collision hash table.
This commit is contained in:
parent
dbc2a10933
commit
743f906bc7
7 changed files with 174 additions and 1 deletions
38
collider.cpp
Normal file
38
collider.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include "collider.hpp"
|
||||
|
||||
using DinkyECS::Entity;
|
||||
|
||||
void SpatialHashTable::insert(Point pos, Entity ent) {
|
||||
table[pos] = ent;
|
||||
}
|
||||
|
||||
void SpatialHashTable::remove(Point pos) {
|
||||
table.erase(pos);
|
||||
}
|
||||
|
||||
void SpatialHashTable::move(Point from, Point to, Entity ent) {
|
||||
remove(from);
|
||||
insert(to, ent);
|
||||
}
|
||||
|
||||
bool SpatialHashTable::occupied(Point at) {
|
||||
return table[at];
|
||||
}
|
||||
|
||||
std::tuple<bool, FoundList> SpatialHashTable::neighbors(Point cell) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return std::tuple(!result.empty(), result);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue