-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.cpp
226 lines (193 loc) · 3.34 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
William Francis
Corrigan Farley
Randy Tobias
*/
#include "player.h"
Player::Player(Texture* left,Texture* right, Texture* front, Texture* jRight, Texture *jLeft)
{
texLeft = left;
texRight = right;
texFront = front;
texRightJump = jRight;
texLeftJump = jLeft;
x = 0.0;
y = 0.0;
speed = 0.0;
health = 100.0;
}
Player::Player()
{
x = 0.0;
y = 0.0;
speed = 0.0;
health = 100.0;
texFront = new Texture();
texRight = new Texture();
texLeft = new Texture();
texLeftJump = new Texture();
texRightJump = new Texture();
}
void Player::setPosition(float x, float y)
{
this->x = x;
this->y = y;
}
void Player::move(float x, float y)
{
}
void Player::setX(float x)
{
this->x = x;
}
void Player::setY(float y)
{
this->y = y;
}
float Player::getX()
{
return x;
}
float Player::getY()
{
return y;
}
float Player::getSpeed()
{
return speed;
}
void Player::setSpeed(float speed)
{
this->speed = speed;
}
float Player::getHealth()
{
return health;
}
void Player::setHealth(float health)
{
this->health = health;
}
void Player::moveLeft()
{
v.x-=2.5;
left = true;
}
void Player::moveRight()
{
v.x+=2.5;
right = true;
}
void Player::stopLeft()
{
if ((v.x != 0) || right)
v.x+=2.5;
left = false;
}
void Player::stopRight()
{
if ((v.x != 0) || left)
v.x-=2.5;
right = false;
}
void Player::moveUp()
{
if (colliding)
v.y = -10;
}
void Player::moveDown()
{
}
void Player::draw()
{
glTranslatef(x,y,-1.0);
if (texFront->isValid()) {
glEnable( GL_TEXTURE_2D );
} else {
glDisable( GL_TEXTURE_2D );
}
texFront->bind();
if (right) {
if (colliding) {
texRight->bind();
} else {
texRightJump->bind();
}
} else if (left) {
if (colliding) {
texLeft->bind();
} else {
texLeftJump->bind();
}
}
glBegin(GL_QUADS);
glColor4f(1.0,1.0,1.0,1.0);
glTexCoord2i( 0,0 );
glVertex3f(0,0,0);
glTexCoord2i( 1,0 );
glVertex3f(PLAYER_WIDTH,0,0);
glTexCoord2i( 1,1 );
glVertex3f(PLAYER_WIDTH,PLAYER_HEIGHT,0);
glTexCoord2i( 0,1 );
glVertex3f(0,PLAYER_HEIGHT,0);
glEnd();
// Reset original translate
glTranslatef(-x,-y,1.0);
}
void Player::setLevel(Level *level) {
this->level = level;
}
float Player::getHeight() {
return PLAYER_HEIGHT;
}
float Player::getWidth() {
return PLAYER_WIDTH;
}
bool Player::singleCollide(GameObject *obj) {
if ((y + PLAYER_HEIGHT) < obj->getY()) return false;
if ((obj->getY() + obj->getHeight()) < y) return false;
if ((x + PLAYER_WIDTH) < obj->getX()) return false;
if ((obj->getX() + obj->getWidth()) < x) return false;
return true;
}
bool Player::collides() {
std::vector<GameObject*>* objects = level->getPlatforms();
std::vector<GameObject*>::iterator i;
for (i=objects->begin();i<objects->end();i++) {
if (singleCollide(*i)) return true;
}
return false;
}
void Player::doMovement(float dT)
{
accel.y = 9.8;
v.x += accel.x*dT;
v.y += accel.y*dT;
colliding = false;
y += v.y*dT*SCALE_FACTOR;
if (collides()) {
y -= v.y*dT*SCALE_FACTOR;
accel.y = 0.0;
v.y = 0.0;
colliding = true;
}
x += v.x*dT*SCALE_FACTOR;
if (collides()) {
x -= v.x*dT*SCALE_FACTOR;
accel.x = 0.0;
v.x = 0.0;
colliding = true;
}
}
/*
void setPosition(float, float);
void setX(float);
void setY(float);
float getX();
float getY();
float getSpeed();
void setSpeed(float);
float getHealth();
void setHealth(float);
void draw();
*/