From 61e04f760a8a5e12827934fc511d1c86bab3e1ab Mon Sep 17 00:00:00 2001 From: TrueBabyChaise Date: Fri, 21 May 2021 14:15:42 +0200 Subject: [PATCH] Add healthSystem The component doesn't disable the entity anymore, the system does it by checking the component of the entity Co-Authored-By: Benjamin HENRY <44569175+EternalRat@users.noreply.github.com> --- sources/Component/Health/HealthComponent.cpp | 3 +- sources/Component/Health/HealthComponent.hpp | 2 ++ sources/System/Health/HealthSystem.cpp | 25 ++++++++++++++++ sources/System/Health/HealthSystem.hpp | 30 ++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 sources/System/Health/HealthSystem.cpp create mode 100644 sources/System/Health/HealthSystem.hpp diff --git a/sources/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp index a129a24e..bd3fff53 100644 --- a/sources/Component/Health/HealthComponent.cpp +++ b/sources/Component/Health/HealthComponent.cpp @@ -32,8 +32,7 @@ namespace BBM { if (damage >= this->_healthPoint) { this->_healthPoint = 0; - this->die(); - } else + } else this->_healthPoint -= damage; } diff --git a/sources/Component/Health/HealthComponent.hpp b/sources/Component/Health/HealthComponent.hpp index ecc67b12..41a8d920 100644 --- a/sources/Component/Health/HealthComponent.hpp +++ b/sources/Component/Health/HealthComponent.hpp @@ -45,5 +45,7 @@ namespace BBM //! @brief A component can't be assigned HealthComponent &operator=(const HealthComponent &) = delete; + + friend class HealthSystem; }; } \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp new file mode 100644 index 00000000..3b03337a --- /dev/null +++ b/sources/System/Health/HealthSystem.cpp @@ -0,0 +1,25 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#include "HealthSystem.hpp" +#include "sources/Component/Health/HealthComponent.hpp" +#include "sources/Component/Controllable/ControllableComponent.hpp" +#include "lib/wal/sources/Entity/Entity.hpp" + +namespace BBM +{ + const std::type_info &HealthSystem::getComponent() const + { + return typeid(HealthComponent); + } + + void HealthSystem::onFixedUpdate(WAL::Entity &entity) + { + auto &health = entity.getComponent(); + + if (health._healthPoint == 0); + health.die(); + } +} \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.hpp b/sources/System/Health/HealthSystem.hpp new file mode 100644 index 00000000..8bf8eec5 --- /dev/null +++ b/sources/System/Health/HealthSystem.hpp @@ -0,0 +1,30 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include "lib/wal/sources/System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle Health entities. + class HealthSystem : public WAL::System + { + public: + //! @inherit + const std::type_info &getComponent() const override; + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + HealthSystem() = default; + //! @brief A Health system is copy constructable + HealthSystem(const HealthSystem &) = default; + //! @brief A default destructor + ~HealthSystem() override = default; + //! @brief A Health system is assignable. + HealthSystem &operator=(const HealthSystem &) = default; + }; +}