diff --git a/CMakeLists.txt b/CMakeLists.txt index 63493adc..d6d3f606 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,8 +25,6 @@ set(SOURCES sources/Map/Map.hpp sources/Items/Bonus.cpp sources/Items/Bonus.hpp - sources/Component/Bonus/BonusComponent.cpp - sources/Component/Bonus/BonusComponent.hpp sources/Component/Position/PositionComponent.cpp sources/Component/Position/PositionComponent.hpp sources/Component/Movable/MovableComponent.cpp @@ -41,8 +39,6 @@ set(SOURCES sources/Component/Keyboard/KeyboardComponent.hpp sources/Component/Health/HealthComponent.cpp sources/Component/Health/HealthComponent.hpp - sources/Component/Bonus/BonusComponent.cpp - sources/Component/Bonus/BonusComponent.hpp sources/System/Movable/MovableSystem.hpp sources/System/Movable/MovableSystem.cpp sources/System/Controllable/ControllableSystem.cpp diff --git a/sources/Component/Bonus/BonusComponent.cpp b/sources/Component/Bonus/BonusComponent.cpp deleted file mode 100644 index c56e81e6..00000000 --- a/sources/Component/Bonus/BonusComponent.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// -// Created by Benjamin Henry on 2021-06-01. -// - -#include "BonusComponent.hpp" -#include - -namespace BBM { - BonusComponent::BonusComponent(WAL::Entity &entity) - : WAL::Component(entity) - {} - - WAL::Component *BonusComponent::clone(WAL::Entity &entity) const - { - return new BonusComponent(entity); - } - - BonusComponent::BonusType BonusComponent::getRandomBonusType() const - { - double rnd = static_cast(std::rand()) / RAND_MAX; - - if (rnd < 0.4) - return (static_cast(std::rand() % (DAMAGEINC - 1) + 1)); - return (NOTHING); - } -} \ No newline at end of file diff --git a/sources/Component/Bonus/BonusComponent.hpp b/sources/Component/Bonus/BonusComponent.hpp deleted file mode 100644 index 94284c23..00000000 --- a/sources/Component/Bonus/BonusComponent.hpp +++ /dev/null @@ -1,46 +0,0 @@ -// -// Created by Benjamin Henry on 2021-06-01. -// - -#pragma once - -#include "Component/Component.hpp" -#include "Entity/Entity.hpp" -#include - -using namespace std::chrono_literals; - -namespace BBM -{ - class BonusComponent : public WAL::Component - { - public: - - enum BonusType { - NOTHING, - BOMBSTOCK, - SPEEDUP, - EXPLOSIONINC, - DAMAGEINC - }; - - std::chrono::nanoseconds disappearTimer = 5s; - - BonusType getRandomBonusType() const; - - //! @inherit - WAL::Component *clone(WAL::Entity &entity) const override; - - //! @brief A Bonus component can't be instantiated, it should be derived. - explicit BonusComponent(WAL::Entity &entity); - - //! @brief A Bonus component can't be instantiated, it should be derived. - BonusComponent(const BonusComponent &) = default; - - //! @brief default destructor - ~BonusComponent() override = default; - - //! @brief A Bonus component can't be assigned - BonusComponent &operator=(const BonusComponent &) = delete; - }; -} \ No newline at end of file diff --git a/sources/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp index 8901ab31..9063520b 100644 --- a/sources/Component/Health/HealthComponent.cpp +++ b/sources/Component/Health/HealthComponent.cpp @@ -10,7 +10,7 @@ namespace BBM { - HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint, const WAL::Callback &onDeath) + HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint, const WAL::Callback &onDeath) : WAL::Component(entity), _healthPoint(healthPoint), onDeath(onDeath) diff --git a/sources/Component/Health/HealthComponent.hpp b/sources/Component/Health/HealthComponent.hpp index 29c202d4..3be3c979 100644 --- a/sources/Component/Health/HealthComponent.hpp +++ b/sources/Component/Health/HealthComponent.hpp @@ -9,6 +9,7 @@ #include #include "Component/Component.hpp" #include "Entity/Entity.hpp" +#include "Wal.hpp" namespace BBM { @@ -21,7 +22,7 @@ namespace BBM public: //! @brief The callback invoked on this entity's death. - WAL::Callback onDeath; + WAL::Callback onDeath; //! @brief add health to the entity void addHealthPoint(unsigned int healthPoint); @@ -36,7 +37,7 @@ namespace BBM WAL::Component *clone(WAL::Entity &entity) const override; //! @brief Constructor - HealthComponent(WAL::Entity &entity, unsigned int healthPoint, const WAL::Callback &onDeath = WAL::Callback()); + HealthComponent(WAL::Entity &entity, unsigned int healthPoint, const 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/Items/Bonus.cpp b/sources/Items/Bonus.cpp index 2dec424c..97a4002b 100644 --- a/sources/Items/Bonus.cpp +++ b/sources/Items/Bonus.cpp @@ -14,46 +14,53 @@ namespace BBM { { if (bonus.shouldDelete()) return; - if (player.hasComponent()) { - auto &bombHolder = player.getComponent(); - bombHolder.maxBombCount++; - } + auto *bombHolder = player.tryGetComponent(); + if (bombHolder == nullptr) + return; + bombHolder->maxBombCount++; } void Bonus::DamageIncreasedBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { if (bonus.shouldDelete()) return; - if (player.hasComponent()) { - auto &bombHolder = player.getComponent(); - auto &playerBonus = player.getComponent(); - bombHolder.damage = 2; - playerBonus.nextDamageBonusRate = playerBonus.damageBonusRate; - } + auto *bombHolder = player.tryGetComponent(); + if (bombHolder == nullptr) + return; + auto &playerBonus = player.getComponent(); + bombHolder->damage = 2; + playerBonus.nextDamageBonusRate = playerBonus.damageBonusRate; } void Bonus::ExplosionRangeBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { if (bonus.shouldDelete()) return; - if (player.hasComponent()) { - auto &bombHolder = player.getComponent(); - auto &playerBonus = player.getComponent(); - if (bombHolder.explosionRadius <= 6) - bombHolder.explosionRadius++; - playerBonus.nextRangeBonusRate = playerBonus.rangeBonusRate; - } + auto *bombHolder = player.tryGetComponent(); + if (bombHolder == nullptr) + return; + auto &playerBonus = player.getComponent(); + if (bombHolder->explosionRadius <= 6) + bombHolder->explosionRadius++; + playerBonus.nextRangeBonusRate = playerBonus.rangeBonusRate; } void Bonus::SpeedUpBonus(WAL::Entity &player, const WAL::Entity &bonus, CollisionComponent::CollidedAxis axis) { if (bonus.shouldDelete()) return; - if (!player.hasComponent()) - return; - auto &controllable = player.getComponent(); + auto *controllable = player.tryGetComponent(); auto &playerBonus = player.getComponent(); - controllable.speed = 0.35f; + controllable->speed = 0.35f; playerBonus.nextSpeedBonusRate = playerBonus.speedBonusRate; } + + Bonus::BonusType Bonus::getRandomBonusType() + { + double rnd = static_cast(std::rand()) / RAND_MAX; + + if (rnd < 0.4) + return (static_cast(std::rand() % (DAMAGEINC - 1) + 1)); + return (NOTHING); + } } \ No newline at end of file diff --git a/sources/Items/Bonus.hpp b/sources/Items/Bonus.hpp index 185b5a26..25fa182c 100644 --- a/sources/Items/Bonus.hpp +++ b/sources/Items/Bonus.hpp @@ -29,5 +29,15 @@ namespace BBM { //! @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, CollisionComponent::CollidedAxis axis); + + enum BonusType { + NOTHING, + BOMBSTOCK, + SPEEDUP, + EXPLOSIONINC, + DAMAGEINC + }; + + static BonusType getRandomBonusType(); }; } \ No newline at end of file diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 516f1ba0..cae0eda9 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -7,9 +7,12 @@ #include "System/Collision/CollisionSystem.hpp" #include "Map.hpp" #include -#include +#include +#include +#include namespace RAY3D = RAY::Drawables::Drawables3D; +using namespace std::chrono_literals; namespace BBM { @@ -29,9 +32,39 @@ namespace BBM mov->_velocity.z = 0; } - void MapGenerator::wallDestroyed(WAL::Entity &entity) + void MapGenerator::wallDestroyed(WAL::Entity &entity, WAL::Wal &wal) { entity.scheduleDeletion(); + auto &position = entity.getComponent().position; + static std::map map = { + {Bonus::BonusType::BOMBSTOCK, "assets/items/bombup"}, + {Bonus::BonusType::SPEEDUP, "assets/items/speedup"}, + //{BonusComponent::BonusType::EXPLOSIONINC, "assets/items/explosion"}, + {Bonus::BonusType::DAMAGEINC, "assets/items/fireup"} + }; + static std::vector> func = { + &Bonus::BombUpBonus, &Bonus::SpeedUpBonus, //&Bonus::ExplosionRangeBonus, + &Bonus::DamageIncreasedBonus + }; + auto bonusType = Bonus::getRandomBonusType(); + + if (bonusType == Bonus::BonusType::NOTHING) + return; + if (!map.contains(bonusType)) + return; + wal.scene->scheduleNewEntity("Bonus") + .addComponent(position) + .addComponent(1, [](WAL::Entity &entity, WAL::Wal &wal) { + 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(5s, [](WAL::Entity &bonus, WAL::Wal &wal){ + bonus.scheduleDeletion(); + }) + .addComponent(map.at(bonusType) + ".obj", std::make_pair(MAP_DIFFUSE, "assets/items/items.png")); } const std::string MapGenerator::assetsPath = "./assets/"; @@ -150,7 +183,6 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1, &MapGenerator::wallDestroyed) - .addComponent() .addComponent( WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 4a961cbc..a6c9c5ed 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -21,6 +21,9 @@ #include "Component/Health/HealthComponent.hpp" #include "Component/Collision/CollisionComponent.hpp" #include "Component/Movable/MovableComponent.hpp" +#include + + namespace BBM { @@ -162,7 +165,7 @@ namespace BBM static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall, CollisionComponent::CollidedAxis collidedAxis); - static void wallDestroyed(WAL::Entity &entity); + static void wallDestroyed(WAL::Entity &entity, WAL::Wal &wal); //! @param width Width of the map diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 2fd9a5af..201c677a 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -87,7 +87,7 @@ namespace BBM .addComponent() .addComponent() .addComponent() - .addComponent(1, [](WAL::Entity &entity) { + .addComponent(1, [](WAL::Entity &entity, WAL::Wal &wal) { auto &animation = entity.getComponent(); animation.setAnimIndex(5); }); diff --git a/sources/System/Bonus/PlayerBonusSystem.hpp b/sources/System/Bonus/PlayerBonusSystem.hpp index 6f0a08fa..b454ce6e 100644 --- a/sources/System/Bonus/PlayerBonusSystem.hpp +++ b/sources/System/Bonus/PlayerBonusSystem.hpp @@ -4,8 +4,8 @@ #pragma once -#include -#include +#include "System/System.hpp" +#include "Wal.hpp" #include "Models/Vector3.hpp" #include "Component/Bonus/PlayerBonusComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index 0714fddc..16c82d82 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -14,7 +14,7 @@ namespace BBM : System(wal) {} - void ControllableSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) + void ControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) { auto &controllable = entity.get(); auto &movable = entity.get(); diff --git a/sources/System/Controllable/ControllableSystem.hpp b/sources/System/Controllable/ControllableSystem.hpp index 268515c1..0a0d8c21 100644 --- a/sources/System/Controllable/ControllableSystem.hpp +++ b/sources/System/Controllable/ControllableSystem.hpp @@ -19,7 +19,7 @@ namespace BBM public: //! @inherit - void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit ControllableSystem(WAL::Wal &wal); diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 195aca97..d73f4ae2 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -12,7 +12,6 @@ #include #include "HealthSystem.hpp" #include "Component/Health/HealthComponent.hpp" -#include "Component/Bonus/BonusComponent.hpp" #include "Entity/Entity.hpp" #include "Items/Bonus.hpp" @@ -24,50 +23,13 @@ namespace BBM : System(wal) {} - void HealthSystem::_createBonus(Vector3f position, BonusComponent::BonusType bonusType, std::chrono::nanoseconds timer) - { - static std::map map = { - {BonusComponent::BonusType::BOMBSTOCK, "assets/items/bombup"}, - {BonusComponent::BonusType::SPEEDUP, "assets/items/speedup"}, - //{BonusComponent::BonusType::EXPLOSIONINC, "assets/items/explosion"}, - {BonusComponent::BonusType::DAMAGEINC, "assets/items/fireup"} - }; - static std::vector> func = { - &Bonus::BombUpBonus, &Bonus::SpeedUpBonus, //&Bonus::ExplosionRangeBonus, - &Bonus::DamageIncreasedBonus - }; - - if (bonusType == BonusComponent::BonusType::NOTHING) - 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, 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) {} - } - void HealthSystem::onFixedUpdate(WAL::ViewEntity &entity) { auto &health = entity.get(); auto &position = entity.get(); if (health.getHealthPoint() == 0) { - if (entity->hasComponent() && !entity->shouldDelete()) { - auto &bonus = entity->getComponent(); - auto bonusType = bonus.getRandomBonusType(); - this->_createBonus(position.position, bonusType, bonus.disappearTimer); - } - health.onDeath(entity); + health.onDeath(entity, this->_wal); } } } \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.hpp b/sources/System/Health/HealthSystem.hpp index acfcd210..044e0347 100644 --- a/sources/System/Health/HealthSystem.hpp +++ b/sources/System/Health/HealthSystem.hpp @@ -5,7 +5,6 @@ #pragma once -#include #include "Models/Vector3.hpp" #include "Wal.hpp" #include "Component/Health/HealthComponent.hpp" @@ -17,9 +16,7 @@ namespace BBM class HealthSystem : public WAL::System { private: - //! @brief Spawn a bonus at the specified position. - void _createBonus(Vector3f position, BonusComponent::BonusType bonusType, std::chrono::nanoseconds timer); - public: + public: //! @inherit void onFixedUpdate(WAL::ViewEntity &entity) override;