Adding a bomb holder system

This commit is contained in:
Zoe Roux
2021-05-31 16:18:24 +02:00
parent d034514e28
commit 43bd781175
8 changed files with 121 additions and 53 deletions
+1 -1
View File
@@ -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
@@ -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;
}
}
@@ -8,30 +8,23 @@
#include "Component/Component.hpp"
#include "Entity/Entity.hpp"
#include <chrono>
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;
+17 -13
View File
@@ -4,20 +4,22 @@
#include <Wal.hpp>
#include <iostream>
#include <System/Movable/MovableSystem.hpp>
#include <System/Renderer/RenderScreenSystem.hpp>
#include <System/Renderer/Render2DScreenSystem.hpp>
#include <System/Renderer/Renderer2DSystem.hpp>
#include "System/Movable/MovableSystem.hpp"
#include "System/Renderer/RenderScreenSystem.hpp"
#include "System/Renderer/Render2DScreenSystem.hpp"
#include "System/Renderer/Renderer2DSystem.hpp"
#include <Model/Model.hpp>
#include <Drawables/2D/Rectangle.hpp>
#include <TraceLog.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 "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<KeyboardSystem>()
.addSystem<GamepadSystem>()
.addSystem<ControllableSystem>()
.addSystem<BombHolderSystem>(wal)
.addSystem<MovableSystem>();
}
@@ -66,13 +69,14 @@ namespace BBM
.addComponent<Drawable2DComponent<RAY2D::Rectangle>>(Vector2f(), Vector2f(10, 10), RED)
.addComponent<ControllableComponent>()
.addComponent<KeyboardComponent>()
.addComponent<MovableComponent>();;
.addComponent<MovableComponent>();
scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
.addComponent<ControllableComponent>()
.addComponent<KeyboardComponent>()
.addComponent<MovableComponent>();
.addComponent<MovableComponent>()
.addComponent<BombHolderComponent>();
scene->addEntity("camera")
.addComponent<PositionComponent>(0, 20, -5)
.addComponent<CameraComponent>();
@@ -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<PositionComponent>(position)
.addComponent<Drawable3DComponent<RAY3D::Model>>("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<BombHolderComponent>();
auto &position = entity.getComponent<PositionComponent>();
auto &controllable = entity.getComponent<ControllableComponent>();
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);
}
}
}
@@ -0,0 +1,34 @@
//
// Created by Zoe Roux on 5/31/21.
//
#pragma once
#include <System/System.hpp>
#include <Wal.hpp>
#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;
};
}
+2 -3
View File
@@ -3,7 +3,6 @@
// Edited by Benjamin Henry on 2021-05-20.
//
#include <iostream>
#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<KeyboardComponent>();
auto &controllable = entity.getComponent<ControllableComponent>();
@@ -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;
+1 -1
View File
@@ -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();