adding 2 tests on collision components callbacks

This commit is contained in:
Clément Le Bihan
2021-06-10 00:14:36 +02:00
parent 2955cd76a7
commit 40a281cfe6
+96 -2
View File
@@ -1,4 +1,4 @@
//
// Created by Louis Auzuret on 5/31/21.
//
@@ -61,7 +61,7 @@ TEST_CASE("Collision test", "[Component][System]")
}
TEST_CASE("Collsion test with movable", "[Component][System]")
TEST_CASE("Collision test with movable", "[Component][System]")
{
Wal wal;
CollisionSystem collision(wal);
@@ -99,6 +99,100 @@ TEST_CASE("Collsion test with movable", "[Component][System]")
REQUIRE(entity.getComponent<PositionComponent>().position.z == 0.0);
}
TEST_CASE("Collision test callbacks calls", "[Component][System]")
{
int nbCallbacksCalled = 0;
Wal wal;
CollisionSystem collision(wal);
MovableSystem movable(wal);
wal.scene = std::make_shared<Scene>();
wal.scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<CollisionComponent>(
[&nbCallbacksCalled](Entity &actual, const Entity &, int) { nbCallbacksCalled++; },
[&nbCallbacksCalled](Entity &actual, const Entity &, int) { nbCallbacksCalled++; }, 0, 5.0)
.addComponent<MovableComponent>();
wal.scene->addEntity("block")
.addComponent<PositionComponent>(0, 0, 0)
.addComponent<CollisionComponent>(
[&nbCallbacksCalled](Entity &actual, const Entity &, int) { nbCallbacksCalled++; },
[&nbCallbacksCalled](Entity &actual, const Entity &, int) {
nbCallbacksCalled++;
try {
auto &mov = actual.getComponent<MovableComponent>();
mov._velocity = Vector3f();
} catch (std::exception &e) {};
}, 0, 1);
Entity &entity = wal.scene->getEntities().front();
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
entity.getComponent<CollisionComponent>().bound.x = 5;
entity.getComponent<CollisionComponent>().bound.y = 5;
entity.getComponent<CollisionComponent>().bound.z = 5;
entity.getComponent<MovableComponent>().addForce({1, 1, 1});
collision.update(std::chrono::nanoseconds(1));
collision.fixedUpdate();
movable.update(std::chrono::nanoseconds(1));
movable.fixedUpdate();
REQUIRE(nbCallbacksCalled == 4);
REQUIRE(entity.getComponent<PositionComponent>().position.x == 0.0);
REQUIRE(entity.getComponent<PositionComponent>().position.y == 0.0);
REQUIRE(entity.getComponent<PositionComponent>().position.z == 0.0);
}
TEST_CASE("Collision test callbacks args", "[Component][System]")
{
int nbCallbacksCalled = 0;
Wal wal;
CollisionSystem collision(wal);
MovableSystem movable(wal);
wal.scene = std::make_shared<Scene>();
wal.scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<CollisionComponent>(
[&nbCallbacksCalled](Entity &actual, const Entity &other, int) {
nbCallbacksCalled++;
REQUIRE(actual.getName() == "player");
REQUIRE(other.getName() == "block");
},
[&nbCallbacksCalled](Entity &actual, const Entity &other, int) {
nbCallbacksCalled++;
REQUIRE(other.getName() == "player");
REQUIRE(actual.getName() == "block");
}, 0, 5.0);
wal.scene->addEntity("block")
.addComponent<PositionComponent>(0, 0, 0)
.addComponent<CollisionComponent>(
[&nbCallbacksCalled](Entity &actual, const Entity &other, int) {
nbCallbacksCalled++;
REQUIRE(other.getName() == "player");
REQUIRE(actual.getName() == "block");
},
[&nbCallbacksCalled](Entity &actual, const Entity &other, int) {
nbCallbacksCalled++;
REQUIRE(actual.getName() == "player");
REQUIRE(other.getName() == "block");
}, 0, 1);
Entity &entity = wal.scene->getEntities().front();
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
entity.getComponent<CollisionComponent>().bound.x = 5;
entity.getComponent<CollisionComponent>().bound.y = 5;
entity.getComponent<CollisionComponent>().bound.z = 5;
collision.update(std::chrono::nanoseconds(1));
collision.fixedUpdate();
REQUIRE(nbCallbacksCalled == 4);
}
TEST_CASE("Vector round", "[Vector]")
{
Vector3f v(1.3, 1.5, 1.7);