|
|
@ -7,55 +7,57 @@ |
|
|
|
using DinkyECS::Entity; |
|
|
|
using DinkyECS::Entity; |
|
|
|
using namespace fmt; |
|
|
|
using namespace fmt; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FoundList require_found(const SpatialHashTable& collider, Point at, bool diag, size_t expect_size) { |
|
|
|
|
|
|
|
println("TEST require_found at={},{}", at.x, at.y); |
|
|
|
|
|
|
|
auto [found, nearby] = collider.neighbors(at, diag); |
|
|
|
|
|
|
|
REQUIRE(found == true); |
|
|
|
|
|
|
|
REQUIRE(nearby.size() == expect_size); |
|
|
|
|
|
|
|
return nearby; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("confirm basic collision operations", "[collision]") { |
|
|
|
TEST_CASE("confirm basic collision operations", "[collision]") { |
|
|
|
DinkyECS::World world; |
|
|
|
DinkyECS::World world; |
|
|
|
Entity player = world.entity(); |
|
|
|
Entity player = world.entity(); |
|
|
|
Entity enemy = world.entity(); |
|
|
|
Entity enemy = world.entity(); |
|
|
|
|
|
|
|
|
|
|
|
SpatialHashTable coltable; |
|
|
|
SpatialHashTable collider; |
|
|
|
coltable.insert({11,11}, player); |
|
|
|
collider.insert({11,11}, player); |
|
|
|
coltable.insert({21,21}, enemy); |
|
|
|
collider.insert({21,21}, enemy); |
|
|
|
|
|
|
|
|
|
|
|
{ // not found
|
|
|
|
{ // not found
|
|
|
|
auto [found, nearby] = coltable.neighbors({1,1}); |
|
|
|
auto [found, nearby] = collider.neighbors({1,1}); |
|
|
|
REQUIRE(!found); |
|
|
|
REQUIRE(!found); |
|
|
|
REQUIRE(nearby.empty()); |
|
|
|
REQUIRE(nearby.empty()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
{ // found
|
|
|
|
// found
|
|
|
|
auto [found, nearby] = coltable.neighbors({10,10}, true); |
|
|
|
FoundList nearby = require_found(collider, {10,10}, true, 1); |
|
|
|
|
|
|
|
|
|
|
|
REQUIRE(found); |
|
|
|
|
|
|
|
REQUIRE(nearby[0] == player); |
|
|
|
REQUIRE(nearby[0] == player); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ // removed
|
|
|
|
{ // removed
|
|
|
|
coltable.remove({11,11}); |
|
|
|
collider.remove({11,11}); |
|
|
|
auto [found, nearby] = coltable.neighbors({10,10}, true); |
|
|
|
auto [found, nearby] = collider.neighbors({10,10}, true); |
|
|
|
REQUIRE(!found); |
|
|
|
REQUIRE(!found); |
|
|
|
REQUIRE(nearby.empty()); |
|
|
|
REQUIRE(nearby.empty()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
coltable.insert({11,11}, player); // setup for the move test
|
|
|
|
collider.insert({11,11}, player); // setup for the move test
|
|
|
|
|
|
|
|
{ // moving, not found
|
|
|
|
{ // moving
|
|
|
|
collider.move({11,11}, {12, 12}, player); |
|
|
|
coltable.move({11,11}, {12, 12}, player); |
|
|
|
auto [found, nearby] = collider.neighbors({10,10}, true); |
|
|
|
auto [found, nearby] = coltable.neighbors({10,10}, true); |
|
|
|
|
|
|
|
REQUIRE(!found); |
|
|
|
REQUIRE(!found); |
|
|
|
REQUIRE(nearby.empty()); |
|
|
|
REQUIRE(nearby.empty()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
{ // find it after move
|
|
|
|
nearby = require_found(collider, {11,11}, true, 1); |
|
|
|
auto [found, nearby] = coltable.neighbors({11,11}, true); |
|
|
|
|
|
|
|
REQUIRE(found); |
|
|
|
|
|
|
|
REQUIRE(nearby[0] == player); |
|
|
|
REQUIRE(nearby[0] == player); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
REQUIRE(coltable.occupied({12,12})); |
|
|
|
// confirm occupied works
|
|
|
|
REQUIRE(coltable.occupied({21,21})); |
|
|
|
REQUIRE(collider.occupied({12,12})); |
|
|
|
REQUIRE(!coltable.occupied({1,10})); |
|
|
|
REQUIRE(collider.occupied({21,21})); |
|
|
|
} |
|
|
|
REQUIRE(!collider.occupied({1,10})); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -66,27 +68,68 @@ TEST_CASE("confirm multiple entities moving", "[collision]") { |
|
|
|
Entity e2 = world.entity(); |
|
|
|
Entity e2 = world.entity(); |
|
|
|
Entity e3 = world.entity(); |
|
|
|
Entity e3 = world.entity(); |
|
|
|
|
|
|
|
|
|
|
|
SpatialHashTable coltable; |
|
|
|
SpatialHashTable collider; |
|
|
|
coltable.insert({11,11}, player); |
|
|
|
collider.insert({11,11}, player); |
|
|
|
coltable.insert({10,10}, e2); |
|
|
|
collider.insert({10,10}, e2); |
|
|
|
coltable.insert({11,10}, e3); |
|
|
|
collider.insert({11,10}, e3); |
|
|
|
coltable.insert({21,21}, e1); |
|
|
|
collider.insert({21,21}, e1); |
|
|
|
|
|
|
|
|
|
|
|
{ // find e3 and e2
|
|
|
|
FoundList nearby = require_found(collider, {11,11}, false, 1); |
|
|
|
auto [found, nearby] = coltable.neighbors({11, 11}, true); |
|
|
|
REQUIRE(nearby[0] == e3); |
|
|
|
REQUIRE(found); |
|
|
|
|
|
|
|
REQUIRE(nearby.size() == 2); |
|
|
|
nearby = require_found(collider, {11,11}, true, 2); |
|
|
|
// BUG: replace this with std::find/std::search
|
|
|
|
|
|
|
|
REQUIRE(nearby[0] == e3); |
|
|
|
REQUIRE(nearby[0] == e3); |
|
|
|
REQUIRE(nearby[1] == e2); |
|
|
|
REQUIRE(nearby[1] == e2); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
coltable.move({11,11}, {20,20}, player); |
|
|
|
collider.move({11,11}, {20,20}, player); |
|
|
|
{ // should only find the e1
|
|
|
|
nearby = require_found(collider, {20,20}, true, 1); |
|
|
|
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); |
|
|
|
REQUIRE(nearby[0] == e1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("test edge cases that might crash", "[collision]") { |
|
|
|
|
|
|
|
DinkyECS::World world; |
|
|
|
|
|
|
|
Entity player = world.entity(); |
|
|
|
|
|
|
|
Entity enemy = world.entity(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SpatialHashTable collider; |
|
|
|
|
|
|
|
collider.insert({0,0}, player); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Point enemy_at = {1, 0}; |
|
|
|
|
|
|
|
collider.insert(enemy_at, enemy); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FoundList nearby = require_found(collider, {0,0}, true, 1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
collider.move({1,0}, {1,1}, enemy); |
|
|
|
|
|
|
|
nearby = require_found(collider, {0,0}, true, 1); |
|
|
|
|
|
|
|
REQUIRE(nearby[0] == enemy); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
collider.move({1,1}, {0,1}, enemy); |
|
|
|
|
|
|
|
nearby = require_found(collider, {0,0}, true, 1); |
|
|
|
|
|
|
|
REQUIRE(nearby[0] == enemy); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("check all diagonal works", "[collision]") { |
|
|
|
|
|
|
|
DinkyECS::World world; |
|
|
|
|
|
|
|
Entity player = world.entity(); |
|
|
|
|
|
|
|
Entity enemy = world.entity(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SpatialHashTable collider; |
|
|
|
|
|
|
|
Point player_at = {1,1}; |
|
|
|
|
|
|
|
collider.insert(player_at, player); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Point enemy_at = {1, 0}; |
|
|
|
|
|
|
|
collider.insert(enemy_at, enemy); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(size_t x = 0; x <= 2; x++) { |
|
|
|
|
|
|
|
for(size_t y = 0; y <= 2; y++) { |
|
|
|
|
|
|
|
if(enemy_at.x == player_at.x && enemy_at.y == player_at.y) continue; // skip player spot
|
|
|
|
|
|
|
|
FoundList nearby = require_found(collider, player_at, true, 1); |
|
|
|
|
|
|
|
REQUIRE(nearby[0] == enemy); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// move the enemy to a new spot around the player
|
|
|
|
|
|
|
|
Point move_to = {enemy_at.x + x, enemy_at.y + y}; |
|
|
|
|
|
|
|
collider.move(enemy_at, move_to, enemy); |
|
|
|
|
|
|
|
enemy_at = move_to; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|