Implemented a simple collision hash table.
This commit is contained in:
parent
dbc2a10933
commit
743f906bc7
7 changed files with 174 additions and 1 deletions
95
tests/collider.cpp
Normal file
95
tests/collider.cpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <fmt/core.h>
|
||||
#include <string>
|
||||
#include "collider.hpp"
|
||||
#include "dinkyecs.hpp"
|
||||
|
||||
using DinkyECS::Entity;
|
||||
using namespace fmt;
|
||||
|
||||
TEST_CASE("confirm basic collision operations", "[collision]") {
|
||||
DinkyECS::World world;
|
||||
Entity player = world.entity();
|
||||
Entity enemy = world.entity();
|
||||
|
||||
SpatialHashTable coltable;
|
||||
coltable.insert({11,11}, player);
|
||||
coltable.insert({21,21}, enemy);
|
||||
|
||||
{ // not found
|
||||
auto [found, nearby] = coltable.neighbors({1,1});
|
||||
REQUIRE(!found);
|
||||
REQUIRE(nearby.empty());
|
||||
}
|
||||
|
||||
{ // found
|
||||
auto [found, nearby] = coltable.neighbors({10,10});
|
||||
|
||||
REQUIRE(found);
|
||||
REQUIRE(nearby[0] == player);
|
||||
}
|
||||
|
||||
{ // removed
|
||||
coltable.remove({11,11});
|
||||
auto [found, nearby] = coltable.neighbors({10,10});
|
||||
REQUIRE(!found);
|
||||
REQUIRE(nearby.empty());
|
||||
}
|
||||
|
||||
coltable.insert({11,11}, player); // setup for the move test
|
||||
|
||||
{ // moving
|
||||
coltable.move({11,11}, {12, 12}, player);
|
||||
auto [found, nearby] = coltable.neighbors({10,10});
|
||||
REQUIRE(!found);
|
||||
REQUIRE(nearby.empty());
|
||||
}
|
||||
|
||||
{ // find it after move
|
||||
auto [found, nearby] = coltable.neighbors({11,11});
|
||||
REQUIRE(found);
|
||||
REQUIRE(nearby[0] == player);
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(coltable.occupied({12,12}));
|
||||
REQUIRE(coltable.occupied({21,21}));
|
||||
REQUIRE(!coltable.occupied({1,10}));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST_CASE("confirm multiple entities moving", "[collision]") {
|
||||
DinkyECS::World world;
|
||||
Entity player = world.entity();
|
||||
Entity e1 = world.entity();
|
||||
Entity e2 = world.entity();
|
||||
Entity e3 = world.entity();
|
||||
|
||||
SpatialHashTable coltable;
|
||||
coltable.insert({11,11}, player);
|
||||
coltable.insert({10,10}, e2);
|
||||
coltable.insert({11,10}, e3);
|
||||
coltable.insert({21,21}, e1);
|
||||
|
||||
{ // find e3 and e2
|
||||
auto [found, nearby] = coltable.neighbors({11, 11});
|
||||
REQUIRE(found);
|
||||
REQUIRE(nearby.size() == 3);
|
||||
// BUG: replace this with std::find/std::search
|
||||
REQUIRE(nearby[0] == e2);
|
||||
REQUIRE(nearby[1] == e3);
|
||||
REQUIRE(nearby[2] == player);
|
||||
}
|
||||
|
||||
coltable.move({11,11}, {20,20}, player);
|
||||
{ // should only find the e1
|
||||
auto [found, nearby] = coltable.neighbors({20,20});
|
||||
REQUIRE(found);
|
||||
REQUIRE(nearby.size() == 2);
|
||||
// BUG: replace this with std::find/std::search
|
||||
REQUIRE(nearby[0] == player);
|
||||
REQUIRE(nearby[1] == e1);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue