adding unit test and constructor for collusion components without callbacks

This commit is contained in:
Bluub
2021-05-31 15:26:35 +02:00
parent 47aac95801
commit 761eaabddb
4 changed files with 53 additions and 32 deletions
@@ -32,6 +32,14 @@ namespace BBM
: WAL::Component(entity), onCollide(callback), _bound({boundSize, boundSize, boundSize})
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound)
: WAL::Component(entity), onCollide(), _bound(bound)
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize)
: WAL::Component(entity), onCollide(), _bound({boundSize, boundSize, boundSize})
{ }
float CollisionComponent::getBoundX(void) const
{
return _bound.x;
@@ -47,7 +47,13 @@ namespace BBM
//! @brief Constructor with a WAL::Callback
CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> callback, float boundSize = 0);
//! @brief Constructor of collider with no callback
CollisionComponent(WAL::Entity &entity, Vector3f bound);
//! @brief Constructor no callback, same boundSize for all axis
CollisionComponent(WAL::Entity &entity, float boundSize);
//! @brief A component can't be instantiated, it should be derived.
CollisionComponent(const CollisionComponent &) = default;
@@ -25,6 +25,8 @@ namespace BBM
std::max(posA.getY(), posA.getY() + col.getBoundY()),
std::max(posA.getZ(), posA.getZ() + col.getBoundZ())};
for (auto &other : _wal.scene->getEntities()) {
if (&other == &entity)
continue;
auto &colB = entity.getComponent<CollisionComponent>();
auto &posB = other.getComponent<PositionComponent>();
Vector3f minB = { std::min(posB.getX(), posB.getX() + colB.getBoundX()),
+36 -31
View File
@@ -18,36 +18,41 @@ using namespace BBM;
TEST_CASE("Collsion test", "[Component][System]")
{
Wal wal;
CollisionSystem collision(wal);
wal.scene = std::shared_ptr<Scene>(new Scene);
wal.scene->addEntity("player")
.addComponent<PositionComponent>();
// .addComponent<CollisionComponent>([](Entity &actual, const Entity &) {
// auto &pos = actual.getComponent<PositionComponent>();
// pos.position.x = 1;
// pos.position.y = 1;
// pos.position.z = 1;
// }, 5.0);
//Entity &entity = wal.scene->getEntities()[0];
//
//REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
//
//entity.getComponent<CollisionComponent>()._bound.x = 5;
//entity.getComponent<CollisionComponent>()._bound.y = 5;
//entity.getComponent<CollisionComponent>()._bound.z = 5;
//
//CollisionSystem collision(wal);
//collision.onUpdate(entity, std::chrono::nanoseconds(1));
//collision.onFixedUpdate(entity);
//REQUIRE(entity.getComponent<PositionComponent>().position.x == 0);
//REQUIRE(entity.getComponent<PositionComponent>().position.y == 0);
//REQUIRE(entity.getComponent<PositionComponent>().position.z == 0);
//
//
//wal.scene->addEntity("block")
// .addComponent<PositionComponent>()
// .addComponent<CollisionComponent>([](Entity &, const Entity &){}, 1);
//collision.onUpdate(entity, std::chrono::nanoseconds(1));
//collision.onFixedUpdate(entity);
//REQUIRE(entity.getComponent<PositionComponent>().position.x == 1);
//REQUIRE(entity.getComponent<PositionComponent>().position.y == 1);
//REQUIRE(entity.getComponent<PositionComponent>().position.z == 1);
.addComponent<PositionComponent>()
.addComponent<CollisionComponent>([](Entity &actual, const Entity &) {
try {
auto &pos = actual.getComponent<PositionComponent>();
pos.position.x = 1;
pos.position.y = 1;
pos.position.z = 1;
} catch (std::exception &e) {};
}, 5.0);
Entity &entity = wal.scene->getEntities()[0];
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.onUpdate(entity, std::chrono::nanoseconds(1));
collision.onFixedUpdate(entity);
REQUIRE(entity.getComponent<PositionComponent>().position.x == 0.0);
REQUIRE(entity.getComponent<PositionComponent>().position.y == 0.0);
REQUIRE(entity.getComponent<PositionComponent>().position.z == 0.0);
wal.scene->addEntity("block")
.addComponent<PositionComponent>(2,2,2)
.addComponent<CollisionComponent>(1);
Entity &player = wal.scene->getEntities()[0];
collision.onUpdate(entity, std::chrono::nanoseconds(1));
REQUIRE(player.hasComponent(typeid(PositionComponent)));
collision.onFixedUpdate(player);
REQUIRE(wal.scene->getEntities().size() == 2);
REQUIRE(player.hasComponent(typeid(PositionComponent)));
REQUIRE(player.getComponent<PositionComponent>().position.x == 1.0);
REQUIRE(player.getComponent<PositionComponent>().position.y == 1);
REQUIRE(player.getComponent<PositionComponent>().position.z == 1);
}