diff options
author | chai <chaifix@163.com> | 2019-01-12 21:48:33 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-01-12 21:48:33 +0800 |
commit | 8b00d67febf133e89f6a0bfabc41feed555dc4a9 (patch) | |
tree | fe48ef17c250afa40c2588300fcdb5920dba6951 /src/libjin/graphics/animations/animator.cpp | |
parent | a907c39756ef6b368d06643afa491c49a9044a8e (diff) |
*去掉文件前缀je_
Diffstat (limited to 'src/libjin/graphics/animations/animator.cpp')
-rw-r--r-- | src/libjin/graphics/animations/animator.cpp | 128 |
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 |