diff --git a/CMakeLists.txt b/CMakeLists.txt index 760e1c33..09365555 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,15 +101,15 @@ target_link_libraries(bomberman PUBLIC wal ray) add_executable(unit_tests EXCLUDE_FROM_ALL - ${SOURCES} - tests/EntityTests.cpp - tests/MainTest.cpp - tests/EngineTests.cpp - tests/CallbackTest.cpp - tests/MoveTests.cpp - tests/ViewTest.cpp - tests/CollisionTest.cpp -) + ${SOURCES} + tests/EntityTests.cpp + tests/MainTest.cpp + tests/EngineTests.cpp + tests/CallbackTest.cpp + tests/MoveTests.cpp + tests/ViewTest.cpp + tests/CollisionTest.cpp + ) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index df727b2d..652e6727 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -4,31 +4,54 @@ #include "Component/Collision/CollisionComponent.hpp" - -namespace BBM +namespace BBM { CollisionComponent::CollisionComponent(WAL::Entity &entity) - : WAL::Component(entity) - { } + : WAL::Component(entity) + {} WAL::Component *CollisionComponent::clone(WAL::Entity &entity) const { return new CollisionComponent(entity); } - CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, Vector3f bound) - : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound) - { } + CollisionComponent::CollisionComponent(WAL::Entity &entity, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, + Vector3f positionOffset, + Vector3f bound) + : WAL::Component(entity), + onCollide(onCollide), + onCollided(onCollided), + bound(bound), + positionOffset(positionOffset) + {} - CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, float boundSize) - : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound({boundSize, boundSize, boundSize}) - { } + CollisionComponent::CollisionComponent(WAL::Entity &entity, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, + float positionOffset, + float boundSize) + : WAL::Component(entity), + onCollide(onCollide), + onCollided(onCollided), + bound({boundSize, boundSize, boundSize}), + positionOffset({positionOffset, positionOffset, positionOffset}) + {} - CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound) - : WAL::Component(entity), onCollide(), onCollided(), bound(bound) - { } + CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f positionOffset, Vector3f bound) + : WAL::Component(entity), + onCollide(), + onCollided(), + bound(bound), + positionOffset(positionOffset) + {} - CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize) - : WAL::Component(entity), onCollide(), onCollided(), bound({boundSize, boundSize, boundSize}) - { } + CollisionComponent::CollisionComponent(WAL::Entity &entity, float positionOffset, float boundSize) + : WAL::Component(entity), + onCollide(), + onCollided(), + bound({boundSize, boundSize, boundSize}), + positionOffset({positionOffset, positionOffset, positionOffset}) + {} } \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 5bfe7e16..06878a76 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -9,43 +9,61 @@ #include "Component/Component.hpp" #include "Entity/Entity.hpp" -namespace BBM +namespace BBM { class CollisionComponent : public WAL::Component { - private: - public: - //! @brief onCollide functions to be called - WAL::Callback onCollide; - //! @brief onCollided functions to be called - WAL::Callback onCollided; - //! @brief Bound size on all axis - Vector3f bound; - //! @inherit - WAL::Component *clone(WAL::Entity &entity) const override; + public: + //! @brief Used to tell the collided axis + //! @note Usage: (collidedAxis (int given by callback)) & CollidedAxis::X + enum CollidedAxis { + X = 1, + Y = 2, + Z = 4 + }; - //! @brief A component can't be instantiated, it should be derived. - explicit CollisionComponent(WAL::Entity &entity); + //! @brief onCollide functions to be called + WAL::Callback onCollide; + //! @brief onCollided functions to be called + WAL::Callback onCollided; + //! @brief Bound size on all axis + Vector3f bound; + //! @brief Offset from the position component + Vector3f positionOffset; - //! @brief Constructor with a WAL::Callback - CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided,Vector3f bound); + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; - //! @brief Constructor with a WAL::Callback, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, float boundSize = 0); + //! @brief A component can't be instantiated, it should be derived. + explicit CollisionComponent(WAL::Entity &entity); - //! @brief Constructor of collider with no callback - CollisionComponent(WAL::Entity &entity, Vector3f bound); + //! @brief Constructor with a WAL::Callback + CollisionComponent(WAL::Entity &entity, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, + Vector3f positionOffset, + Vector3f bound); - //! @brief Constructor no callback, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, float boundSize); + //! @brief Constructor with a WAL::Callback, same boundSize for all axis + CollisionComponent(WAL::Entity &entity, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, + float positionOffset, + float boundSize); - //! @brief Default copy constructor - CollisionComponent(const CollisionComponent &) = default; + //! @brief Constructor of collider with no callback + CollisionComponent(WAL::Entity &entity, Vector3f positionOffset, Vector3f bound); - //! @brief default destructor - ~CollisionComponent() override = default; + //! @brief Constructor no callback, same boundSize & positionOffset for all axis + CollisionComponent(WAL::Entity &entity, float positionOffset, float boundSize); - //! @brief A component can't be assigned - CollisionComponent &operator=(const CollisionComponent &) = delete; + //! @brief Default copy constructor + CollisionComponent(const CollisionComponent &) = default; + + //! @brief default destructor + ~CollisionComponent() override = default; + + //! @brief A component can't be assigned + CollisionComponent &operator=(const CollisionComponent &) = delete; }; } \ No newline at end of file diff --git a/sources/Items/Bonus.cpp b/sources/Items/Bonus.cpp index f6c14c58..9da6e4c1 100644 --- a/sources/Items/Bonus.cpp +++ b/sources/Items/Bonus.cpp @@ -2,12 +2,13 @@ // Created by HENRY Benjamin on 02/06/2021. // +#include #include "Component/Movable/MovableComponent.hpp" #include "Bonus.hpp" #include "Component/BombHolder/BombHolderComponent.hpp" namespace BBM { - void Bonus::BombUpBonus(WAL::Entity &player, const WAL::Entity &bonus) + void Bonus::BombUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { if (player.hasComponent()) { auto &bombHolder = player.getComponent(); @@ -15,7 +16,7 @@ namespace BBM { } } - void Bonus::DamageIncreasedBonus(WAL::Entity &player, const WAL::Entity &bonus) + void Bonus::DamageIncreasedBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { if (player.hasComponent()) { auto &bombHolder = player.getComponent(); @@ -24,7 +25,7 @@ namespace BBM { } } - void Bonus::ExplosionRangeBonus(WAL::Entity &player, const WAL::Entity &bonus) + void Bonus::ExplosionRangeBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { if (player.hasComponent()) { auto &bombHolder = player.getComponent(); @@ -33,7 +34,7 @@ namespace BBM { } } - void Bonus::SpeedUpBonus(WAL::Entity &player, const WAL::Entity &bonus) + void Bonus::SpeedUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { if (!player.hasComponent()) return; @@ -41,7 +42,7 @@ namespace BBM { movable.addForce(Vector3f(1, 0, 1)); } - void Bonus::IgnoreWallsBonus(WAL::Entity &player, const WAL::Entity &bonus) + void Bonus::IgnoreWallsBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { if (player.hasComponent()) { auto &bombHolder = player.getComponent(); diff --git a/sources/Items/Bonus.hpp b/sources/Items/Bonus.hpp index 25579ef0..36a37607 100644 --- a/sources/Items/Bonus.hpp +++ b/sources/Items/Bonus.hpp @@ -13,26 +13,26 @@ namespace BBM { //! @param bonus bonus //! @param player the entity on which the effect will be applied //! @brief Apply bonus effect that allows players to carry one more bomb than before - static void BombUpBonus(WAL::Entity &player, const WAL::Entity &bonus); + static void BombUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis); //! @param bonus bonus //! @param player the entity on which the effect will be applied //! @brief Apply bonus effect who increased the bomb damage - static void DamageIncreasedBonus(WAL::Entity &player, const WAL::Entity &bonus); + static void DamageIncreasedBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis); //! @param bonus bonus //! @param player the entity on which the effect will be applied //! @brief Apply bonus effect that expend the explosion range of the bomb - static void ExplosionRangeBonus(WAL::Entity &player, const WAL::Entity &bonus); + static void ExplosionRangeBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis); //! @param bonus bonus //! @param player the entity on which the effect will be applied //! @brief Apply bonus effect that allows to run faster - static void SpeedUpBonus(WAL::Entity &player, const WAL::Entity &bonus); + static void SpeedUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis); //! @param bonus bonus //! @param player the entity on which the effect will be applied //! @brief Apply bonus effect that allows bomb explosion to pass through walls - static void IgnoreWallsBonus(WAL::Entity &player, const WAL::Entity &bonus); + static void IgnoreWallsBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis); }; } \ No newline at end of file diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 643d471d..516f1ba0 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -3,7 +3,8 @@ // Edited by Benjamin Henry on 5/26/21. // -#include +#include "Component/Collision/CollisionComponent.hpp" +#include "System/Collision/CollisionSystem.hpp" #include "Map.hpp" #include #include @@ -12,23 +13,20 @@ namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { - void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall) + void MapGenerator::wallCollide(WAL::Entity &entity, + const WAL::Entity &wall, + CollisionComponent::CollidedAxis collidedAxis) { auto *mov = entity.tryGetComponent(); + if (!mov) return; - auto &pos = entity.getComponent(); - const auto &wallPos = wall.getComponent(); - auto diff = pos.position + mov->getVelocity() - wallPos.position; -// mov->_velocity = Vector3f(); -// if (diff.x <= 0 && mov->_velocity.x < 0) -// mov->_velocity.x = 0; -// if (diff.x >= 0 && mov->_velocity.x > 0) -// mov->_velocity.x = 0; -// if (diff.z <= 0 && mov->_velocity.z < 0) -// mov->_velocity.z = 0; -// if (diff.z >= 0 && mov->_velocity.z > 0) -// mov->_velocity.z = 0; + if (collidedAxis & CollisionComponent::CollidedAxis::X) + mov->_velocity.x = 0; + if (collidedAxis & CollisionComponent::CollidedAxis::Y) + mov->_velocity.x = 0; + if (collidedAxis & CollisionComponent::CollidedAxis::Z) + mov->_velocity.z = 0; } void MapGenerator::wallDestroyed(WAL::Entity &entity) @@ -59,8 +57,11 @@ namespace BBM if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) - .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng)); + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) + .addComponent(unbreakableObj, + std::make_pair(MAP_DIFFUSE, unbreakablePng)); } } } @@ -73,25 +74,33 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, 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, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); @@ -107,7 +116,7 @@ namespace BBM if (map[std::make_tuple(i, 0, j)] != HOLE && map[std::make_tuple(i, -1, j)] != BUMPER) scene->addEntity("Unbreakable Wall") .addComponent(Vector3f(i, -1, j)) - .addComponent(floorObj, + .addComponent(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); } } @@ -142,7 +151,9 @@ namespace BBM .addComponent(coords) .addComponent(1, &MapGenerator::wallDestroyed) .addComponent() - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } @@ -175,7 +186,9 @@ namespace BBM scene->addEntity("Unbreakable Block") .addComponent(coords) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } @@ -194,7 +207,8 @@ namespace BBM if (coords.y == 0) holeEntity.addComponent(holeObj, std::make_pair(MAP_DIFFUSE, holePng)); else - holeEntity.addComponent(secondFloorObj, std::make_pair(MAP_DIFFUSE, secondFloorPng)); + holeEntity.addComponent(secondFloorObj, + std::make_pair(MAP_DIFFUSE, secondFloorPng)); /*.addComponent([](WAL::Entity &other, const WAL::Entity &entity) { if (other.hasComponent()) { auto &health = other.getComponent(); @@ -339,7 +353,7 @@ namespace BBM return (map); } - void MapGenerator::loadMap(int width, int height, MapBlock map, std::shared_ptr scene) + void MapGenerator::loadMap(int width, int height, MapBlock map, const std::shared_ptr &scene) { generateWall(width, height, scene); generateFloor(map, width, height, scene); diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 7085d662..4a961cbc 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -27,150 +27,155 @@ namespace BBM class MapGenerator { - private: - //! @brief Enum of the block available. - enum BlockType { - NOTHING, - BREAKABLE, - HOLE, - UPPERFLOOR, - FLOOR, - BUMPER, - STAIRS, - SPAWNER, - UNBREAKABLE - }; + private: + //! @brief Enum of the block available. + enum BlockType + { + NOTHING, + BREAKABLE, + HOLE, + UPPERFLOOR, + FLOOR, + BUMPER, + STAIRS, + SPAWNER, + UNBREAKABLE + }; - using MapElem = std::function scene)>; - using MapBlock = std::map, BlockType>; + using MapElem = std::function scene)>; + using MapBlock = std::map, BlockType>; - //! @brief Generate random block type - static BlockType getRandomBlockType(); + //! @brief Generate random block type + static BlockType getRandomBlockType(); - //! @param map ASCII map - //! @param x x index on the block - //! @param z z index on the block - //! @param blockType blockType to compare with position - static bool isCloseToBlockType(std::map, BlockType> map, int x, int y, int z, BlockType blockType); + //! @param map ASCII map + //! @param x x index on the block + //! @param z z index on the block + //! @param blockType blockType to compare with position + static bool isCloseToBlockType(std::map, BlockType> map, + int x, int y, int z, + BlockType blockType); - //! @param width Width of the map - //! @param height Height of the map - //! @param scene Scene where the map is instanced - //! @brief Generate the unbreakable block of the map - static void generateUnbreakableBlock(int width, int height, std::shared_ptr scene); + //! @param width Width of the map + //! @param height Height of the map + //! @param scene Scene where the map is instanced + //! @brief Generate the unbreakable block of the map + static void generateUnbreakableBlock(int width, int height, std::shared_ptr scene); - //! @param width Width of the map - //! @param height Height of the map - //! @param scene Scene where the map is instanced - //! @brief Generate the wall of the map - static void generateWall(int width, int height, std::shared_ptr scene); + //! @param width Width of the map + //! @param height Height of the map + //! @param scene Scene where the map is instanced + //! @brief Generate the wall of the map + static void generateWall(int width, int height, std::shared_ptr scene); - //! @param width Width of the map - //! @param height Height of the map - //! @param scene Scene where the map is instanced - //! @brief Generate the floor of the map - static void generateFloor(MapBlock map, int width, int height, std::shared_ptr scene); + //! @param width Width of the map + //! @param height Height of the map + //! @param scene Scene where the map is instanced + //! @brief Generate the floor of the map + static void generateFloor(MapBlock map, int width, int height, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create element of the map - static void createElement(Vector3f coords, std::shared_ptr scene, BlockType blockType); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create element of the map + static void createElement(Vector3f coords, std::shared_ptr scene, BlockType blockType); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create breakable of the map - static void createBreakable(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create breakable of the map + static void createBreakable(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create unbreakable of the map - static void createUnbreakable(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create unbreakable of the map + static void createUnbreakable(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create hole of the map - static void createHole(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create hole of the map + static void createHole(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create bumper of the map - static void createBumper(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create bumper of the map + static void createBumper(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create floor of the map - static void createFloor(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create floor of the map + static void createFloor(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create upper floor of the map - static void createUpperFloor(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create upper floor of the map + static void createUpperFloor(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create stair of the map - static void createStairs(Vector3f coords, std::shared_ptr scene); - - //! @param map Map to load with block declared inside - //! @param width Width of the map - //! @param height Height of the map - //! @brief Generate map of block to be loaded - static MapBlock createSpawner(MapBlock map, int width, int height); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create stair of the map + static void createStairs(Vector3f coords, std::shared_ptr scene); - //! @param map Map to load with block declared inside - //! @param width Width of the map - //! @param height Height of the map - //! @brief Generate height for the map - static MapBlock createHeight(MapBlock map, int width, int height); + //! @param map Map to load with block declared inside + //! @param width Width of the map + //! @param height Height of the map + //! @brief Generate map of block to be loaded + static MapBlock createSpawner(MapBlock map, int width, int height); - //! @param map Map to load with block declared inside - //! @param width Width of the map - //! @param height Height of the map - //! @brief Clean breakable on stairs, bumpers, etc.. - static MapBlock cleanBreakable(MapBlock map, int width, int height); + //! @param map Map to load with block declared inside + //! @param width Width of the map + //! @param height Height of the map + //! @brief Generate height for the map + static MapBlock createHeight(MapBlock map, int width, int height); - - static const std::string assetsPath; - - static const std::string wallAssetsPath; - - static const std::string imageExtension; - - static const std::string objExtension; - - static const std::string unbreakableWallPath; - - static const std::string breakableWallPath; - - static const std::string floorPath; - - static const std::string stairsPath; - - static const std::string bumperPath; - - static const std::string secondFloorPath; - - static const std::string holePath; - - static const std::string secondFloorHolePath; - - public: - static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall); - static void wallDestroyed(WAL::Entity &entity); + //! @param map Map to load with block declared inside + //! @param width Width of the map + //! @param height Height of the map + //! @brief Clean breakable on stairs, bumpers, etc.. + static MapBlock cleanBreakable(MapBlock map, int width, int height); - //! @param width Width of the map - //! @param height Height of the map - //! @brief Generate map of block to be loaded - static MapBlock createMap(int width, int height); + static const std::string assetsPath; + + static const std::string wallAssetsPath; + + static const std::string imageExtension; + + static const std::string objExtension; + + static const std::string unbreakableWallPath; + + static const std::string breakableWallPath; + + static const std::string floorPath; + + static const std::string stairsPath; + + static const std::string bumperPath; + + static const std::string secondFloorPath; + + static const std::string holePath; + + static const std::string secondFloorHolePath; + + public: + static void wallCollide(WAL::Entity &entity, + const WAL::Entity &wall, + CollisionComponent::CollidedAxis collidedAxis); + static void wallDestroyed(WAL::Entity &entity); + + + //! @param width Width of the map + //! @param height Height of the map + //! @brief Generate map of block to be loaded + static MapBlock createMap(int width, int height); + + //! @param width Width of the map + //! @param height Height of the map + //! @param map Map to load with block declared inside + //! @param scene Scene where the map is instanced + //! @brief Generate the map + static void loadMap(int width, int height, MapBlock map, const std::shared_ptr &scene); - //! @param width Width of the map - //! @param height Height of the map - //! @param map Map to load with block declared inside - //! @param scene Scene where the map is instanced - //! @brief Generate the map - static void loadMap(int width, int height, MapBlock map, std::shared_ptr scene); - }; } // namespace BBM \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index d49c590a..96c6b5f2 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -80,7 +80,7 @@ namespace BBM .addComponent() .addComponent() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) - .addComponent(1) + .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() .addComponent() .addComponent(1, [](WAL::Entity &entity) { @@ -90,12 +90,12 @@ namespace BBM scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); -// scene->addEntity("cube") -// .addComponent(5, 0, 5) -// .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) -// .addComponent() -// .addComponent() -// .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 3); + /*scene->addEntity("cube") + .addComponent(-5, 0, -5) + .addComponent(Vector3f(0, 0, 0), Vector3f(3, 3, 3), RED) + .addComponent() + .addComponent() + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, -1, 3);*/ std::srand(std::time(nullptr)); MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 6a3e36cd..db68e3c0 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -14,7 +14,7 @@ namespace BBM : System(wal) { } - bool CollisionSystem::collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB) + bool CollisionSystem::boxesCollide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB) { bool overlapX = (minA.x <= maxB.x && maxA.x >= minB.x) || (minB.x <= maxA.x && maxB.x >= minA.x); bool overlapY = (minA.y <= maxB.y && maxA.y >= minB.y) || (minB.y <= maxA.y && maxB.y >= minA.y); @@ -26,20 +26,51 @@ namespace BBM void CollisionSystem::onFixedUpdate(WAL::ViewEntity &entity) { auto &posA = entity.get(); - auto &col = entity.get(); - Vector3f position = posA.position; - if (auto *movable = entity->tryGetComponent()) - position += movable->getVelocity(); - Vector3f minA = Vector3f::min(position, position + col.bound); - Vector3f maxA = Vector3f::max(position, position + col.bound); + auto &colA = entity.get(); + Vector3f pointA = posA.position + colA.positionOffset; + Vector3f pointAx = pointA; + Vector3f pointAy = pointA; + Vector3f pointAz = pointA; + + if (auto *movable = entity->tryGetComponent()) { + auto vel = movable->getVelocity(); + pointAx.x += vel.x; + pointAy.y += vel.y; + pointAz.z += vel.z; + } + + Vector3f minAx = Vector3f::min(pointAx, pointAx + colA.bound); + Vector3f maxAx = Vector3f::max(pointAx, pointAx + colA.bound); + + Vector3f minAy = Vector3f::min(pointAy, pointAy + colA.bound); + Vector3f maxAy = Vector3f::max(pointAy, pointAy + colA.bound); + + Vector3f minAz = Vector3f::min(pointAz, pointAz + colA.bound); + Vector3f maxAz = Vector3f::max(pointAz, pointAz + colA.bound); + for (auto &[other, posB, colB] : this->getView()) { if (other.getUid() == entity->getUid()) continue; - Vector3f minB = Vector3f::min(posB.position, posB.position + colB.bound); - Vector3f maxB = Vector3f::max(posB.position, posB.position + colB.bound); - if (collide(minA, maxA, minB, maxB)) { - col.onCollide(entity, other); - colB.onCollided(entity, other); + + auto pointB = posB.position + colB.positionOffset; + int collidedAxis = 0; + + // TODO if B is also a movable we don't check with it's changing position + Vector3f minB = Vector3f::min(pointB, pointB + colB.bound); + Vector3f maxB = Vector3f::max(pointB, pointB + colB.bound); + + if (boxesCollide(minAx, maxAx, minB, maxB)) { + collidedAxis += CollisionComponent::CollidedAxis::X; + } + if (boxesCollide(minAy, maxAy, minB, maxB)) { + collidedAxis += CollisionComponent::CollidedAxis::Y; + } + if (boxesCollide(minAz, maxAz, minB, maxB)) { + collidedAxis += CollisionComponent::CollidedAxis::Z; + } + if (collidedAxis) { + colA.onCollide(entity, other, static_cast(collidedAxis)); + colB.onCollided(entity, other, static_cast(collidedAxis)); } } } diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp index 2c0f5491..e1e75ede 100644 --- a/sources/System/Collision/CollisionSystem.hpp +++ b/sources/System/Collision/CollisionSystem.hpp @@ -31,6 +31,6 @@ namespace BBM CollisionSystem &operator=(const CollisionSystem &) = delete; //! @brief check AABB collision - static bool collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB); + static bool boxesCollide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB); }; } \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 19b17476..85fa2f0f 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -33,7 +33,7 @@ namespace BBM {BonusComponent::BonusType::DAMAGEINC, "assets/items/fireup"}, {BonusComponent::BonusType::IGNOREWALLS, "assets/items/wallpass"} }; - static std::vector> func = { + static std::vector> func = { &Bonus::BombUpBonus, &Bonus::SpeedUpBonus, //&Bonus::ExplosionRangeBonus, &Bonus::DamageIncreasedBonus, &Bonus::IgnoreWallsBonus }; @@ -42,18 +42,18 @@ namespace BBM return; try { this->_wal.scene->scheduleNewEntity("Bonus") - .addComponent(position) - .addComponent(1, [](WAL::Entity &entity) { - entity.scheduleDeletion(); - }) - .addComponent(position.y) - .addComponent([](WAL::Entity &bonus, const WAL::Entity &player) { - bonus.scheduleDeletion(); - }, func[bonusType - 1]) - .addComponent(timer, [](WAL::Entity &bonus, WAL::Wal &wal){ - bonus.scheduleDeletion(); - }) - .addComponent(map.at(bonusType) + ".obj", std::make_pair(MAP_DIFFUSE, "assets/items/items.png")); + .addComponent(position) + .addComponent(1, [](WAL::Entity &entity) { + entity.scheduleDeletion(); + }) + .addComponent(position.y) + .addComponent([](WAL::Entity &bonus, const WAL::Entity &player, CollisionComponent::CollidedAxis axis) { + bonus.scheduleDeletion(); + }, func[bonusType - 1], 0.25, .75) + .addComponent(timer, [](WAL::Entity &bonus, WAL::Wal &wal){ + bonus.scheduleDeletion(); + }) + .addComponent(map.at(bonusType) + ".obj", std::make_pair(MAP_DIFFUSE, "assets/items/items.png")); } catch (std::out_of_range const &err) {} } diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index cdabb452..e735c1e9 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -10,13 +10,16 @@ #include "Component/Renderer/Drawable2DComponent.hpp" #include "Drawables/ADrawable3D.hpp" + +#include "Component/Collision/CollisionComponent.hpp" + namespace BBM { RenderSystem::RenderSystem(WAL::Wal &wal, RAY::Window &window, bool debugMode) : System(wal), - _window(window), - _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE), - _debugMode(debugMode) + _window(window), + _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE), + _debugMode(debugMode) { this->_window.setFPS(this->FPS); } @@ -44,7 +47,8 @@ namespace BBM this->_window.endDrawing(); } - void RenderSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) + void RenderSystem::onUpdate(WAL::ViewEntity &entity, + std::chrono::nanoseconds dtime) { const auto &pos = entity.get(); const auto &cam = entity.get(); diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 394b1ccc..34bff636 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -8,6 +8,7 @@ #include "Wal.hpp" #define private public + #include "System/Collision/CollisionSystem.hpp" #include "System/Movable/MovableSystem.hpp" #include "Component/Movable/MovableComponent.hpp" @@ -24,14 +25,14 @@ TEST_CASE("Collision test", "[Component][System]") wal.scene = std::make_shared(); wal.scene->addEntity("player") .addComponent() - .addComponent([](Entity &actual, const Entity &) { + .addComponent([](Entity &actual, const Entity &, int _) { try { - auto &pos = actual.getComponent(); - pos.position.x = 1; - pos.position.y = 1; - pos.position.z = 1; + auto &pos = actual.getComponent(); + pos.position.x = 1; + pos.position.y = 1; + pos.position.z = 1; } catch (std::exception &e) {}; - }, [](Entity &, const Entity &){}, 5.0); + }, [](Entity &, const Entity &, int) {}, 0, 5.0); Entity &entity = wal.scene->getEntities().front(); REQUIRE(entity.getComponent().position == Vector3f()); @@ -44,10 +45,10 @@ TEST_CASE("Collision test", "[Component][System]") REQUIRE(entity.getComponent().position.x == 0.0); REQUIRE(entity.getComponent().position.y == 0.0); REQUIRE(entity.getComponent().position.z == 0.0); - + wal.scene->addEntity("block") - .addComponent(2,2,2) - .addComponent(1); + .addComponent(2, 2, 2) + .addComponent(0, 1); Entity &player = wal.scene->getEntities().front(); collision.update(std::chrono::nanoseconds(1)); REQUIRE(player.hasComponent(typeid(PositionComponent))); @@ -68,17 +69,19 @@ TEST_CASE("Collsion test with movable", "[Component][System]") wal.scene = std::make_shared(); wal.scene->addEntity("player") .addComponent() - .addComponent([](Entity &actual, const Entity &) {}, [](Entity &actual, const Entity &) {}, 5.0) + .addComponent([](Entity &actual, const Entity &, int) {}, + [](Entity &actual, const Entity &, int) {}, 0, 5.0) .addComponent(); - + wal.scene->addEntity("block") .addComponent(0, 0, 0) - .addComponent([](Entity &actual, const Entity &){}, [](Entity &actual, const Entity &) { - try { - auto &mov = actual.getComponent(); - mov._velocity = Vector3f(); - } catch (std::exception &e) {}; - }, 1); + .addComponent([](Entity &actual, const Entity &, int) {}, + [](Entity &actual, const Entity &, int) { + 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());