Playing around with it some more to see how a move would work.
This commit is contained in:
parent
98baa13264
commit
98993481b0
1 changed files with 69 additions and 33 deletions
|
@ -1,18 +1,21 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <fmt/core.h>
|
||||||
|
|
||||||
|
using namespace fmt;
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
int x, y;
|
int x, y;
|
||||||
|
|
||||||
bool operator==(const Point& other) const {
|
bool operator==(const Point& other) const {
|
||||||
return other.x == x && other.y == y;
|
return other.x == x && other.y == y;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Object {
|
struct Object {
|
||||||
Point position;
|
Point position;
|
||||||
// Add other object data as needed
|
// Add other object data as needed
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PointHash {
|
struct PointHash {
|
||||||
|
@ -22,49 +25,82 @@ struct PointHash {
|
||||||
};
|
};
|
||||||
|
|
||||||
class SpatialHashTable {
|
class SpatialHashTable {
|
||||||
public:
|
public:
|
||||||
SpatialHashTable() {}
|
SpatialHashTable() {}
|
||||||
|
|
||||||
void insert(Object* obj) {
|
void insert(Object* obj) {
|
||||||
table[obj->position].push_back(obj);
|
table[obj->position].push_back(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(Object* obj) {
|
||||||
|
table.erase(obj->position);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Object*> getNearbyObjects(Point position) {
|
std::vector<Object*> getNearbyObjects(Point position) {
|
||||||
std::vector<Object*> result;
|
std::vector<Object*> result;
|
||||||
Point cell = position;
|
Point cell = position;
|
||||||
|
|
||||||
// Check the current cell and its 8 neighbors
|
// Check the current cell and its 8 neighbors
|
||||||
for (int x = cell.x - 1; x <= cell.x + 1; x++) {
|
for (int x = cell.x - 1; x <= cell.x + 1; x++) {
|
||||||
for (int y = cell.y - 1; y <= cell.y + 1; y++) {
|
for (int y = cell.y - 1; y <= cell.y + 1; y++) {
|
||||||
Point neighborCell = {x, y};
|
Point neighborCell = {x, y};
|
||||||
auto it = table.find(neighborCell);
|
auto it = table.find(neighborCell);
|
||||||
if (it != table.end()) {
|
if (it != table.end()) {
|
||||||
result.insert(result.end(), it->second.begin(), it->second.end());
|
result.insert(result.end(), it->second.begin(), it->second.end());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<Point, std::vector<Object*>, PointHash> table;
|
std::unordered_map<Point, std::vector<Object*>, PointHash> table;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SpatialHashTable hashTable;
|
SpatialHashTable hashTable;
|
||||||
Object obj1 = {{5, 5}};
|
Object obj1 = {{5, 5}};
|
||||||
Object obj2 = {{15, 15}};
|
Object obj2 = {{15, 15}};
|
||||||
Object bomb = {{25, 25}};
|
Object bomb = {{25, 25}};
|
||||||
|
|
||||||
hashTable.insert(&obj1);
|
hashTable.insert(&obj1);
|
||||||
hashTable.insert(&obj2);
|
hashTable.insert(&obj2);
|
||||||
hashTable.insert(&bomb);
|
hashTable.insert(&bomb);
|
||||||
|
|
||||||
std::vector<Object*> nearby = hashTable.getNearbyObjects({24, 24});
|
std::vector<Object*> nearby = hashTable.getNearbyObjects({24, 24});
|
||||||
for (Object* obj : nearby) {
|
|
||||||
std::cout << obj->position.x << ", " << obj->position.y << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
for (Object* obj : nearby) {
|
||||||
|
println("{},{}", obj->position.x, obj->position.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
println("AFTER MOVE");
|
||||||
|
|
||||||
|
// now attempt a move
|
||||||
|
hashTable.remove(&bomb);
|
||||||
|
bomb.position.x += 1;
|
||||||
|
bomb.position.y += 1;
|
||||||
|
hashTable.insert(&bomb);
|
||||||
|
|
||||||
|
nearby = hashTable.getNearbyObjects({24, 24});
|
||||||
|
|
||||||
|
for (Object* obj : nearby) {
|
||||||
|
println("{},{}", obj->position.x, obj->position.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
println("AFTER MOVE BACK");
|
||||||
|
|
||||||
|
// now attempt a move
|
||||||
|
hashTable.remove(&bomb);
|
||||||
|
bomb.position.x -= 3;
|
||||||
|
bomb.position.y -= 2;
|
||||||
|
hashTable.insert(&bomb);
|
||||||
|
|
||||||
|
nearby = hashTable.getNearbyObjects({24, 24});
|
||||||
|
|
||||||
|
for (Object* obj : nearby) {
|
||||||
|
println("{},{}", obj->position.x, obj->position.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue