-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwMultisprite.cpp
More file actions
103 lines (85 loc) · 2.78 KB
/
twMultisprite.cpp
File metadata and controls
103 lines (85 loc) · 2.78 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "twMultisprite.h"
#include "gamedata.h"
#include "frameFactory.h"
void TWMultiSprite::advanceFrame(Uint32 ticks) {
timeSinceLastFrame += ticks;
if (timeSinceLastFrame > frameInterval) {
currentFrame = (currentFrame+1) % numberOfFrames;
timeSinceLastFrame = 0;
}
}
TWMultiSprite::TWMultiSprite( const std::string& name) :
Drawable(name,
Vector2f(Gamedata::getInstance().getXmlInt(name+"/Loc/X"),
Gamedata::getInstance().getXmlInt(name+"/Loc/Y")),
Vector2f(Gamedata::getInstance().getXmlInt(name+"/SpeedX"),
Gamedata::getInstance().getXmlInt(name+"/SpeedY")),
0,0,
Gamedata::getInstance().getXmlInt(name+"/Depth")
),
frames( FrameFactory::getInstance().getFrames(name+"left") ),
framesMirror( FrameFactory::getInstance().getFrames(name+"right") ),
worldWidth(Gamedata::getInstance().getXmlInt("world/Width")),
worldHeight(Gamedata::getInstance().getXmlInt("world/Height")),
currentFrame(0),
numberOfFrames( Gamedata::getInstance().getXmlInt(name+"/Frames") ),
frameInterval( Gamedata::getInstance().getXmlInt(name+"/FrameInterval") ),
timeSinceLastFrame( 0 ),
frameWidth(frames[0]->getWidth()),
frameHeight(frames[0]->getHeight()),
flag(false),
flagStop(false)
{ }
TWMultiSprite::TWMultiSprite(const TWMultiSprite& s) :
Drawable(s),
frames(s.frames),
framesMirror(s.framesMirror),
worldWidth( s.worldWidth ),
worldHeight( s.worldHeight ),
currentFrame(s.currentFrame),
numberOfFrames( s.numberOfFrames ),
frameInterval( s.frameInterval ),
timeSinceLastFrame( s.timeSinceLastFrame ),
frameWidth( s.frameWidth ),
frameHeight( s.frameHeight ),
flag(s.flag),
flagStop(s.flagStop)
{ }
void TWMultiSprite::draw() const {
Uint32 x = static_cast<Uint32>(X());
Uint32 y = static_cast<Uint32>(Y());
if (!flag)
{ frames[currentFrame]->draw(x, y);}
else
{
framesMirror[currentFrame]-> draw(x,y);}
}
void TWMultiSprite::update(Uint32 ticks) {
advanceFrame(ticks);
if (!flagStop) {
Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001;
setPosition(getPosition() + incr);}
if ( Y() < 0) {
velocityY( abs( velocityY() ) );
Y(0);
}
if ( Y() > worldHeight-frameHeight) {
velocityY( -abs( velocityY() ) );
Y(worldHeight-frameHeight);
}
if ( X() < 0) {
velocityX( abs( velocityX() ) );
flag = true;
X(0);
}
if ( X() > worldWidth-frameWidth) {
velocityX( -abs( velocityX() ) );
flag = false;
X( worldWidth-frameWidth);
}
}
//void TWMultiSprite::right() { X(X()+4.0); flag=true;}
//void TWMultiSprite::left() { X(X()-4.0); flag=false;}
//void TWMultiSprite::up() { velocityY(velocityY()-575.0);}
//void TWMultiSprite::down() { velocityY(velocityY()+5.0);}
//void TWMultiSprite::stop() { flagStop=true;}