aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/graphics/animations/animator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/graphics/animations/animator.cpp')
-rw-r--r--src/libjin/graphics/animations/animator.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/libjin/graphics/animations/animator.cpp b/src/libjin/graphics/animations/animator.cpp
new file mode 100644
index 0000000..8db3bc2
--- /dev/null
+++ b/src/libjin/graphics/animations/animator.cpp
@@ -0,0 +1,128 @@
+#include "../../math/vector2.hpp"
+#include "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;
+ float interval = 1 / mSpeed;
+ mTick += dt;
+ uint fc = mAnimation->getFrameCount();
+ if (mTick >= interval)
+ {
+ mIndex += int(mTick / interval);
+ mTick = fmod(mTick, interval);
+ 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) const
+ {
+ if (!mAnimation)
+ return;
+ const Sprite* spr = mAnimation->getFrame(mIndex);
+ if (spr)
+ spr->render(x, y, sx, sy, r);
+ }
+
+ void Animator::forceToFrame(uint index)
+ {
+ mIndex = index;
+
+ }
+
+ void Animator::setSpeed(float speed)
+ {
+ mSpeed = speed;
+ }
+
+ void Animator::setDefaultSpeed()
+ {
+ if(mAnimation != nullptr)
+ mSpeed = mAnimation->getSpeed();
+ else
+ {
+ jin_log_error("Animation is null.");
+ return;
+ }
+ }
+
+ void Animator::setLoop(bool loop)
+ {
+ mLoop = loop;
+ }
+
+ void Animator::setDefaultLoop()
+ {
+ if(mAnimation != nullptr)
+ mLoop = mAnimation->isLoop();
+ else
+ {
+ jin_log_error("Animation is null.");
+ return;
+ }
+ }
+
+ float Animator::getSpeed()
+ {
+ return mSpeed;
+ }
+
+ uint Animator::getFrameCount()
+ {
+ return mAnimation->getFrameCount();
+ }
+
+ }
+ }
+} \ No newline at end of file