From 9474767c68c48eff2618df80433b0b81ebf529c6 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 20 Nov 2018 21:43:01 +0800 Subject: =?UTF-8?q?*=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/graphics/animations/je_animation.cpp | 95 +++++++++++++++++++++++++ src/libjin/graphics/animations/je_animation.h | 41 +++++++++-- 2 files changed, 131 insertions(+), 5 deletions(-) (limited to 'src/libjin/graphics/animations') 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& 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(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(mIndex, 0, mFrames.size() - 1)) + return; + const Sprite* spr = getCurrentFrame(); + if(spr) + spr->render(x, y, sx, sy, r); + } } } diff --git a/src/libjin/graphics/animations/je_animation.h b/src/libjin/graphics/animations/je_animation.h index a13a83b..4c2fd64 100644 --- a/src/libjin/graphics/animations/je_animation.h +++ b/src/libjin/graphics/animations/je_animation.h @@ -19,15 +19,22 @@ namespace JinEngine class Animation { public: + Animation(); + /// /// /// - void update(float dt); + void addFrame(const Sprite* frame); + + /// + /// + /// + void addFrames(const std::vector& frames); /// /// /// - void start(); + void update(float dt); /// /// @@ -37,7 +44,7 @@ namespace JinEngine /// /// /// - void stop(); + void resume(); /// /// Force rewind. @@ -49,6 +56,11 @@ namespace JinEngine /// void setSpeed(float speed); + /// + /// + /// + void setLoop(bool isLoop); + /// /// Get current frame index. /// @@ -57,7 +69,7 @@ namespace JinEngine /// /// /// - Sprite* getCurrentFrame(); + const Sprite* getCurrentFrame(); /// /// Set current frame index. @@ -71,17 +83,36 @@ namespace JinEngine /// void render(float x, float y, float sx, float sy, float r); + /// + /// + /// + Animation clone(); + private: + + void next(); + /// /// Key frames. /// - std::vector mFrames; + std::vector mFrames; /// /// Animation playing speed. /// float mSpeed; + /// + /// + /// + float mTick; + + uint mIndex; + + float mLoop; + + bool mActive; + }; } // namespace Animations -- cgit v1.1-26-g67d0