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); } }