#include "animation.h" #include namespace Graphics{ // Basic animation, loops through the pictures Animation::Animation(){} Animation::Animation(const sf::Texture& texture, int count, int switchTime, int height = 0, int width = 0, int row = 0) : i_ (0), count_ (count), row_ (row), lastTime_ (0), switchTime_ (switchTime){ sf::Vector2u size = texture_.getSize(); if ( height == 0 ){ h_ = size.y; } else { h_ = height; } if ( width == 0 ){ if (size.x % count != 0){ printf("Animation: size not divisible by %d\n", count); } w_ = size.x / count_; } else { w_ = width; } rect_ = sf::IntRect(0,row_ * h_,w_,h_); // set to first picture in // animation } bool Animation::ticked(int deltaTime){ lastTime_ += deltaTime; if ( switchTime_ > lastTime_ ){ return false; } lastTime_ = 0; return true; } sf::IntRect& Animation::next(int deltaTime){ if ( !ticked(deltaTime) ){ return rect_; } if (i_ < count_ -1 ){ i_ += 1; } else { i_ = 0; } rect_.left = i_ * w_; return rect_; } void Animation::reset(){ i_ = 0; rect_.left = i_ * w_; rect_.top = row_ * h_; } bool Animation::finished(){ return false; } Animation::~Animation(){} // ONESHOT ANIMATION OneShotAnimation::OneShotAnimation(){} OneShotAnimation::OneShotAnimation(const sf::Texture& texture, int count, int switchTime, int height = 0, int width = 0, int row = 0): Animation(texture, count, switchTime, height, width, row) { } bool OneShotAnimation::finished(){ return (i_ == count_ - 1); } sf::IntRect& OneShotAnimation::next(int deltaTime){ if ( !ticked(deltaTime) ){ return rect_; } if ( finished() ){ return rect_; } if (i_ < count_ -1 ){ i_ += 1; } rect_.left = i_ * w_; return rect_; } // BOUNCING ANIMATION BouncingAnimation::BouncingAnimation(){} BouncingAnimation::BouncingAnimation(const sf::Texture& texture, int count, int switchTime, int height = 0, int width = 0, int row = 0): Animation(texture, count, switchTime, height, width, row), up_ (true) { } sf::IntRect& BouncingAnimation::next(int deltaTime){ if( !ticked(deltaTime) ) { return rect_; } // Rewrite this and eliminate the boolean (use the count as // positive/negative) if ( up_ ) { if( i_ == count_ - 1){ up_ = false; } else { i_++; } } else { i_--; if( i_ == 0 ){ up_ = true; } } rect_.left = i_ * w_; return rect_; } BouncingAnimation::~BouncingAnimation(){} }