-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultisprite.cpp
77 lines (66 loc) · 2.27 KB
/
multisprite.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
76
77
#include "multisprite.h"
#include "gameData.h"
#include "imageFactory.h"
void MultiSprite::advanceFrame(Uint32 ticks) {
timeSinceLastFrame += ticks;
if (timeSinceLastFrame > frameInterval) {
currentFrame = (currentFrame+1) % numberOfFrames;
timeSinceLastFrame = 0;
}
}
MultiSprite::MultiSprite( const std::string& name) :
Drawable(name,
Vector2f(Gamedata::getInstance().getXmlInt(name+"/startLoc/x"),
Gamedata::getInstance().getXmlInt(name+"/startLoc/y")),
Vector2f(Gamedata::getInstance().getXmlInt(name+"/speedX"),
Gamedata::getInstance().getXmlInt(name+"/speedY"))
),
images( ImageFactory::getInstance().getImages(name) ),
currentFrame(0),
numberOfFrames( Gamedata::getInstance().getXmlInt(name+"/frames") ),
frameInterval( Gamedata::getInstance().getXmlInt(name+"/frameInterval")),
timeSinceLastFrame( 0 ),
worldWidth(Gamedata::getInstance().getXmlInt("background/width")),
worldHeight(Gamedata::getInstance().getXmlInt("background/height"))
{ }
MultiSprite::MultiSprite(const MultiSprite& s) :
Drawable(s),
images(s.images),
currentFrame(s.currentFrame),
numberOfFrames( s.numberOfFrames ),
frameInterval( s.frameInterval ),
timeSinceLastFrame( s.timeSinceLastFrame ),
worldWidth( s.worldWidth ),
worldHeight( s.worldHeight )
{ }
MultiSprite& MultiSprite::operator=(const MultiSprite& s) {
Drawable::operator=(s);
images = (s.images);
currentFrame = (s.currentFrame);
numberOfFrames = ( s.numberOfFrames );
frameInterval = ( s.frameInterval );
timeSinceLastFrame = ( s.timeSinceLastFrame );
worldWidth = ( s.worldWidth );
worldHeight = ( s.worldHeight );
return *this;
}
void MultiSprite::draw() const {
images[currentFrame]->draw(getX(), getY(), getScale());
}
void MultiSprite::update(Uint32 ticks) {
advanceFrame(ticks);
Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001;
setPosition(getPosition() + incr);
if ( getY() < 0) {
setVelocityY( fabs( getVelocityY() ) );
}
if ( getY() > worldHeight-getScaledHeight()) {
setVelocityY( -fabs( getVelocityY() ) );
}
if ( getX() < 0) {
setVelocityX( fabs( getVelocityX() ) );
}
if ( getX() > worldWidth-getScaledWidth()) {
setVelocityX( -fabs( getVelocityX() ) );
}
}