The next little game in the series where I make a fancy rogue game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
roguish/collider.cpp

38 lines
887 B

#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);
}