diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 06878a76..e4244a7a 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -23,8 +23,14 @@ namespace BBM }; //! @brief onCollide functions to be called + //! @param first self + //! @param second the entity you collided + //! @param third the collision axis (to know which axis collided) WAL::Callback onCollide; //! @brief onCollided functions to be called + //! @param first the entity that collided you + //! @param second self + //! @param third the collision axis (to know which axis collided) WAL::Callback onCollided; //! @brief Bound size on all axis Vector3f bound; diff --git a/sources/Items/Bonus.cpp b/sources/Items/Bonus.cpp index 60365e05..47a5c2ba 100644 --- a/sources/Items/Bonus.cpp +++ b/sources/Items/Bonus.cpp @@ -12,36 +12,35 @@ namespace BBM { void Bonus::BombUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { - if (bonus.shouldDelete()) + if (bonus.shouldDelete() || axis != 7) return; auto *bombHolder = player.tryGetComponent(); - if (bombHolder == nullptr) + if (!bombHolder) return; bombHolder->maxBombCount++; } void Bonus::ExplosionRangeBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { - static int test = 0; - if (bonus.shouldDelete()) + if (bonus.shouldDelete() || axis != 7) return; auto *bombHolder = player.tryGetComponent(); auto *playerBonus = player.tryGetComponent(); - if (bombHolder == nullptr || playerBonus == nullptr) + if (!bombHolder || !playerBonus) return; if (bombHolder->explosionRadius <= 6) bombHolder->explosionRadius++; - std::cout << ++test << " Explosion radius : " << bombHolder->explosionRadius << std::endl; playerBonus->nextRangeBonusRate = playerBonus->rangeBonusRate; } void Bonus::SpeedUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { - std::cout << "soeed" << std::endl; - if (bonus.shouldDelete()) + if (bonus.shouldDelete() || axis != 7) return; auto *controllable = player.tryGetComponent(); auto *playerBonus = player.tryGetComponent(); + if (!controllable || !playerBonus) + return; controllable->speed = 0.35f; playerBonus->nextSpeedBonusRate = playerBonus->speedBonusRate; } @@ -51,7 +50,7 @@ namespace BBM { double rnd = static_cast(std::rand()) / RAND_MAX; if (rnd < 0.8) - return (static_cast(std::rand() % (DAMAGEINC - 1) + 1)); + return (static_cast(std::rand() % (EXPLOSIONINC - 1) + 1)); return (NOTHING); } } \ No newline at end of file diff --git a/sources/Items/Bonus.hpp b/sources/Items/Bonus.hpp index 25fa182c..6c15efd4 100644 --- a/sources/Items/Bonus.hpp +++ b/sources/Items/Bonus.hpp @@ -34,8 +34,7 @@ namespace BBM { NOTHING, BOMBSTOCK, SPEEDUP, - EXPLOSIONINC, - DAMAGEINC + EXPLOSIONINC }; static BonusType getRandomBonusType(); diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 5b923d6c..fb9ef9b3 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -17,9 +17,9 @@ using namespace std::chrono_literals; namespace BBM { - void MapGenerator::wallCollide(WAL::Entity &entity, - const WAL::Entity &wall, - CollisionComponent::CollidedAxis collidedAxis) + void MapGenerator::wallCollided(WAL::Entity &entity, + const WAL::Entity &wall, + CollisionComponent::CollidedAxis collidedAxis) { auto *mov = entity.tryGetComponent(); @@ -57,10 +57,9 @@ namespace BBM entity.scheduleDeletion(); }) .addComponent(position.y) - .addComponent(func[bonusType - 1], [](WAL::Entity &bonus, const WAL::Entity &player, CollisionComponent::CollidedAxis axis) { + .addComponent([](WAL::Entity &bonus, const WAL::Entity &player, CollisionComponent::CollidedAxis axis) { bonus.scheduleDeletion(); - std::cout << "called" << std::endl; - }, 0.5, .5) + }, func[bonusType - 1], 0.5, .5) .addComponent(5s, [](WAL::Entity &bonus, WAL::Wal &wal){ bonus.scheduleDeletion(); }) @@ -94,7 +93,7 @@ namespace BBM .addComponent>() .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, 0.25, .75) + &MapGenerator::wallCollided, 0.25, .75) .addComponent(unbreakableObj, false, std::make_pair(MAP_DIFFUSE, unbreakablePng)); } @@ -112,7 +111,7 @@ namespace BBM .addComponent>() .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, Vector3f(-(width + 1) / 2 , 0.25, 0.25), Vector3f(width + 1, 2, 0.75)) + &MapGenerator::wallCollided, Vector3f(-(width + 1) / 2 , 0.25, 0.25), Vector3f(width + 1, 2, 0.75)) .addComponent(unbreakableObj, false, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); @@ -121,8 +120,8 @@ namespace BBM .addComponent>() .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, Vector3f(-(width + 1) / 2 , 0.25, 0.25), Vector3f(width + 1, 2, 0.75)) - .addComponent(unbreakableObj, false, + &MapGenerator::wallCollided, Vector3f(-(width + 1) / 2 , 0.25, 0.25), Vector3f(width + 1, 2, 0.75)) + .addComponent(unbreakableObj, false, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") @@ -130,16 +129,16 @@ namespace BBM .addComponent>() .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, Vector3f(0.25, 0.25, -(height + 1) / 2 ), Vector3f(0.75, 2, height + 1)) - .addComponent(unbreakableObj, false, + &MapGenerator::wallCollided, Vector3f(0.25, 0.25, -(height + 1) / 2 ), Vector3f(0.75, 2, height + 1)) + .addComponent(unbreakableObj, false, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, height / 2)) .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, Vector3f(0.25, 0.25, -(height + 1) / 2 ), Vector3f(0.75, 2, height + 1)) - .addComponent(unbreakableObj, false, + &MapGenerator::wallCollided, Vector3f(0.25, 0.25, -(height + 1) / 2 ), Vector3f(0.75, 2, height + 1)) + .addComponent(unbreakableObj, false, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); } @@ -188,7 +187,7 @@ namespace BBM .addComponent(1, &MapGenerator::wallDestroyed) .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, 0.25, .75) + &MapGenerator::wallCollided, 0.25, .75) .addComponent(breakableObj, false, std::make_pair(MAP_DIFFUSE, breakablePng)); } @@ -224,7 +223,7 @@ namespace BBM .addComponent>() .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, 0.25, .75) + &MapGenerator::wallCollided, 0.25, .75) .addComponent(UnbreakableObj, false, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index ea8da96c..8fb6da1c 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -155,9 +155,9 @@ namespace BBM static const std::string secondFloorHolePath; public: - static void wallCollide(WAL::Entity &entity, - const WAL::Entity &wall, - CollisionComponent::CollidedAxis collidedAxis); + static void wallCollided(WAL::Entity &entity, + const WAL::Entity &wall, + CollisionComponent::CollidedAxis collidedAxis); static void wallDestroyed(WAL::Entity &entity, WAL::Wal &wal); diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index 118129fc..6a5dac6e 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -27,7 +27,7 @@ namespace BBM auto &bombInfo = bomb.getComponent(); if (bombInfo.ignoreOwner && bombInfo.ownerID == entity.getUid()) return; - return MapGenerator::wallCollide(entity, bomb, collidedAxis); + return MapGenerator::wallCollided( entity, bomb, collidedAxis); } diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index bb12b32f..db68e3c0 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -69,7 +69,7 @@ namespace BBM collidedAxis += CollisionComponent::CollidedAxis::Z; } if (collidedAxis) { - colA.onCollide(other, entity, static_cast(collidedAxis)); + colA.onCollide(entity, other, static_cast(collidedAxis)); colB.onCollided(entity, other, static_cast(collidedAxis)); } } diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index 16c82d82..6916de9f 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -20,6 +20,6 @@ namespace BBM auto &movable = entity.get(); Vector2f move = controllable.move.normalized() * controllable.speed; - movable.addForce(Vector3f(move.x, controllable.jump, move.y)); + movable.addForce(Vector3f(move.x, 0, move.y)); } } \ No newline at end of file diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 1aeb43b3..fa4331e8 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -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().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(); + + wal.scene->addEntity("player") + .addComponent() + .addComponent( + [&nbCallbacksCalled](Entity &actual, const Entity &, int) { nbCallbacksCalled++; }, + [&nbCallbacksCalled](Entity &actual, const Entity &, int) { nbCallbacksCalled++; }, 0, 5.0) + .addComponent(); + + wal.scene->addEntity("block") + .addComponent(0, 0, 0) + .addComponent( + [&nbCallbacksCalled](Entity &actual, const Entity &, int) { nbCallbacksCalled++; }, + [&nbCallbacksCalled](Entity &actual, const Entity &, int) { + nbCallbacksCalled++; + try { + auto &mov = actual.getComponent(); + mov._velocity = Vector3f(); + } catch (std::exception &e) {}; + }, 0, 1); + Entity &entity = wal.scene->getEntities().front(); + REQUIRE(entity.getComponent().position == Vector3f()); + + entity.getComponent().bound.x = 5; + entity.getComponent().bound.y = 5; + entity.getComponent().bound.z = 5; + + entity.getComponent().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().position.x == 0.0); + REQUIRE(entity.getComponent().position.y == 0.0); + REQUIRE(entity.getComponent().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(); + + wal.scene->addEntity("player") + .addComponent() + .addComponent( + [&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(0, 0, 0) + .addComponent( + [&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().position == Vector3f()); + + entity.getComponent().bound.x = 5; + entity.getComponent().bound.y = 5; + entity.getComponent().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);