diff --git a/CMakeLists.txt b/CMakeLists.txt index f43e51f3..47411480 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,10 @@ set(SOURCES sources/Runner/Runner.hpp sources/Map/Map.cpp sources/Map/Map.hpp + sources/Bonus/Bonus.cpp + sources/Bonus/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 @@ -28,6 +32,8 @@ 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/Bonus/Bonus.cpp b/sources/Bonus/Bonus.cpp new file mode 100644 index 00000000..4db1b2c6 --- /dev/null +++ b/sources/Bonus/Bonus.cpp @@ -0,0 +1,38 @@ +// +// Created by HENRY Benjamin on 02/06/2021. +// + +#include "Bonus.hpp" +//#include "Component/BombHolderComponent/BombHolderComponent.hpp" + +namespace BBM { + void Bonus::BombUpBonus(const WAL::Entity &entity, const WAL::Entity &other) + { + auto &bombHolder = other.getComponent(); + bombHolder.maxBombCount++; + } + + void Bonus::DamageIncreasedBonus(const WAL::Entity &entity, const WAL::Entity &other) + { + auto &bombHolder = other.getComponent(); + //bombHolder.damage++; + } + + void Bonus::ExplosionRangeBonus(const WAL::Entity &entity, const WAL::Entity &other) + { + auto &bombHolder = other.getComponent(); + //bombHolder.explosionRange++; + } + + void Bonus::SpeedUpBonus(const WAL::Entity &entity, const WAL::Entity &other) + { + auto &movable = other.getComponent(); + movable.addForce(Vector3f(1, 0, 1)); + } + + void Bonus::IgnoreWallsBonus(const WAL::Entity &entity, const WAL::Entity &other) + { + auto &bombHolder = other.getComponent(); + //bombHolder.ignoreWall = false; + } +} \ No newline at end of file diff --git a/sources/Bonus/Bonus.hpp b/sources/Bonus/Bonus.hpp new file mode 100644 index 00000000..46d4d5e1 --- /dev/null +++ b/sources/Bonus/Bonus.hpp @@ -0,0 +1,18 @@ +// +// Created by HENRY Benjamin on 02/06/2021. +// + +#pragma once + +#include "Entity/Entity.hpp" + +namespace BBM { + class Bonus { + public: + static void BombUpBonus(const WAL::Entity &entity, const WAL::Entity &other); + static void DamageIncreasedBonus(const WAL::Entity &entity, const WAL::Entity &other); + static void ExplosionRangeBonus(const WAL::Entity &entity, const WAL::Entity &other); + static void SpeedUpBonus(const WAL::Entity &entity, const WAL::Entity &other); + static void IgnoreWallsBonus(const WAL::Entity &entity, const WAL::Entity &other); + }; +} \ No newline at end of file diff --git a/sources/Component/Bonus/BonusComponent.cpp b/sources/Component/Bonus/BonusComponent.cpp new file mode 100644 index 00000000..c2789f1c --- /dev/null +++ b/sources/Component/Bonus/BonusComponent.cpp @@ -0,0 +1,21 @@ +// +// Created by Benjamin Henry on 2021-06-01. +// + +#include "BonusComponent.hpp" + +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 + { + return (static_cast(std::rand() % IGNOREWALLS)); + } +} \ No newline at end of file diff --git a/sources/Component/Bonus/BonusComponent.hpp b/sources/Component/Bonus/BonusComponent.hpp new file mode 100644 index 00000000..9e12cc10 --- /dev/null +++ b/sources/Component/Bonus/BonusComponent.hpp @@ -0,0 +1,47 @@ +// +// 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, + IGNOREWALLS + }; + + std::chrono::nanoseconds disappearTimer = 10s; + + 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/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 90bbfbb9..0f24ee91 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -3,24 +3,66 @@ // Edited by Benjamin Henry on 2021-05-20. // +#include +#include +#include #include "HealthSystem.hpp" #include "Component/Health/HealthComponent.hpp" -#include "Component/Controllable/ControllableComponent.hpp" +#include "Component/Bonus/BonusComponent.hpp" #include "Entity/Entity.hpp" +#include "Bonus/Bonus.hpp" + +namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { - HealthSystem::HealthSystem() + HealthSystem::HealthSystem(WAL::Wal &wal) : WAL::System({ typeid(HealthComponent) - }) + }), + _wal(wal) {} + void HealthSystem::_createBonus(Vector3f position, BonusComponent::BonusType bonusType, std::chrono::nanoseconds timer) + { + 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"}, + {BonusComponent::BonusType::IGNOREWALLS, "assets/items/wallpass"} + }; + std::vector> func = { + &Bonus::BombUpBonus, &Bonus::SpeedUpBonus, &Bonus::ExplosionRangeBonus, + &Bonus::DamageIncreasedBonus, &Bonus::IgnoreWallsBonus + }; + + if (bonusType == BonusComponent::BonusType::NOTHING) + return; + std::cout << "Bonus spawned" << std::endl; + this->_wal.scene->addEntity("Bonus") + .addComponent(position) + .addComponent(1) + //.addComponent(1, func[bonusType -1]) + //.addComponent(timer, &[](WAL::Entity &bonus){ + // std::cout << "Bonus disappeared" << std::endl; + // bonus.scheduleDeletion(); + // }) + .addComponent>(map.at(bonusType) + ".obj", std::make_pair(MAP_DIFFUSE, "assets/items/items.png")); + } + void HealthSystem::onFixedUpdate(WAL::Entity &entity) { auto &health = entity.getComponent(); + auto &position = entity.getComponent(); - if (health.getHealthPoint() == 0) + if (health.getHealthPoint() == 0) { + if (entity.hasComponent()) { + auto &bonus = entity.getComponent(); + auto bonusType = bonus.getRandomBonusType(); + this->_createBonus(position.position, bonusType, bonus.disappearTimer); + } health.onDeath(entity); + } } } \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.hpp b/sources/System/Health/HealthSystem.hpp index 15ca2062..3b59b1ca 100644 --- a/sources/System/Health/HealthSystem.hpp +++ b/sources/System/Health/HealthSystem.hpp @@ -5,6 +5,9 @@ #pragma once +#include +#include "Models/Vector3.hpp" +#include "Wal.hpp" #include "System/System.hpp" namespace BBM @@ -12,12 +15,18 @@ namespace BBM //! @brief A system to handle Health entities. class HealthSystem : public WAL::System { + private: + //! @brief A reference to the engine to spawn new entities. + WAL::Wal &_wal; + + //! @brief Spawn a bonus at the specified position. + void _createBonus(Vector3f position, BonusComponent::BonusType bonusType, std::chrono::nanoseconds timer); public: //! @inherit void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor - HealthSystem(); + HealthSystem(WAL::Wal &wal); //! @brief A Health system is copy constructable HealthSystem(const HealthSystem &) = default; //! @brief A default destructor