diff --git a/CMakeLists.txt b/CMakeLists.txt index 2371cd9e..9ff217e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,7 +56,7 @@ set(SOURCES sources/Component/Renderer/CameraComponent.hpp sources/System/Renderer/Render2DScreenSystem.cpp sources/System/Renderer/Render2DScreenSystem.hpp -) + sources/System/BombHolder/BombHolderSystem.cpp sources/System/BombHolder/BombHolderSystem.hpp) add_executable(bomberman sources/main.cpp diff --git a/sources/Component/BombHolder/BombHolderComponent.cpp b/sources/Component/BombHolder/BombHolderComponent.cpp index 031dab8b..bff44867 100644 --- a/sources/Component/BombHolder/BombHolderComponent.cpp +++ b/sources/Component/BombHolder/BombHolderComponent.cpp @@ -9,31 +9,16 @@ namespace BBM { BombHolderComponent::BombHolderComponent(WAL::Entity &entity) - : WAL::Component(entity), - _bombCount() + : WAL::Component(entity) {} BombHolderComponent::BombHolderComponent(WAL::Entity &entity, unsigned int maxBombCount) : WAL::Component(entity), - _bombCount(), - _maxBombCount(maxBombCount) + maxBombCount(maxBombCount) {} WAL::Component *BombHolderComponent::clone(WAL::Entity &entity) const { return new BombHolderComponent(entity); } - - void BombHolderComponent::addBomb(unsigned int bombCount) - { - this->_bombCount += bombCount; - } - - void BombHolderComponent::removeBomb(unsigned int damage) - { - if (damage >= this->_bombCount) { - this->_bombCount = 0; - } else - this->_bombCount -= damage; - } } \ No newline at end of file diff --git a/sources/Component/BombHolder/BombHolderComponent.hpp b/sources/Component/BombHolder/BombHolderComponent.hpp index 049fd56c..b35e9620 100644 --- a/sources/Component/BombHolder/BombHolderComponent.hpp +++ b/sources/Component/BombHolder/BombHolderComponent.hpp @@ -8,30 +8,23 @@ #include "Component/Component.hpp" #include "Entity/Entity.hpp" +#include + +using namespace std::chrono_literals; namespace BBM { class BombHolderComponent : public WAL::Component { - - private: - //! @brief bomb count of an entity - unsigned int _bombCount; - //! @brief max bomb count of an entity - unsigned int _maxBombCount; - public: - //! @brief add bomb to the entity - void addBomb(unsigned int bombCount); - - //! @brief add bomb bax of the entity - void addMaxBombCount(unsigned int maxBombCount); - - //! @brief reduce bomb max of the entity - void removeMaxBombCount(unsigned int bombCount); - - //! @brief reduce bomb - void removeBomb(unsigned int bombCount); + //! @brief The number of bomb that this entity hold. + unsigned int bombCount = 1; + //! @brief The max number of bomb that this entity can have. + unsigned int maxBombCount = 3; + //! @brief The number of seconds of each refill. This variable is used to reset the nextBombRefill value. + std::chrono::nanoseconds refillRate = 5000ns; + //! @brief The number of nanosecond before the next bomb refill. + std::chrono::nanoseconds nextBombRefill = refillRate; //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index c6a6c196..a8a321fa 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -4,20 +4,22 @@ #include #include -#include -#include -#include -#include +#include "System/Movable/MovableSystem.hpp" +#include "System/Renderer/RenderScreenSystem.hpp" +#include "System/Renderer/Render2DScreenSystem.hpp" +#include "System/Renderer/Renderer2DSystem.hpp" #include #include #include -#include -#include -#include -#include -#include -#include -#include +#include "Component/BombHolder/BombHolderComponent.hpp" +#include "System/BombHolder/BombHolderSystem.hpp" +#include "System/Renderer/Renderer3DSystem.hpp" +#include "System/Keyboard/KeyboardSystem.hpp" +#include "System/Controllable/ControllableSystem.hpp" +#include "Component/Movable/MovableComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" +#include "Component/Keyboard/KeyboardComponent.hpp" +#include "System/Gamepad/GamepadSystem.hpp" #include "Models/Vector2.hpp" #include "Component/Renderer/CameraComponent.hpp" #include "Runner.hpp" @@ -43,6 +45,7 @@ namespace BBM wal.addSystem() .addSystem() .addSystem() + .addSystem(wal) .addSystem(); } @@ -66,13 +69,14 @@ namespace BBM .addComponent>(Vector2f(), Vector2f(10, 10), RED) .addComponent() .addComponent() - .addComponent();; + .addComponent(); scene->addEntity("player") .addComponent() .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() - .addComponent(); + .addComponent() + .addComponent(); scene->addEntity("camera") .addComponent(0, 20, -5) .addComponent(); diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp new file mode 100644 index 00000000..1132c3be --- /dev/null +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -0,0 +1,53 @@ +// +// Created by Zoe Roux on 5/31/21. +// + +#include "Component/Renderer/Drawable3DComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" +#include "BombHolderSystem.hpp" +#include "Component/Position/PositionComponent.hpp" +#include "Component/BombHolder/BombHolderComponent.hpp" + +using namespace std::chrono_literals; +namespace RAY3D = RAY::Drawables::Drawables3D; + +namespace BBM +{ + BombHolderSystem::BombHolderSystem(WAL::Wal &wal) + : WAL::System({ + typeid(PositionComponent), + typeid(BombHolderComponent), + typeid(ControllableComponent) + }), + _wal(wal) + {} + + void BombHolderSystem::_spawnBomb(Vector3f position) + { + this->_wal.scene->addEntity("Bomb") + .addComponent(position) + .addComponent>("assets/bombs/bomb.obj", + std::make_pair(MAP_DIFFUSE, "assets/bombs/bomb_normal.png")); + } + + void BombHolderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) + { + auto &holder = entity.getComponent(); + auto &position = entity.getComponent(); + auto &controllable = entity.getComponent(); + + if (holder.bombCount < holder.maxBombCount) { + holder.nextBombRefill -= dtime; + if (holder.nextBombRefill <= 0ns) { + holder.nextBombRefill = holder.refillRate; + holder.bombCount++; + std::cout << "Count: " << holder.bombCount << std::endl; + } + } + if (controllable.bomb && holder.bombCount > 0) { + holder.bombCount--; + std::cout << "Now" << std::endl; + this->_spawnBomb(position.position); + } + } +} \ No newline at end of file diff --git a/sources/System/BombHolder/BombHolderSystem.hpp b/sources/System/BombHolder/BombHolderSystem.hpp new file mode 100644 index 00000000..1241c9d9 --- /dev/null +++ b/sources/System/BombHolder/BombHolderSystem.hpp @@ -0,0 +1,34 @@ +// +// Created by Zoe Roux on 5/31/21. +// + +#pragma once + +#include +#include +#include "Models/Vector3.hpp" + +namespace BBM +{ + //! @brief The system that allow one to place bombs. + class BombHolderSystem : public WAL::System + { + private: + //! @brief A reference to the engine to spawn new entities. + WAL::Wal &_wal; + //! @brief Spawn a bomb at the specified position. + void _spawnBomb(Vector3f position); + public: + //! @inherit + void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; + + //! @brief A default constructor + explicit BombHolderSystem(WAL::Wal &wal); + //! @brief A bomb holder system is copy constructable + BombHolderSystem(const BombHolderSystem &) = default; + //! @brief A default destructor + ~BombHolderSystem() override = default; + //! @brief A bomb holder system is not assignable. + BombHolderSystem &operator=(const BombHolderSystem &) = delete; + }; +} diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 13ad9804..a6c5963b 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -3,7 +3,6 @@ // Edited by Benjamin Henry on 2021-05-20. // -#include #include "KeyboardSystem.hpp" #include "Component/Keyboard/KeyboardComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" @@ -21,7 +20,7 @@ namespace BBM }) {} - void KeyboardSystem::onFixedUpdate(WAL::Entity &entity) + void KeyboardSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) { const auto &keyboard = entity.getComponent(); auto &controllable = entity.getComponent(); @@ -33,7 +32,7 @@ namespace BBM }; for (auto key : keyPressedMap) - key.second = Keyboard::isDown(key.first); + key.second = Keyboard::isPressed(key.first); controllable.move = Vector2f(); if (Keyboard::isDown(keyboard.keyRight)) controllable.move.x += 1; diff --git a/sources/System/Keyboard/KeyboardSystem.hpp b/sources/System/Keyboard/KeyboardSystem.hpp index f17c5f15..5aba771e 100644 --- a/sources/System/Keyboard/KeyboardSystem.hpp +++ b/sources/System/Keyboard/KeyboardSystem.hpp @@ -15,7 +15,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override; //! @brief A default constructor KeyboardSystem();