diff options
author | chai <chaifix@163.com> | 2018-11-20 21:43:01 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-11-20 21:43:01 +0800 |
commit | 9474767c68c48eff2618df80433b0b81ebf529c6 (patch) | |
tree | ef33099fb7aa63f5c80f52319f6bd8a158ea31c1 /src/libjin/graphics/animations/je_animation.cpp | |
parent | 5bd4477d4cf413e90a6b9ef5c8991798a687a0ec (diff) |
*动画
Diffstat (limited to 'src/libjin/graphics/animations/je_animation.cpp')
-rw-r--r-- | src/libjin/graphics/animations/je_animation.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/libjin/graphics/animations/je_animation.cpp b/src/libjin/graphics/animations/je_animation.cpp index 4fe673a..028ef56 100644 --- a/src/libjin/graphics/animations/je_animation.cpp +++ b/src/libjin/graphics/animations/je_animation.cpp @@ -1,5 +1,8 @@ +#include "../../math/je_vector2.hpp" #include "je_animation.h" +using namespace JinEngine::Math; + namespace JinEngine { namespace Graphics @@ -7,7 +10,99 @@ namespace JinEngine namespace Animations { + Animation::Animation() + : mIndex(0) + , mActive(true) + , mLoop(true) + , mTick(0) + { + } + + void Animation::addFrame(const Sprite* frame) + { + if(frame != nullptr) + mFrames.push_back(frame); + } + + void Animation::addFrames(const std::vector<Sprite*>& frames) + { + mFrames.insert(mFrames.end(), frames.begin(), frames.end()); + } + + void Animation::update(float dt) + { + if (!mActive) + return; + mTick += dt; + if (mTick >= mSpeed) + { + mTick -= mSpeed; + next(); + } + } + + void Animation::next() + { + int count = mFrames.size(); + ++mIndex; + if (mLoop) + mIndex %= count; + mIndex = clamp<uint>(mIndex, 0, count - 1); + } + + void Animation::pause() + { + mActive = false; + } + + void Animation::resume() + { + mActive = true; + } + + void Animation::rewind() + { + mIndex = 0; + mTick = 0; + } + + void Animation::setSpeed(float speed) + { + mSpeed = speed; + } + + void Animation::setLoop(bool isLoop) + { + mLoop = isLoop; + } + + uint Animation::getCurrentFrameIndex() + { + return mIndex; + } + + const Sprite* Animation::getCurrentFrame() + { + if (mIndex >= mFrames.size()) + return nullptr; + return mFrames[mIndex]; + } + + void Animation::setCurrentFrame(uint frame) + { + mIndex = frame; + } + void Animation::render(float x, float y, float sx, float sy, float r) + { + if (mFrames.size() == 0) + return; + if (without<uint>(mIndex, 0, mFrames.size() - 1)) + return; + const Sprite* spr = getCurrentFrame(); + if(spr) + spr->render(x, y, sx, sy, r); + } } } |