diff options
Diffstat (limited to 'src/animatedsprite.cpp')
-rw-r--r-- | src/animatedsprite.cpp | 56 |
1 files changed, 45 insertions, 11 deletions
diff --git a/src/animatedsprite.cpp b/src/animatedsprite.cpp index 59bf2f88..ec666d80 100644 --- a/src/animatedsprite.cpp +++ b/src/animatedsprite.cpp @@ -41,16 +41,17 @@ AnimatedSprite::AnimatedSprite(SpriteDef *sprite): mSprite(sprite), mAction(0), mAnimation(0), - mFrame(0), - mAlpha(1.0f) + mFrame(0) { assert(mSprite); + mAlpha = 1.0f; + // Take possession of the sprite mSprite->incRef(); // Play the stand animation by default - play(ACTION_STAND); + play(SpriteAction::STAND); } AnimatedSprite *AnimatedSprite::load(const std::string &filename, int variant) @@ -69,18 +70,22 @@ AnimatedSprite::~AnimatedSprite() mSprite->decRef(); } -void AnimatedSprite::reset() +bool AnimatedSprite::reset() { + bool ret = mFrameIndex !=0 || mFrameTime != 0 || mLastTime != 0; + mFrameIndex = 0; mFrameTime = 0; mLastTime = 0; + + return ret; } -void AnimatedSprite::play(SpriteAction spriteAction) +bool AnimatedSprite::play(std::string spriteAction) { Action *action = mSprite->getAction(spriteAction); if (!action) - return; + return false; mAction = action; Animation *animation = mAction->getAnimation(mDirection); @@ -91,10 +96,14 @@ void AnimatedSprite::play(SpriteAction spriteAction) mFrame = mAnimation->getFrame(0); reset(); + + return true; } + + return false; } -void AnimatedSprite::update(int time) +bool AnimatedSprite::update(int time) { // Avoid freaking out at first frame or when tick_time overflows if (time < mLastTime || mLastTime == 0) @@ -102,16 +111,22 @@ void AnimatedSprite::update(int time) // If not enough time has passed yet, do nothing if (time <= mLastTime || !mAnimation) - return; + return false; unsigned int dt = time - mLastTime; mLastTime = time; + Animation *animation = mAnimation; + Frame *frame = mFrame; + if (!updateCurrentAnimation(dt)) { // Animation finished, reset to default - play(ACTION_STAND); + play(SpriteAction::STAND); } + + // Make sure something actually changed + return animation != mAnimation || frame != mFrame; } bool AnimatedSprite::updateCurrentAnimation(unsigned int time) @@ -158,14 +173,14 @@ bool AnimatedSprite::draw(Graphics *graphics, int posX, int posY) const posY + mFrame->offsetY); } -void AnimatedSprite::setDirection(SpriteDirection direction) +bool AnimatedSprite::setDirection(SpriteDirection direction) { if (mDirection != direction) { mDirection = direction; if (!mAction) - return; + return false; Animation *animation = mAction->getAnimation(mDirection); @@ -175,7 +190,21 @@ void AnimatedSprite::setDirection(SpriteDirection direction) mFrame = mAnimation->getFrame(0); reset(); } + + return true; } + + return false; +} + +size_t AnimatedSprite::getCurrentFrame() const +{ + return mFrameIndex; +} + +size_t AnimatedSprite::getFrameCount() const +{ + return mAnimation->getLength(); } int AnimatedSprite::getWidth() const @@ -193,3 +222,8 @@ int AnimatedSprite::getHeight() const else return 0; } + +const Image* AnimatedSprite::getImage() const +{ + return mFrame ? mFrame->image : 0; +} |