-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
155 lines (138 loc) · 4.49 KB
/
player.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "player.h"
#include "gameData.h"
#include "imageFactory.h"
void Player::advanceFrame(Uint32 ticks) {
timeSinceLastFrame += ticks;
if (timeSinceLastFrame > frameInterval) {
currentFrame = (currentFrame+1) % numberOfFrames;
timeSinceLastFrame = 0;
}
}
void Player::reverseAdvanceFrame(Uint32 ticks) {
timeSinceLastFrame += ticks;
if (timeSinceLastFrame > frameInterval) {
currentFrame = (currentFrame-1) % numberOfFrames;
timeSinceLastFrame = 0;
}
}
Player::Player( 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_left( ImageFactory::getInstance().getImages(name+"_Left") ),
images_right( ImageFactory::getInstance().getImages(name+"_Right") ),
current_images(images_right),
bounds({(int)getX()-2,(int)getY()-2, 5, 5}),
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")),
initialVelocity(getVelocity())
{}
Player::Player(const Player& s) :
Drawable(s),
images_left(s.images_left),
images_right(s.images_right),
bounds(s.bounds),
currentFrame(s.currentFrame),
numberOfFrames( s.numberOfFrames ),
frameInterval( s.frameInterval ),
timeSinceLastFrame( s.timeSinceLastFrame ),
worldWidth( s.worldWidth ),
worldHeight( s.worldHeight ),
initialVelocity( s.initialVelocity )
{ }
Player& Player::operator=(const Player& s) {
Drawable::operator=(s);
images_left = (s.images_left);
images_right = (s.images_right);
bounds = (s.bounds);
currentFrame = (s.currentFrame);
numberOfFrames = ( s.numberOfFrames );
frameInterval = ( s.frameInterval );
timeSinceLastFrame = ( s.timeSinceLastFrame );
worldWidth = ( s.worldWidth );
worldHeight = ( s.worldHeight );
initialVelocity = ( s.initialVelocity );
return *this;
}
void Player::draw() const {
current_images[currentFrame]->draw(getX(), getY(), getScale());
}
void Player::stop() {
//setVelocity( Vector2f(0, 0) );
setVelocityX( 0.93*getVelocityX() );
setVelocityY(0);
}
void Player::right(Uint32 ticks) {
//bool x = (current_images == images_left);
//printf("images_left == %s", x ? "true" : "false");
if (current_images == images_left){
while (getCurrentFrameNum() < numberOfFrames-1) {
//printf("current left to right frame num is %d\n", getCurrentFrameNum() );
advanceFrame(ticks);
}
current_images = images_right;
}
while (getCurrentFrameNum() < numberOfFrames-1) {
//printf("current right to right frame num is %d\n", getCurrentFrameNum() );
advanceFrame(ticks);
}
//if ( getX() < worldWidth-getScaledWidth()) {
// setVelocityX(initialVelocity[0]);
//}
}
void Player::left(Uint32 ticks) {
//bool x = (current_images == images_right);
//printf(" images_right == %s", x ? "true" : "false");
if (current_images == images_right){
while (getCurrentFrameNum() > 0) {
//printf("current right to left frame num is %d\n", getCurrentFrameNum() );
reverseAdvanceFrame(ticks);
}
current_images = images_left;
}
while (getCurrentFrameNum() > 0) {
//printf("current left to _Left frame num is %d\n", getCurrentFrameNum() );
reverseAdvanceFrame(ticks);
}
/*if ( getX() > 0) {
setVelocityX(-initialVelocity[0]);
}*/
}
void Player::up() {
if (currentFrame != 0) {
currentFrame = 0;
}
current_images = images_right;
//if ( getY() > 0) {
// setVelocityY( -initialVelocity[1] );
//}
}
void Player::down() {
if ( getY() < worldHeight-getScaledHeight()) {
setVelocityY( initialVelocity[1] );
}
}
void Player::update(Uint32 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() ) );
}
stop();
}