Add option to define beginFrame

Used function overloading for backwards compatibility
Also add a getter to check if the sprite is animating (because protected)
This commit is contained in:
Jorim Tielemans 2019-07-31 17:44:56 +02:00
parent c2039cf254
commit 66892f846a
2 changed files with 15 additions and 4 deletions

View File

@ -58,7 +58,7 @@ protected:
bool stayWithinBounds;
u32 imageSize, tileIndex;
SpriteSize spriteSize;
u32 animationDelay, amountOfFrames, currentFrame, animationCounter;
u32 animationDelay, amountOfFrames, beginFrame, currentFrame, animationCounter;
bool animating;
std::unique_ptr<OBJ_ATTR> oam;
@ -78,6 +78,13 @@ public:
this->animationDelay = animationDelay;
animate();
}
void makeAnimated(int beginFrame, int amountOfFrames, int animationDelay) {
this->amountOfFrames = amountOfFrames;
this->animationDelay = animationDelay;
this->beginFrame = beginFrame;
this->currentFrame = beginFrame;
animate();
}
void animate() { this->animating = true; }
void animateToFrame(int frame) { this->currentFrame = frame; }
void stopAnimating() { this->animating = false; }
@ -107,6 +114,7 @@ public:
u32 getWidth() { return w; }
u32 getY() { return y; }
u32 getCurrentFrame() { return currentFrame; }
bool isAnimating() { return animating; };
bool isOffScreen();
friend class SpriteManager;

View File

@ -14,7 +14,7 @@ Sprite::Sprite(const Sprite &other) : Sprite(nullptr, 0, other.x, other.y, other
Sprite::Sprite(const void *imageData, int imageSize, int x, int y, SpriteSize size)
: x(x), y(y), data(imageData), imageSize(imageSize), spriteSize(size),
animationDelay(0), amountOfFrames(0), currentFrame(0), animationCounter(0) {
animationDelay(0), amountOfFrames(0), beginFrame(0), currentFrame(0), animationCounter(0) {
setAttributesBasedOnSize(size);
}
@ -86,8 +86,11 @@ void Sprite::updateAnimation() {
animationCounter++;
if(animationCounter > animationDelay) {
currentFrame++;
if(currentFrame > (amountOfFrames - 1)) {
currentFrame = 0;
if(currentFrame > (amountOfFrames - 1) + beginFrame) {
currentFrame = beginFrame;
}
if(currentFrame < beginFrame) {
currentFrame = beginFrame;
}
animationCounter = 0;