diff --git a/CMakeLists.txt b/CMakeLists.txt index 966a6d01..dfb4cd6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,11 +69,15 @@ 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/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp index fe81fabb..5499d243 100644 --- a/sources/Component/Health/HealthComponent.cpp +++ b/sources/Component/Health/HealthComponent.cpp @@ -6,16 +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(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 2eadafc5..d8ff7531 100644 --- a/sources/Component/Health/HealthComponent.hpp +++ b/sources/Component/Health/HealthComponent.hpp @@ -34,12 +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); + 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; diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp index 629a11b8..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) @@ -120,6 +122,13 @@ namespace BBM return std::sqrt(std::pow(this->x - o.x, 2) + std::pow(this->y - o.y, 2)); } + double angle(const Vector2 &o) const + { + 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 / PI_NUMBER)); + } + double magnitude() const { return std::sqrt(std::pow(this->x, 2) + std::pow(this->y, 2)); diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 011ffee3..ab953439 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -22,6 +22,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" @@ -54,6 +56,7 @@ namespace BBM RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); wal.addSystem() + .addSystem() .addSystem(window); } @@ -64,11 +67,15 @@ 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"), 3) .addComponent(1) - .addComponent(); - + .addComponent() + .addComponent(1, [](WAL::Entity &entity) { + auto &animation = entity.getComponent(); + animation.setAnimIndex(5); + }); scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); diff --git a/sources/System/Animator/AnimatorSystem.cpp b/sources/System/Animator/AnimatorSystem.cpp new file mode 100644 index 00000000..f03aeb9b --- /dev/null +++ b/sources/System/Animator/AnimatorSystem.cpp @@ -0,0 +1,37 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#include +#include +#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::Wal &wal) + : System(wal) + {} + + void AnimatorSystem::onFixedUpdate(WAL::ViewEntity &entity) + { + const auto &controllable = entity.get(); + auto drawable = entity.get().drawable.get(); + auto &animation = entity.get(); + auto anim = dynamic_cast(drawable); + if (anim && controllable.move != Vector2f(0, 0)) { + anim->setRotationAngle(controllable.move.angle(Vector2f(-1, 0))); + animation.setAnimIndex(0); + return; + } + animation.setAnimIndex(1); + } +} \ 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..fcddfc13 --- /dev/null +++ b/sources/System/Animator/AnimatorSystem.hpp @@ -0,0 +1,29 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#pragma once + +#include +#include "Component/Animation/AnimationsComponent.hpp" +#include "System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle Animator entities. + class AnimatorSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::ViewEntity &entity) override; + + //! @brief A default constructor + AnimatorSystem(WAL::Wal &wal); + //! @brief An Animator system is copy constructable + AnimatorSystem(const AnimatorSystem &) = default; + //! @brief A default destructor + ~AnimatorSystem() override = default; + //! @brief An Animator system is assignable. + AnimatorSystem &operator=(const AnimatorSystem &) = default; + }; +} \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 29e0db1f..4419e9e1 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" @@ -18,7 +19,8 @@ namespace BBM { auto &health = entity.get(); - if (health.getHealthPoint() == 0) + if (health.getHealthPoint() == 0) { health.onDeath(entity); + } } } \ No newline at end of file