From daf5919523e711f7db5ff76f4411e65493af29e3 Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Thu, 3 Jun 2021 11:38:15 +0200 Subject: [PATCH 1/8] start animator --- CMakeLists.txt | 6 ++- .../Component/Animator/AnimatorComponent.cpp | 16 +++++++ .../Component/Animator/AnimatorComponent.hpp | 28 +++++++++++ sources/Runner/Runner.cpp | 6 ++- sources/System/Animator/AnimatorSystem.cpp | 46 +++++++++++++++++++ sources/System/Animator/AnimatorSystem.hpp | 27 +++++++++++ 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 sources/Component/Animator/AnimatorComponent.cpp create mode 100644 sources/Component/Animator/AnimatorComponent.hpp create mode 100644 sources/System/Animator/AnimatorSystem.cpp create mode 100644 sources/System/Animator/AnimatorSystem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fd2543bf..f7499f20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,12 +64,16 @@ set(SOURCES sources/Component/Collision/CollisionComponent.hpp sources/System/Collision/CollisionSystem.hpp sources/System/Collision/CollisionSystem.cpp + sources/Component/Animator/AnimatorComponent.cpp + sources/Component/Animator/AnimatorComponent.hpp + sources/System/Animator/AnimatorSystem.cpp + sources/System/Animator/AnimatorSystem.hpp ) add_executable(bomberman sources/main.cpp ${SOURCES} - ) + ) target_include_directories(bomberman PUBLIC sources) target_link_libraries(bomberman PUBLIC wal ray) diff --git a/sources/Component/Animator/AnimatorComponent.cpp b/sources/Component/Animator/AnimatorComponent.cpp new file mode 100644 index 00000000..fa5d8cc2 --- /dev/null +++ b/sources/Component/Animator/AnimatorComponent.cpp @@ -0,0 +1,16 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#include "AnimatorComponent.hpp" + +namespace BBM { + AnimatorComponent::AnimatorComponent(WAL::Entity &entity) + : WAL::Component(entity) + {} + + WAL::Component *AnimatorComponent::clone(WAL::Entity &entity) const + { + return new AnimatorComponent(entity); + } +} \ No newline at end of file diff --git a/sources/Component/Animator/AnimatorComponent.hpp b/sources/Component/Animator/AnimatorComponent.hpp new file mode 100644 index 00000000..0c31b576 --- /dev/null +++ b/sources/Component/Animator/AnimatorComponent.hpp @@ -0,0 +1,28 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#pragma once + +#include + +namespace BBM { + class AnimatorComponent : public WAL::Component + { + public: + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief An Animator component can't be instantiated, it should be derived. + explicit AnimatorComponent(WAL::Entity &entity); + + //! @brief An Animator component can't be instantiated, it should be derived. + AnimatorComponent(const AnimatorComponent &) = default; + + //! @brief default destructor + ~AnimatorComponent() override = default; + + //! @brief An Animator component can't be assigned + AnimatorComponent &operator=(const AnimatorComponent &) = delete; + }; +} \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 8b1f36fd..9ce162c1 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -26,6 +26,8 @@ #include "Runner.hpp" #include "Models/GameState.hpp" #include +#include +#include #include "Component/Animation/AnimationsComponent.hpp" #include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" @@ -51,6 +53,7 @@ namespace BBM .addSystem() .addSystem() .addSystem(wal) + .addSystem() .addSystem(); } @@ -75,8 +78,9 @@ namespace BBM .addComponent() .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() + .addComponent() .addComponent() - .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) + .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(2) .addComponent(); scene->addEntity("cube") diff --git a/sources/System/Animator/AnimatorSystem.cpp b/sources/System/Animator/AnimatorSystem.cpp new file mode 100644 index 00000000..aa13683a --- /dev/null +++ b/sources/System/Animator/AnimatorSystem.cpp @@ -0,0 +1,46 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#include +#include +#include +#include +#include +#include "AnimatorSystem.hpp" +#include "Component/Renderer/Drawable3DComponent.hpp" + +using Keyboard = RAY::Controller::Keyboard; +namespace RAY3D = RAY::Drawables::Drawables3D; + +namespace BBM +{ + AnimatorSystem::AnimatorSystem() + : WAL::System({ + typeid(AnimatorComponent), + typeid(KeyboardComponent) + }) + {} + + void AnimatorSystem::onFixedUpdate(WAL::Entity &entity) + { + if (!entity.hasComponent()) + return; + auto &model = entity.getComponent>(); + auto &animation = entity.getComponent(); + const auto &keyboard = entity.getComponent(); + animation.setAnimIndex(1); + if (Keyboard::isDown(keyboard.keyRight)) { + model.member.setRotationAngle(180.0f); + } + if (Keyboard::isDown(keyboard.keyLeft)) { + model.member.setRotationAngle(0.0f); + } + if (Keyboard::isDown(keyboard.keyUp)) { + model.member.setRotationAngle(90.0f); + } + if (Keyboard::isDown(keyboard.keyDown)) { + model.member.setRotationAngle(270.0f); + } + } +} \ No newline at end of file diff --git a/sources/System/Animator/AnimatorSystem.hpp b/sources/System/Animator/AnimatorSystem.hpp new file mode 100644 index 00000000..6f4d950c --- /dev/null +++ b/sources/System/Animator/AnimatorSystem.hpp @@ -0,0 +1,27 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#pragma once + +#include "System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle Health entities. + class AnimatorSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + AnimatorSystem(); + //! @brief A Health system is copy constructable + AnimatorSystem(const AnimatorSystem &) = default; + //! @brief A default destructor + ~AnimatorSystem() override = default; + //! @brief A Health system is assignable. + AnimatorSystem &operator=(const AnimatorSystem &) = default; + }; +} \ No newline at end of file From 45ee02e06ba490460c9b4b55d2a0f4545f4285ef Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Thu, 3 Jun 2021 12:29:46 +0200 Subject: [PATCH 2/8] animator has been made for right/left/up/down --- sources/Runner/Runner.cpp | 4 ++-- sources/System/Animator/AnimatorSystem.cpp | 26 +++++++++++++++------- sources/System/Health/HealthSystem.cpp | 11 +++++++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 9ce162c1..c6f90238 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -53,7 +53,6 @@ namespace BBM .addSystem() .addSystem() .addSystem(wal) - .addSystem() .addSystem(); } @@ -65,6 +64,7 @@ namespace BBM wal.addSystem>(); wal.addSystem>(); wal.addSystem(); + wal.addSystem(); wal.addSystem(window) .addSystem>(); @@ -80,7 +80,7 @@ namespace BBM .addComponent() .addComponent() .addComponent() - .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) + .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) .addComponent(2) .addComponent(); scene->addEntity("cube") diff --git a/sources/System/Animator/AnimatorSystem.cpp b/sources/System/Animator/AnimatorSystem.cpp index aa13683a..ef1f8d56 100644 --- a/sources/System/Animator/AnimatorSystem.cpp +++ b/sources/System/Animator/AnimatorSystem.cpp @@ -7,40 +7,50 @@ #include #include #include +#include #include "AnimatorSystem.hpp" #include "Component/Renderer/Drawable3DComponent.hpp" using Keyboard = RAY::Controller::Keyboard; namespace RAY3D = RAY::Drawables::Drawables3D; +using Key = RAY::Controller::Keyboard::Key; namespace BBM { AnimatorSystem::AnimatorSystem() : WAL::System({ typeid(AnimatorComponent), - typeid(KeyboardComponent) + typeid(ControllableComponent) }) {} void AnimatorSystem::onFixedUpdate(WAL::Entity &entity) { - if (!entity.hasComponent()) + if (!entity.hasComponent()) return; + const auto &controllable = entity.getComponent(); auto &model = entity.getComponent>(); auto &animation = entity.getComponent(); - const auto &keyboard = entity.getComponent(); - animation.setAnimIndex(1); - if (Keyboard::isDown(keyboard.keyRight)) { + if (controllable.move.x == 1) { model.member.setRotationAngle(180.0f); + animation.setAnimIndex(0); + return; } - if (Keyboard::isDown(keyboard.keyLeft)) { + if (controllable.move.x == -1) { model.member.setRotationAngle(0.0f); + animation.setAnimIndex(0); + return; } - if (Keyboard::isDown(keyboard.keyUp)) { + if (controllable.move.y == 1) { model.member.setRotationAngle(90.0f); + animation.setAnimIndex(0); + return; } - if (Keyboard::isDown(keyboard.keyDown)) { + if (controllable.move.y == -1) { model.member.setRotationAngle(270.0f); + animation.setAnimIndex(0); + return; } + animation.setAnimIndex(1); } } \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 90bbfbb9..57b5bf27 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -3,6 +3,7 @@ // Edited by Benjamin Henry on 2021-05-20. // +#include #include "HealthSystem.hpp" #include "Component/Health/HealthComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" @@ -12,7 +13,8 @@ namespace BBM { HealthSystem::HealthSystem() : WAL::System({ - typeid(HealthComponent) + typeid(HealthComponent), + typeid(AnimationsComponent) }) {} @@ -20,7 +22,12 @@ namespace BBM { auto &health = entity.getComponent(); - if (health.getHealthPoint() == 0) + if (health.getHealthPoint() == 0) { + if (entity.hasComponent()) { + auto &animation = entity.getComponent(); + animation.setAnimIndex(5); + } health.onDeath(entity); + } } } \ No newline at end of file From 378ba98050baa5dcd22107b1400cf14393c9f5df Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Thu, 3 Jun 2021 14:58:03 +0200 Subject: [PATCH 3/8] added death animation through a callback onDeath --- sources/Component/Health/HealthComponent.cpp | 6 +++++ sources/Component/Health/HealthComponent.hpp | 3 +++ sources/Runner/Runner.cpp | 6 ++++- sources/System/Animator/AnimatorSystem.cpp | 27 ++++++-------------- sources/System/Health/HealthSystem.cpp | 7 +---- 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/sources/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp index fe81fabb..20e2a3b6 100644 --- a/sources/Component/Health/HealthComponent.cpp +++ b/sources/Component/Health/HealthComponent.cpp @@ -18,6 +18,12 @@ namespace BBM _healthPoint(healthPoint) {} + HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint, std::function callback) + : WAL::Component(entity), + _healthPoint(healthPoint), + onDeath(callback) + {} + WAL::Component *HealthComponent::clone(WAL::Entity &entity) const { return new HealthComponent(entity); diff --git a/sources/Component/Health/HealthComponent.hpp b/sources/Component/Health/HealthComponent.hpp index 2eadafc5..2ece2f0b 100644 --- a/sources/Component/Health/HealthComponent.hpp +++ b/sources/Component/Health/HealthComponent.hpp @@ -41,6 +41,9 @@ namespace BBM //! @brief Constructor HealthComponent(WAL::Entity &entity, unsigned int healthPoint); + //! @brief Constructor + HealthComponent(WAL::Entity &entity, unsigned int healthPoint, std::function callback); + //! @brief A Health component can't be instantiated, it should be derived. HealthComponent(const HealthComponent &) = default; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index c6f90238..8ab5827a 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -82,7 +82,11 @@ namespace BBM .addComponent() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) .addComponent(2) - .addComponent(); + .addComponent() + .addComponent(1, [](WAL::Entity &entity) { + auto &animation = entity.getComponent(); + animation.setAnimIndex(5); + }); scene->addEntity("cube") .addComponent(-5, 0, -5) .addComponent>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) diff --git a/sources/System/Animator/AnimatorSystem.cpp b/sources/System/Animator/AnimatorSystem.cpp index ef1f8d56..874dba08 100644 --- a/sources/System/Animator/AnimatorSystem.cpp +++ b/sources/System/Animator/AnimatorSystem.cpp @@ -28,28 +28,17 @@ namespace BBM { if (!entity.hasComponent()) return; + const std::vector> moveDiag = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}}; + const std::vector rotationAngle = {0.0f, 45.0f, 90.0f, 135.0f, 180.0f, 225.0f, 270.0f, 315.0f}; const auto &controllable = entity.getComponent(); auto &model = entity.getComponent>(); auto &animation = entity.getComponent(); - if (controllable.move.x == 1) { - model.member.setRotationAngle(180.0f); - animation.setAnimIndex(0); - return; - } - if (controllable.move.x == -1) { - model.member.setRotationAngle(0.0f); - animation.setAnimIndex(0); - return; - } - if (controllable.move.y == 1) { - model.member.setRotationAngle(90.0f); - animation.setAnimIndex(0); - return; - } - if (controllable.move.y == -1) { - model.member.setRotationAngle(270.0f); - animation.setAnimIndex(0); - return; + for (int i = 0; i != moveDiag.size(); i++) { + if (controllable.move.x == moveDiag[i][0] && controllable.move.y == moveDiag[i][1]) { + model.member.setRotationAngle(rotationAngle[i]); + animation.setAnimIndex(0); + return; + } } animation.setAnimIndex(1); } diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 57b5bf27..25cabc8e 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -13,8 +13,7 @@ namespace BBM { HealthSystem::HealthSystem() : WAL::System({ - typeid(HealthComponent), - typeid(AnimationsComponent) + typeid(HealthComponent) }) {} @@ -23,10 +22,6 @@ namespace BBM auto &health = entity.getComponent(); if (health.getHealthPoint() == 0) { - if (entity.hasComponent()) { - auto &animation = entity.getComponent(); - animation.setAnimIndex(5); - } health.onDeath(entity); } } From 9e6b3b9e409a8108ee2db2f631b6fc2e601c0706 Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Mon, 7 Jun 2021 12:05:37 +0200 Subject: [PATCH 4/8] fix doc --- sources/System/Animator/AnimatorSystem.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/System/Animator/AnimatorSystem.hpp b/sources/System/Animator/AnimatorSystem.hpp index 6f4d950c..3aae4d6c 100644 --- a/sources/System/Animator/AnimatorSystem.hpp +++ b/sources/System/Animator/AnimatorSystem.hpp @@ -8,7 +8,7 @@ namespace BBM { - //! @brief A system to handle Health entities. + //! @brief A system to handle Animator entities. class AnimatorSystem : public WAL::System { public: @@ -17,11 +17,11 @@ namespace BBM //! @brief A default constructor AnimatorSystem(); - //! @brief A Health system is copy constructable + //! @brief An Animator system is copy constructable AnimatorSystem(const AnimatorSystem &) = default; //! @brief A default destructor ~AnimatorSystem() override = default; - //! @brief A Health system is assignable. + //! @brief An Animator system is assignable. AnimatorSystem &operator=(const AnimatorSystem &) = default; }; } \ No newline at end of file From e59cc52f6ee3f5cfb93c8f138f2f778a9148fdd8 Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Mon, 7 Jun 2021 16:41:47 +0200 Subject: [PATCH 5/8] removed 1 com --- sources/Models/Vector2.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp index 37728478..b8f5b9cc 100644 --- a/sources/Models/Vector2.hpp +++ b/sources/Models/Vector2.hpp @@ -125,7 +125,6 @@ namespace BBM float dot = this->x * o.x + this->y * o.y; float det = this->x * o.y - this->y * o.x; return (std::atan2(det, dot) * (180.0f / M_PI)); - //return (std::atan(std::abs(o.y - this->y) / std::abs(o.x - this->x))); } double magnitude() const From 1081574ba732f2465cf538cf64a2adab58f57c64 Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Mon, 7 Jun 2021 16:42:58 +0200 Subject: [PATCH 6/8] old collisioncomponent (runner.cpp) --- sources/Runner/Runner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 5077b06f..ab953439 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -70,7 +70,7 @@ namespace BBM .addComponent() .addComponent() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) - .addComponent(2) + .addComponent(1) .addComponent() .addComponent(1, [](WAL::Entity &entity) { auto &animation = entity.getComponent(); From 5fc43dc15ac6c678c1bf5e6b262adaee2edbdf33 Mon Sep 17 00:00:00 2001 From: HENRY Benjamin Date: Mon, 7 Jun 2021 16:50:50 +0200 Subject: [PATCH 7/8] add pi_number define (fix windows) --- sources/Models/Vector2.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp index b8f5b9cc..b3dacf2b 100644 --- a/sources/Models/Vector2.hpp +++ b/sources/Models/Vector2.hpp @@ -9,6 +9,8 @@ #include #include "Vector/Vector2.hpp" +#define PI_NUMBER 3.14159265359 + namespace BBM { //! @brief A Vector2 data type. (templated to allow any kind of vector2) @@ -124,7 +126,7 @@ namespace BBM { float dot = this->x * o.x + this->y * o.y; float det = this->x * o.y - this->y * o.x; - return (std::atan2(det, dot) * (180.0f / M_PI)); + return (std::atan2(det, dot) * (180.0f / PI_NUMBER)); } double magnitude() const From c16f264920678139b1c56d24f627019ead7c260a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Mon, 7 Jun 2021 19:21:52 +0200 Subject: [PATCH 8/8] some non breaking changes fix (std::move + One ctor with opt params instead of multiple ctors) --- sources/Component/Health/HealthComponent.cpp | 14 +++----------- sources/Component/Health/HealthComponent.hpp | 8 +------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/sources/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp index 20e2a3b6..5499d243 100644 --- a/sources/Component/Health/HealthComponent.cpp +++ b/sources/Component/Health/HealthComponent.cpp @@ -6,22 +6,14 @@ #include "HealthComponent.hpp" +#include + namespace BBM { - HealthComponent::HealthComponent(WAL::Entity &entity) - : WAL::Component(entity), - _healthPoint() - {} - - HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint) - : WAL::Component(entity), - _healthPoint(healthPoint) - {} - HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint, std::function callback) : WAL::Component(entity), _healthPoint(healthPoint), - onDeath(callback) + onDeath(std::move(callback)) {} WAL::Component *HealthComponent::clone(WAL::Entity &entity) const diff --git a/sources/Component/Health/HealthComponent.hpp b/sources/Component/Health/HealthComponent.hpp index 2ece2f0b..d8ff7531 100644 --- a/sources/Component/Health/HealthComponent.hpp +++ b/sources/Component/Health/HealthComponent.hpp @@ -34,15 +34,9 @@ namespace BBM //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; - - //! @brief A Health component can't be instantiated, it should be derived. - explicit HealthComponent(WAL::Entity &entity); //! @brief Constructor - HealthComponent(WAL::Entity &entity, unsigned int healthPoint); - - //! @brief Constructor - HealthComponent(WAL::Entity &entity, unsigned int healthPoint, std::function callback); + explicit HealthComponent(WAL::Entity &entity, unsigned int healthPoint = 1, std::function callback = {}); //! @brief A Health component can't be instantiated, it should be derived. HealthComponent(const HealthComponent &) = default;