-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
428 lines (347 loc) · 11.8 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <GL/freeglut_std.h>
#include "player.h"
#include "behaviourupdate.h"
#include "bomb.h"
#include "bullet.h"
#include "cube.h"
wf_object_t* player_t::sPlayerBodyModel = NULL;
float player_t::sPlayerBodyModelRadius = 0;
vector3f player_t::sBombDoor(0, 0, -1);
player_t::player_t(takeoff_t* takeoff, float radius) :
takeoff(takeoff), radius(radius) {
position = takeoff->getStart();
controller = new airplane_movement_t(position);
cannon = new cannon_t(vector3f(radius * 0.8f, 0, -radius * 0.2f));
float x = 1.0f * radius;
float y = 0.6f * radius;
float z = -0.19f * radius;
propellerLeft = new propeller_t(vector3f(x, y, z));
propellerRight = new propeller_t(vector3f(x, -y, z));
float factor = 0.5f / radius;
propellerLeft->setScaleFactor(factor);
propellerRight->setScaleFactor(factor);
cockpitOffset = vector3f(radius * 0.4f, 0, 0);
horizontal = takeoff->getHorizontalAngle();
vertical = takeoff->getVerticalAngle();
}
void player_t::setManager(projectile_manager_t* manager) {
this->manager = manager;
cannon->setManager(manager);
}
void player_t::sInit(wf_object_loader_t& loader) {
sPlayerBodyModel = loader.loadRes("trenoSemHelice");
sPlayerBodyModel->scale(1 / loader.getMostDistantVertex().length());
}
void player_t::cannonView() {
vector3f off = cannon->getOffset();
// off.x -= radius * 0.35f;
off.rotateX(horizontalAngularVelocityDrawing);
off.rotateY(vertical);
off.rotateZ(horizontal);
point3f p = position + off;
point3f ep = position;
vector3f v = cannon->getOffset() + cannon->getDirection() * cannon->getLength();
v.rotateX(horizontalAngularVelocityDrawing);
v.rotateY(vertical);
v.rotateZ(horizontal);
ep += v;
float h = radius * 0.3f;
gluLookAt(p.x, p.y, p.z + h,
ep.x, ep.y, ep.z + h,
0, 0, 1);
}
void player_t::cockpitView() {
vector3f up(0, 0, 1);
vector3f forward(1, 0, 0);
up.rotateX(horizontalAngularVelocityDrawing);
up.rotateY(vertical);
up.rotateZ(horizontal);
// forward = forward.rotateX(horizontalAngularVelocityDrawing);
forward.rotateY(vertical);
forward.rotateZ(horizontal);
vector3f ck = cockpitOffset;
ck.rotateX(horizontalAngularVelocityDrawing);
ck.rotateY(vertical);
ck.rotateZ(horizontal);
point3f p = position + ck;
gluLookAt(p.x, p.y, p.z,
p.x + forward.x, p.y + forward.y, p.z + forward.z,
up.x, up.y, up.z);
}
void player_t::keyPress(unsigned char key) {
switch (mBehaviour) {
case Behaviour::ON_GROUND:
if (key == 'u' || key == 'U') {
// lets start the takeoff
printf("The airplane will take off!\n");
mBehaviour = Behaviour::TAKING_OFF;
}
break;
case Behaviour::TAKING_OFF:
// reset is handled by the Game class.
break;
case Behaviour::CONTROLLING:
// The user takes control of the airplane
controller->keyPress(key);
break;
case Behaviour::GAME_OVER:
break;
}
}
void player_t::keyRelease(unsigned char key) {
if (mBehaviour == Behaviour::CONTROLLING) {
controller->keyReleased(key);
}
}
void player_t::update(int millis) {
// The player has four behaviours, but only two of them are needed in this
// update.
behaviour_update_t* behaviour = NULL;
switch (mBehaviour) {
case Behaviour::TAKING_OFF:
behaviour = takeoff;
break;
case Behaviour::CONTROLLING:
behaviour = controller;
break;
case Behaviour::ON_GROUND:
// the horizontal is constant
horizontal = takeoff->getHorizontalAngle();
// the player is at the ground parallel to it
vertical = 0;
// There is no rotation around its own axis yet.
horizontalAngularVelocity = 0;
break;
}
float velocity = 0;
if (behaviour != NULL) {
behaviour->update(millis);
position = behaviour->getPosition();
velocity = behaviour->getVelocityMagnitude();
// printf("(%f, %f, %f)\n", position.x, position.y, position.z);
horizontal = behaviour->getHorizontalAngle();
vertical = behaviour->getVerticalAngle();
// There is no rotation around its own axis yet.
const float angleFactor = 1.0f;
horizontalAngularVelocity = behaviour->getHorizontalAngularVelocity() * angleFactor;
}
if (mBehaviour == Behaviour::TAKING_OFF && takeoff->isCompleted()) {
// transition from takeoff to give the controller to the user.
mBehaviour = Behaviour::CONTROLLING;
controller->setPosition(position);
controller->setInitialMagnitude(takeoff->getFinalVelocity() * velocityFactor);
controller->setAngles(takeoff->getHorizontalAngle(), 0);
}
const float k = 0.9f;
horizontalAngularVelocityDrawing *= k;
horizontalAngularVelocityDrawing += horizontalAngularVelocity * (1 - k);
propellerLeft->update(millis);
propellerRight->update(millis);
propellerLeft->setMagnitude(velocity);
propellerRight->setMagnitude(velocity);
}
void player_t::draw(bool cockpit, bool gun, bool body, bool aim) {
if (dead) {
return;
}
// TODO Add beam of light from the player when it is night mode
// glDisable(GL_CULL_FACE);
glPushMatrix();
{
// if (aim) {
// point3f p = position;
//
// vector3f v = cannon->getOffset();
//
// v.rotateX(horizontalAngularVelocityDrawing);
// v.rotateY(vertical);
// v.rotateZ(horizontal);
//
// // glPushMatrix();
// // {
// // glTranslatef(p.x + v.x, p.y + v.y, p.z + v.z);
// // glutSolidSphere(2, 8, 8);
// // }
// // glPopMatrix();
//
// v = cannon->getOffset() + cannon->getDirection() * cannon->getLength();
//
// v.rotateX(horizontalAngularVelocityDrawing);
// v.rotateY(vertical);
// v.rotateZ(horizontal);
//
// p += v;
//
// glPushMatrix();
// {
// glTranslatef(p.x, p.y, p.z);
// glutSolidSphere(2, 8, 8);
// }
// glPopMatrix();
//
// v = cannon->getDirection();
//
// v.rotateX(horizontalAngularVelocityDrawing);
// v.rotateY(vertical);
// v.rotateZ(horizontal);
//
// p += v * 5;
//
// glPushMatrix();
// {
// glTranslatef(p.x, p.y, p.z);
// glutSolidSphere(2, 8, 8);
// }
// glPopMatrix();
// }
glTranslatef(position.x, position.y, position.z);
drawAxis(radius);
// considering that the front of the airplane is at +x and the back is at -x
const float degreePerRadians = 180 / M_PI;
glRotatef(horizontal * degreePerRadians, 0, 0, 1);
glRotatef(vertical * degreePerRadians, 0, 1, 0);
glRotatef(horizontalAngularVelocityDrawing * degreePerRadians, 1, 0, 0);
if (gun) {
cannon->draw();
}
// cockpit
if (cockpit) {
glPushMatrix();
{
glTranslatef(cockpitOffset.x, cockpitOffset.y, cockpitOffset.z);
// drawBox();
}
glPopMatrix();
}
// glPushMatrix();
// {
// glTranslatef(cockpitOffset.x, cockpitOffset.y, cockpitOffset.z);
// glutSolidSphere(1, 8, 8);
// }
// glPopMatrix();
if (body) {
glPushMatrix();
{
// glRotatef(90, 0, 1, 0);
propellerLeft->transformAndDraw(radius);
propellerRight->transformAndDraw(radius);
}
glPopMatrix();
glRotatef(90, 1, 0, 0);
glScalef(radius, radius, radius);
// glutSolidSphere(1, 8, 8);
drawAxis(radius);
const float s = 1.3;
glScalef(s, s, s);
GLfloat color[] = {1, 1, 1, 1};
glColor3fv(color);
// glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
// glMaterialfv(GL_FRONT, GL_SPECULAR, color);
// glMaterialf(GL_FRONT, GL_SHININESS, 50.0f);
if (sPlayerBodyModel == NULL) {
glutSolidSphere(2, 32, 32);
} else {
sPlayerBodyModel->draw();
}
}
}
glPopMatrix();
}
vector3f player_t::getVelocity() const {
return controller->getVelocity();
}
vector3f player_t::getDirection(float vt) const {
vector3f v(1, 0, 0);
v.rotateY(vertical + vt);
v.rotateZ(horizontal);
return v;
}
void player_t::setPosition(const point3f& p) {
position = p;
controller->setPosition(p);
}
const char* player_t::getName() const {
return "player";
}
void player_t::clipZ(float height) {
if (mBehaviour != Behaviour::CONTROLLING) return;
if (position.z < radius) {
// printf("The player is hitting the ground!\n");
position.z = radius;
// kill();
} else if (position.z > height - radius) {
// printf("The player is hitting the roof!\n");
position.z = height - radius;
}
}
void player_t::bomb() {
printf("The player just threw a bomb\n");
vector3f v = controller->getVelocity();
v.z = 0;
bomb_t* b = new bomb_t(position + sBombDoor * radius, v, horizontal * 180 / M_PI);
b->setRadius(radius / 8);
manager->addProjectile(b);
if (bombListener != NULL) {
bombListener->onBombThrow(b);
}
}
void player_t::fire() {
point3f p = position;
vector3f v = cannon->getOffset();
v.rotateX(horizontalAngularVelocityDrawing);
v.rotateY(controller->getVerticalAngle());
v.rotateZ(controller->getHorizontalAngle());
point3f p1 = p + v;
v = cannon->getOffset() + cannon->getDirection() * cannon->getLength();
v.rotateX(horizontalAngularVelocityDrawing);
v.rotateY(vertical);
v.rotateZ(horizontal);
p += v;
vector3f v2 = p - p1;
v2.normalize();
bullet_t* bullet = new bullet_t(p,
v2 * (bulletVelocityFactor * controller->getMagnitude()), false);
bullet->setRadius(radius / 16);
manager->addProjectile(bullet);
}
void player_t::mousePress(int button) {
if (mBehaviour == Behaviour::CONTROLLING) {
switch (button) {
case GLUT_RIGHT_BUTTON:
bomb();
break;
case GLUT_LEFT_BUTTON:
fire();
break;
}
}
}
void player_t::kill() {
if (!canDie() || dead) return;
dead = true;
mBehaviour = Behaviour::GAME_OVER;
if (death != NULL) {
death->onPlayerDeath();
}
}
bool player_t::canDie() const {
return mBehaviour == Behaviour::CONTROLLING;
}
void player_t::setCannonAxis(float x, float y) {
cannon->setInputAxis(x, y);
}
void player_t::won() {
mBehaviour = Behaviour::GAME_OVER;
}
void player_t::drawMapElement(circle_blueprint_t* blueprint) const {
if (dead) return;
// Make the player green
glColor3f(0, 1, 0);
glPushMatrix();
{
// Draw a circle at the player position
glTranslatef(position.x, position.y, 0);
blueprint->draw(true, radius);
}
glPopMatrix();
}