From fe9a650427e8709c41700f188afd1bc078795fa8 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 7 Jun 2021 21:47:04 +0200 Subject: [PATCH] Making walls breakables --- sources/Component/Health/HealthComponent.cpp | 5 +++-- sources/Component/Health/HealthComponent.hpp | 2 +- sources/Map/Map.cpp | 7 ++++++- sources/Map/Map.hpp | 1 + sources/System/BombHolder/BombHolderSystem.cpp | 6 ++++-- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/sources/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp index fe81fabb..80c9ef7e 100644 --- a/sources/Component/Health/HealthComponent.cpp +++ b/sources/Component/Health/HealthComponent.cpp @@ -13,9 +13,10 @@ namespace BBM _healthPoint() {} - HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint) + HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint, WAL::Callback onDeath) : WAL::Component(entity), - _healthPoint(healthPoint) + _healthPoint(healthPoint), + onDeath(onDeath) {} WAL::Component *HealthComponent::clone(WAL::Entity &entity) const diff --git a/sources/Component/Health/HealthComponent.hpp b/sources/Component/Health/HealthComponent.hpp index 2eadafc5..c613c6b3 100644 --- a/sources/Component/Health/HealthComponent.hpp +++ b/sources/Component/Health/HealthComponent.hpp @@ -39,7 +39,7 @@ namespace BBM explicit HealthComponent(WAL::Entity &entity); //! @brief Constructor - HealthComponent(WAL::Entity &entity, unsigned int healthPoint); + HealthComponent(WAL::Entity &entity, unsigned int healthPoint, WAL::Callback onDeath = WAL::Callback()); //! @brief A Health component can't be instantiated, it should be derived. HealthComponent(const HealthComponent &) = default; diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 4386546a..ed26beb0 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -30,6 +30,11 @@ namespace BBM // mov->_velocity.z = 0; } + void MapGenerator::wallDestroyed(WAL::Entity &entity) + { + entity.scheduleDeletion(); + } + const std::string MapGenerator::assetsPath = "./assets/"; const std::string MapGenerator::wallAssetsPath = MapGenerator::assetsPath + "map/"; const std::string MapGenerator::imageExtension = ".png"; @@ -133,7 +138,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) - .addComponent(1) + .addComponent(1, &MapGenerator::wallDestroyed) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 0e54ea4c..49e82e5b 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -155,6 +155,7 @@ namespace BBM public: static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall); + static void wallDestroyed(WAL::Entity &entity); //! @param width Width of the map diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index 621c87b2..45b28069 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -31,8 +31,10 @@ namespace BBM if (!health || !pos) return; - if (pos->position.distance(bombPosition.position) <= BombHolderSystem::explosionRadius) - health->takeDmg(1); + if (pos->position.distance(bombPosition.position) > BombHolderSystem::explosionRadius) + return; + // TODO do a raycast here to only remove health to entities that are not behind others. + health->takeDmg(1); }); }