-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameController.h
192 lines (156 loc) · 4.65 KB
/
GameController.h
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
#ifndef _GAMECONTROLLER_H_
#define _GAMECONTROLLER_H_
#include "Support/Scene/Canyon.h"
#include "Support/Vector3.h"
#include "Support/Scene/TransformGroup.h"
#include "Support/Scene/Airplane.h"
#include "Support/Scene/Canyon.h"
#include "Support/Scene/Skybox.h"
#include "Support/Scene/Laser.h"
#include "Controller.h"
#include "Util.h"
#include "Support/HighScore.h"
extern void togglePaused();
extern Canyon *canyon;
extern Skybox *skybox;
HighScore highscore("highscore");
void enableKeyEvents();
void disableKeyEvents();
class GameController : public Controller {
public:
virtual void initialize() {
speed = 1;
timeout = 2;
killWithLaser = false;
displayDeathMessage = false;
display = new TransformGroup(Matrix4::TranslationMatrix(0, 0, 0), 2);
airplane = new Airplane(.1, .1, .1);
laser = new Laser();
Vector3 p0 = canyon->getFirstPosition();
display->addChild(*airplane);
display->addChild(*laser);
}
void reset() {
delete display;
delete airplane;
delete laser;
saveHighScore();
highscore.resetScore();
canyon->resetCanyon();
initialize();
enableKeyEvents();
}
virtual void step(double elapsed) {
airplane->step(elapsed * speed);
laser->step(elapsed);
if (displayDeathMessage) {
drawText(deathMessage, kCenter);
}
if (airplane->isDead() && airplane->doneExploding()) {
reset();
return;
}
else if (airplane->isDead()) {
return;
}
Vector3 position = airplane->getPosition();
if (position[Z] > (canyon->getYMin() + 10) * 4) {
canyon->addSegment();
}
if (timeout > 0) {
timeout--;
}
double distAboveCanyon = canyon->aboveCanyon(airplane->getNose());
if (laser->isDone() && killWithLaser) {
disableKeyEvents();
deathMessage = "You have been killed by enemy lasers!";
displayDeathMessage = true;
cerr << "Shot dead by lasers. Don't fly so high!" << endl;
airplane->kill();
}
if (timeout <= 0 && canyon->collisionWithPoint(airplane->getWingTip(true))) {
disableKeyEvents();
deathMessage = "Collision with the right wing!";
displayDeathMessage = true;
cerr << "Collision with right wing!" << endl;
airplane->kill();
}
else if (timeout <= 0 && canyon->collisionWithPoint(airplane->getWingTip(false))) {
disableKeyEvents();
deathMessage = "Collision with the left wing!";
displayDeathMessage = true;
cerr << "Collision with left wing!" << endl;
airplane->kill();
}
else if (timeout <= 0 && canyon->collisionWithPoint(airplane->getNose())) {
disableKeyEvents();
deathMessage = "Collision with the nose!";
displayDeathMessage = true;
cerr << "Collision with nose!" << endl;
airplane->kill();
}
else if (timeout <= 0 && distAboveCanyon > 0 && laser->isDone()) {
Vector3 planePosition = airplane->getPosition();
Vector3 laserDirection = Matrix4::RotationYMatrix(180).multiply(airplane->getDirection().cross(Vector3::MakeVector(0, 1, 0)).scale(50));
laser->reset(planePosition + laserDirection, planePosition);
int probability = distAboveCanyon;
if (rand() % 100 < probability) {
killWithLaser = true;
}
else {
laser->miss();
}
}
highscore.updateScore(elapsed * speed);
}
virtual void saveHighScore() {
highscore.finalize();
}
virtual void draw() {
Matrix4 identity;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
display->setCamera(identity);
canyon->draw();
glPushMatrix();
glLoadIdentity();
skybox->draw(airplane->getDirection());
glPopMatrix();
display->draw(identity);
highscore.draw();
}
virtual void keyDownHandler(int key) {
if (-key == 'a' || key == GLUT_KEY_LEFT) {
airplane->turnLeft();
}
if (-key == 'e' || key == GLUT_KEY_RIGHT) {
airplane->turnRight();
}
if (-key == ',' || key == GLUT_KEY_UP) {
airplane->turnUp();
}
if (-key == 'o' || key == GLUT_KEY_DOWN) {
airplane->turnDown();
}
if (-key == ' ')
speed = 2;
}
virtual void keyUpHandler(int key) {
if (-key == 'a' || -key == 'e' || key == GLUT_KEY_LEFT || key == GLUT_KEY_RIGHT) {
airplane->lrStopTurn();
}
if (-key == ',' || -key == 'o' || key == GLUT_KEY_UP || key == GLUT_KEY_DOWN) {
airplane->udStopTurn();
}
if (-key == ' ')
speed = 1;
}
private:
Airplane* airplane;
TransformGroup* display;
Laser* laser;
int speed, timeout;
bool killWithLaser, displayDeathMessage;
char * deathMessage;
};
#endif