diff --git a/lib/Ray/sources/Model/ModelAnimations.cpp b/lib/Ray/sources/Model/ModelAnimations.cpp index 7ab39f41..5d5d297f 100644 --- a/lib/Ray/sources/Model/ModelAnimations.cpp +++ b/lib/Ray/sources/Model/ModelAnimations.cpp @@ -10,7 +10,8 @@ RAY::Cache<::ModelAnimation> RAY::ModelAnimations::_animationsCache(LoadModelAnimations, UnloadModelAnimations); RAY::ModelAnimations::ModelAnimations(const std::string &filePath): - _animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount)) + _animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount)), + _filePath(filePath) { ::ModelAnimation *ptr = this->_animationsPtr.get(); @@ -20,7 +21,7 @@ RAY::ModelAnimations::ModelAnimations(const std::string &filePath): RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index) { - return this->_animations[index]; + return this->_animations.at(index); } size_t RAY::ModelAnimations::getAnimationsCount() const @@ -28,3 +29,13 @@ size_t RAY::ModelAnimations::getAnimationsCount() const return this->_animationCount; } +std::string RAY::ModelAnimations::getFilePath() const +{ + return this->_filePath; +} + +const RAY::ModelAnimation &RAY::ModelAnimations::at(int index) const +{ + return this->_animations.at(index); +} + diff --git a/lib/Ray/sources/Model/ModelAnimations.hpp b/lib/Ray/sources/Model/ModelAnimations.hpp index ba46a0e4..9188955a 100644 --- a/lib/Ray/sources/Model/ModelAnimations.hpp +++ b/lib/Ray/sources/Model/ModelAnimations.hpp @@ -27,15 +27,24 @@ namespace RAY { //! @brief Default constructor ~ModelAnimations() = default; + //! @brief Default move ctor + ModelAnimations(ModelAnimations &&) = default; + //! @brief Only single entity can hold these animations pointers ModelAnimations &operator=(const ModelAnimations &) = delete; //! @brief Castin Object to raw model animation pointer ModelAnimation &operator[](int index); + //! @brief Same usage as the operator[] but const + const ModelAnimation &at(int index) const; + //! @return the number of loaded animations size_t getAnimationsCount() const; + //! @brief Get the creation file + std::string getFilePath() const; + private: //! @brief Holds the pointer returned by the loading function std::shared_ptr<::ModelAnimation> _animationsPtr; @@ -46,6 +55,9 @@ namespace RAY { //! @brief the number of loaded animations int _animationCount; + //! @brief The file where the animations were loaded (used to create a copy of this class) + const std::string _filePath; + static Cache<::ModelAnimation> _animationsCache; }; } diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp index ce29964d..843dcbcc 100644 --- a/sources/Component/Animation/AnimationsComponent.cpp +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -8,9 +8,9 @@ namespace BBM { - AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation, int animIndex) + AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex) : WAL::Component(entity), - _modelAnimation(modelAnimation), + _modelAnimation(std::move(modelAnimation)), _currentAnimIndex(animIndex) { this->_modelAnimation[this->_currentAnimIndex].setFrameCounter(0); @@ -18,15 +18,17 @@ namespace BBM WAL::Component *AnimationsComponent::clone(WAL::Entity &entity) const { - return new AnimationsComponent(entity, this->_modelAnimation, this->_currentAnimIndex); + return new AnimationsComponent(entity, + RAY::ModelAnimations(this->_modelAnimation.getFilePath()), + this->_currentAnimIndex); } size_t AnimationsComponent::getCurrentAnimFrameCounter() const { - return this->_modelAnimation[this->_currentAnimIndex].getFrameCounter(); + return this->_modelAnimation.at(this->_currentAnimIndex).getFrameCounter(); } - RAY::ModelAnimation AnimationsComponent::getCurrentModelAnim() const + RAY::ModelAnimation AnimationsComponent::getCurrentModelAnim() { return this->_modelAnimation[this->_currentAnimIndex]; } diff --git a/sources/Component/Animation/AnimationsComponent.hpp b/sources/Component/Animation/AnimationsComponent.hpp index 239c9470..ffaf0599 100644 --- a/sources/Component/Animation/AnimationsComponent.hpp +++ b/sources/Component/Animation/AnimationsComponent.hpp @@ -15,7 +15,7 @@ namespace BBM { private: //! @brief To get the animation data - RAY::ModelAnimations &_modelAnimation; + RAY::ModelAnimations _modelAnimation; //! @brief The index of the int _currentAnimIndex; public: @@ -32,7 +32,7 @@ namespace BBM size_t getCurrentAnimFrameCounter() const; //! @brief get the current - RAY::ModelAnimation getCurrentModelAnim() const; + RAY::ModelAnimation getCurrentModelAnim(); //! @brief set the anim frame counter void setCurrentAnimFrameCounter(size_t animFrameCounter); @@ -44,7 +44,7 @@ namespace BBM void incCurrentAnimFrameCounter(); //! @brief ctor entity and the path of the animation file - explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations &modelAnimation, int animIndex); + explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex); //! @brief copy ctor AnimationsComponent(const AnimationsComponent &) = default; //! @brief dtor diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index d2a1adc9..6ca30a27 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -22,6 +22,9 @@ #include "Component/Renderer/CameraComponent.hpp" #include "Runner.hpp" #include "Models/GameState.hpp" +#include +#include "Component/Animation/AnimationsComponent.hpp" +#include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" namespace RAY2D = RAY::Drawables::Drawables2D; @@ -53,6 +56,7 @@ namespace BBM RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); wal.addSystem>(); + wal.addSystem(); wal.addSystem(window) .addSystem>(); @@ -73,6 +77,7 @@ namespace BBM .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() + .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(); scene->addEntity("camera") .addComponent(8, 20, 7) diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index f9f7c9ef..66d1253e 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -2,6 +2,7 @@ // Created by cbihan on 01/06/2021. // +#include #include "AnimationsSystem.hpp" #include "Component/Animation/AnimationsComponent.hpp" #include "Model/Model.hpp" @@ -11,12 +12,14 @@ namespace BBM { AnimationsSystem::AnimationsSystem() - : WAL::System({typeid(AnimationsComponent), - typeid(Drawable3DComponent)}) + : WAL::System({ + typeid(Drawable3DComponent), + typeid(AnimationsComponent) + }) { } - void AnimationsSystem::onFixedUpdate(WAL::Entity &entity) + void AnimationsSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) { auto &model = entity.getComponent>(); auto &anim = entity.getComponent(); diff --git a/sources/System/Animation/AnimationsSystem.hpp b/sources/System/Animation/AnimationsSystem.hpp index eb508b3e..fb08b2ef 100644 --- a/sources/System/Animation/AnimationsSystem.hpp +++ b/sources/System/Animation/AnimationsSystem.hpp @@ -10,8 +10,9 @@ namespace BBM { class AnimationsSystem : public WAL::System { + public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override; //! @brief A default constructor AnimationsSystem();