diff options
Diffstat (limited to 'src/libjin/graphics/animations/je_animator.cpp')
-rw-r--r-- | src/libjin/graphics/animations/je_animator.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/libjin/graphics/animations/je_animator.cpp b/src/libjin/graphics/animations/je_animator.cpp new file mode 100644 index 0000000..4528e8c --- /dev/null +++ b/src/libjin/graphics/animations/je_animator.cpp @@ -0,0 +1,85 @@ +#include "../../math/je_vector2.hpp" +#include "je_animator.h" + +using namespace JinEngine::Math; + +namespace JinEngine +{ + namespace Graphics + { + namespace Animations + { + + Animator::Animator() + : mIndex(0) + , mTick(0) + , mIsActive(true) + { + } + + void Animator::setAnimation(const Animation* anim) + { + mAnimation = anim; + if (mAnimation) + { + mSpeed = mAnimation->getSpeed(); + mLoop = mAnimation->isLoop(); + } + } + + void Animator::play() + { + mIndex = 0; + mIsActive = true; + mTick = 0; + } + + void Animator::pause() + { + mIsActive = false; + } + + void Animator::resume() + { + mIsActive = true; + } + + void Animator::update(float dt) + { + if (!mIsActive || !mAnimation) + return; + mTick += dt; + uint fc = mAnimation->getFrameCount(); + while (mTick >= mSpeed) + { + mTick -= mSpeed; + ++mIndex; + if (mLoop) + mIndex %= fc; + mIndex = clamp<uint>(mIndex, 0, fc - 1); + } + } + + void Animator::rewind() + { + mIndex = 0; + } + + void Animator::render(float x, float y, float sx, float sy, float r) + { + if (!mIsActive || !mAnimation) + return; + const Sprite* spr = mAnimation->getFrame(mIndex); + if (spr) + spr->render(x, y, sx, sy, r); + } + + void Animator::forceToFrame(uint index) + { + mIndex = index; + + } + + } + } +}
\ No newline at end of file |