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.
96 lines
2.3 KiB
96 lines
2.3 KiB
1 month ago
|
#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);
|
||
|
}
|
||
|
}
|