Finishing exploson radius

This commit is contained in:
Zoe Roux
2021-06-09 12:10:19 +02:00
parent c69c87e910
commit a6b4f1717a
7 changed files with 94 additions and 19 deletions
+54
View File
@@ -0,0 +1,54 @@
//
// Created by Zoe Roux on 6/9/21.
//
#pragma once
#include <Component/Component.hpp>
#include <string_view>
namespace BBM
{
template<std::size_t I>
struct StringLiteral
{
public:
char value[I];
//! @brief Implicitly convert an array of char to a string literal.
constexpr StringLiteral(const char (&str)[I]) // NOLINT(google-explicit-constructor)
: value()
{
std::copy_n(str, I, value);
}
//! @brief A string literal is copy constructable.
constexpr StringLiteral(const StringLiteral &) = default;
//! @brief A default destructor
constexpr ~StringLiteral() = default;
//! @brief A string literal is assignable.
constexpr StringLiteral &operator=(const StringLiteral &) = default;
};
template <StringLiteral name>
class TagComponent : public WAL::Component
{
public:
Component *clone(WAL::Entity &entity) const override
{
return new TagComponent<name>(entity);
}
//! @brief Create a new empty tag component.
explicit TagComponent(WAL::Entity &entity)
: WAL::Component(entity)
{}
//! @brief A default copy constructor.
TagComponent(const TagComponent &) = default;
//! @brief A default destructor
~TagComponent() override = default;
//! @brief A tag component is not assignable.
TagComponent &operator=(const TagComponent &) = delete;
};
constexpr const char Blowable[] = "Blowable";
}
+8
View File
@@ -6,6 +6,7 @@
#include <Component/Collision/CollisionComponent.hpp>
#include "Map.hpp"
#include <iostream>
#include <Component/Tag/TagComponent.hpp>
namespace RAY3D = RAY::Drawables::Drawables3D;
@@ -58,6 +59,7 @@ namespace BBM
if (!(i % 2) && !(j % 2)) {
scene->addEntity("Unbreakable Wall")
.addComponent<PositionComponent>(i, 0, j)
.addComponent<TagComponent<Blowable>>()
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng));
}
@@ -72,24 +74,28 @@ namespace BBM
scene->addEntity("Bottom Wall")
.addComponent<PositionComponent>(Vector3f((width + 1) / 2, 0, -1))
.addComponent<TagComponent<Blowable>>()
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj,
std::make_pair(MAP_DIFFUSE, unbreakablePnj),
RAY::Vector3(width + 3, 1, 1));
scene->addEntity("Upper Wall")
.addComponent<PositionComponent>(Vector3f((width + 1) / 2, 0, height + 1))
.addComponent<TagComponent<Blowable>>()
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj,
std::make_pair(MAP_DIFFUSE, unbreakablePnj),
RAY::Vector3(width + 3, 1, 1));
scene->addEntity("Left Wall")
.addComponent<PositionComponent>(Vector3f(width + 1, 0, height / 2))
.addComponent<TagComponent<Blowable>>()
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj,
std::make_pair(MAP_DIFFUSE, unbreakablePnj),
RAY::Vector3(1, 1, height + 1));
scene->addEntity("Right Wall")
.addComponent<PositionComponent>(Vector3f(-1, 0, height / 2))
.addComponent<TagComponent<Blowable>>()
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj,
std::make_pair(MAP_DIFFUSE, unbreakablePnj),
@@ -141,6 +147,7 @@ namespace BBM
.addComponent<PositionComponent>(coords)
.addComponent<HealthComponent>(1, &MapGenerator::wallDestroyed)
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
.addComponent<TagComponent<Blowable>>()
.addComponent<Drawable3DComponent, RAY3D::Model>(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng));
}
@@ -173,6 +180,7 @@ namespace BBM
scene->addEntity("Unbreakable Block")
.addComponent<PositionComponent>(coords)
.addComponent<TagComponent<Blowable>>()
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
.addComponent<Drawable3DComponent, RAY3D::Model>(UnbreakableObj,
std::make_pair(MAP_DIFFUSE, UnbreakablePng));
+2
View File
@@ -28,6 +28,7 @@
#include <System/Health/HealthSystem.hpp>
#include <System/Animator/AnimatorSystem.hpp>
#include <Component/Animator/AnimatorComponent.hpp>
#include <Component/Tag/TagComponent.hpp>
#include "Component/Animation/AnimationsComponent.hpp"
#include "System/Animation/AnimationsSystem.hpp"
#include "Map/Map.hpp"
@@ -77,6 +78,7 @@ namespace BBM
.addComponent<ControllableComponent>()
.addComponent<AnimatorComponent>()
.addComponent<KeyboardComponent>()
.addComponent<TagComponent<Blowable>>()
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 3)
.addComponent<CollisionComponent>(1)
.addComponent<MovableComponent>()
+15 -18
View File
@@ -8,7 +8,8 @@
#include "BombHolderSystem.hpp"
#include "Component/Health/HealthComponent.hpp"
#include <functional>
#include <iostream>
#include "Component/Collision/CollisionComponent.hpp"
#include "Component/Tag/TagComponent.hpp"
using namespace std::chrono_literals;
namespace RAY3D = RAY::Drawables::Drawables3D;
@@ -25,22 +26,18 @@ namespace BBM
{
if (count <= 0)
return;
std::cout << position << " count: " << count << std::endl;
// TODO use it in a global context with a find and so on.
wal.getSystem<EventSystem>().dispatchEvent([position, &wal, count](WAL::Entity &entity) {
auto *health = entity.tryGetComponent<HealthComponent>();
auto *pos = entity.tryGetComponent<PositionComponent>();
if (health && pos && pos->position.round() == position) {
std::cout << pos->position << std::endl;
health->takeDmg(1);
}
else {
_dispatchExplosion(position + Vector3f(1, 0, 0), wal, count - 1);
_dispatchExplosion(position + Vector3f(-1, 0, 0), wal, count - 1);
_dispatchExplosion(position + Vector3f(0, 0, 1), wal, count - 1);
_dispatchExplosion(position + Vector3f(0, 0, -1), wal, count - 1);
wal.getSystem<EventSystem>().dispatchEvent([position, count](WAL::Wal &wal) {
for (auto &[entity, pos, _] : wal.scene->view<PositionComponent, TagComponent<Blowable>>()) {
if (pos.position.round() == position) {
if (auto *health = entity.tryGetComponent<HealthComponent>())
health->takeDmg(1);
return;
}
}
_dispatchExplosion(position + Vector3f(1, 0, 0), wal, count - 1);
_dispatchExplosion(position + Vector3f(-1, 0, 0), wal, count - 1);
_dispatchExplosion(position + Vector3f(0, 0, 1), wal, count - 1);
_dispatchExplosion(position + Vector3f(0, 0, -1), wal, count - 1);
});
}
@@ -48,13 +45,13 @@ namespace BBM
{
bomb.scheduleDeletion();
auto position = bomb.getComponent<PositionComponent>().position.round();
_dispatchExplosion(position, wal, 2);
_dispatchExplosion(position, wal, 3);
}
void BombHolderSystem::_spawnBomb(Vector3f position)
{
this->_wal.scene->scheduleNewEntity("Bomb")
.addComponent<PositionComponent>(position)
.addComponent<PositionComponent>(position.round())
.addComponent<TimerComponent>(BombHolderSystem::explosionTimer, &BombHolderSystem::_bombExplosion)
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/bombs/bomb.obj",
std::make_pair(MAP_DIFFUSE, "assets/bombs/bomb_normal.png"));
+8
View File
@@ -15,6 +15,11 @@ namespace BBM
this->_events.emplace_back(event);
}
void EventSystem::dispatchEvent(const std::function<void(WAL::Wal &)> &event)
{
this->_globalEvents.emplace_back(event);
}
void EventSystem::onUpdate(WAL::ViewEntity<> &entity, std::chrono::nanoseconds)
{
for (auto &event : this->_events)
@@ -23,6 +28,9 @@ namespace BBM
void EventSystem::onSelfUpdate()
{
for (auto &event : this->_globalEvents)
event(this->_wal);
this->_events.clear();
this->_globalEvents.clear();
}
}
+6 -1
View File
@@ -14,11 +14,16 @@ namespace BBM
{
private:
//! @brief The list of events that occurred in the last update.
std::vector<std::function<void (WAL::Entity &)>> _events;
std::list<std::function<void (WAL::Entity &)>> _events;
//! @brief The list of events that occurred in the last update.
std::list<std::function<void (WAL::Wal &)>> _globalEvents;
public:
//! @brief Inform the system that a new event has occurred and it should run the given method on every entities.
void dispatchEvent(const std::function<void (WAL::Entity &)>& event);
//! @brief Inform the system that a new event has occurred and it should run the given method on every entities.
void dispatchEvent(const std::function<void (WAL::Wal &)>& event);
//! @inherit
void onUpdate(WAL::ViewEntity<> &entity, std::chrono::nanoseconds dtime) override;
//! @inherit