-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimatedSprite.cpp
76 lines (66 loc) · 2.08 KB
/
AnimatedSprite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "AnimatedSprite.h"
//Animated Entity with a sprite, designed to be extended for any Entity with animation
//see header file for more description
AnimatedSprite::AnimatedSprite(
sf::Vector2f pPosition,
sf::Vector2f pVelocity,
sf::Vector2i pSize,
sf::Texture *pTexture,
float pAngle,
float pAngularVelocity) :
//initialization list
mSprite(*pTexture),
mPosition(pPosition),
mVelocity(pVelocity),
mSpriteSize(pSize),
mSize(pSize),
mAngle(pAngle),
mAngularVelocity(pAngularVelocity),
mCurrentAnimation(0),
mIsAnimating(false)
{
//set origin of sprite in center for easy transformations like scale/rotate
mSprite.setOrigin(pSize.x/2, pSize.y/2);
}
AnimatedSprite::~AnimatedSprite(void)
{
}
//basic update method that extended classes should call if overriding
//because this will handle animation update
void AnimatedSprite::update()
{
//move through Animation array if animating
if(mIsAnimating)
{
mAnimations.at(mCurrentAnimation).animate();
}
//else set sprite to first in animation
else
{
mAnimations.at(mCurrentAnimation).resetAnimation();
}
//move sprite by velocity
mPosition += mVelocity;
}
//draw to window, don't see a point of overriding it
//takes in pointer to window and pInterpolation (which is a variable to guess where the sprite should be between updates)
void AnimatedSprite::draw(sf::RenderWindow *window, float pInterpolation)
{
//set sprite position to position of the actual entity + interpolation
mSprite.setPosition(mPosition.x + (mVelocity.x * pInterpolation), mPosition.y + (mVelocity.y * pInterpolation));
//only draw the certain sprite needed from the sprite sheet
mSprite.setTextureRect(sf::IntRect(mAnimations[mCurrentAnimation].getCurrentFrame() * mSpriteSize.x,
mCurrentAnimation * mSpriteSize.y,
mSpriteSize.x, mSpriteSize.y));
window->draw(mSprite);
}
//switch between animating and not animating
//this could've been one method but it would've been harder to tell if starting or stopping
void AnimatedSprite::startAnimation()
{
mIsAnimating = true;
}
void AnimatedSprite::endAnimation()
{
mIsAnimating = false;
}