|
|
|
#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}, true);
|
|
|
|
|
|
|
|
REQUIRE(found);
|
|
|
|
REQUIRE(nearby[0] == player);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // removed
|
|
|
|
coltable.remove({11,11});
|
|
|
|
auto [found, nearby] = coltable.neighbors({10,10}, true);
|
|
|
|
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}, true);
|
|
|
|
REQUIRE(!found);
|
|
|
|
REQUIRE(nearby.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // find it after move
|
|
|
|
auto [found, nearby] = coltable.neighbors({11,11}, true);
|
|
|
|
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}, true);
|
|
|
|
REQUIRE(found);
|
|
|
|
REQUIRE(nearby.size() == 2);
|
|
|
|
// BUG: replace this with std::find/std::search
|
|
|
|
REQUIRE(nearby[0] == e3);
|
|
|
|
REQUIRE(nearby[1] == e2);
|
|
|
|
}
|
|
|
|
|
|
|
|
coltable.move({11,11}, {20,20}, player);
|
|
|
|
{ // should only find the e1
|
|
|
|
auto [found, nearby] = coltable.neighbors({20,20}, true);
|
|
|
|
REQUIRE(found);
|
|
|
|
REQUIRE(nearby.size() == 1);
|
|
|
|
// BUG: replace this with std::find/std::search
|
|
|
|
REQUIRE(nearby[0] == e1);
|
|
|
|
}
|
|
|
|
}
|