-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandleCommands.hpp
226 lines (200 loc) · 6.93 KB
/
HandleCommands.hpp
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
#include "Parameters.hpp"
void SandroRun::controller() {
if (glfwGetKey(window, GLFW_KEY_ESCAPE)) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
// Variables
float deltaT, time;
bool fire = false;
glm::vec3 m = glm::vec3(0.0f);
glm::vec3 r = glm::vec3(0.0f);
glm::vec3 ux = glm::vec3(1, 0, 0);
glm::vec3 uy = glm::vec3(0, 1, 0);
glm::vec3 uz = glm::vec3(0, 0, -1);
// Input acquisition
getSixAxis(deltaT, m, r, fire, time);
// Fire handling
bool handleFire = (!(wasFire) && fire);
if (handleFire) {
holdFire = !holdFire && scene.gameState;
}
wasFire = fire;
// Game beginning
switch (scene.gameState) {
case INITIAL_SCREEN:
if (handleFire) {
std::cout << "Game started" << std::endl;
scene.gameState = GAME_SCREEN;
scene.currText = 1;
scene.startTime = time;
createCommandBuffers();
refreshAudio();
}
break;
case GAME_SCREEN:
mainGame(deltaT, time, m, r, ux, uz);
break;
case GAME_OVER_ANIMATION:
gameOverAnimation(deltaT);
break;
case GAME_OVER_SCREEN:
// Splash screen fading
if (scene.splashVisibility != 1.0f) {
scene.splashVisibility = glm::clamp(scene.splashVisibility + 2 * deltaT, 0.0f, 1.0f);
}
if (handleFire) {
resetGame();
std::cout << "Game restarted" << std::endl;
createCommandBuffers();
}
break;
}
}
void SandroRun::mainGame(float deltaT, float time, glm::vec3 m, glm::vec3 r, glm::vec3 ux, glm::vec3 uz) {
// Collisions detection
if (!holdP) {
if (checkCollisionsWithCars() || checkCollisionsWithGuardRails()) {
scene.gameState = GAME_OVER_ANIMATION;
refreshAudio();
return;
}
}
// Daytime update
bool n = glfwGetKey(window, GLFW_KEY_N);
if (n && !wasN) {
scene.dayTime = (scene.dayTime + 1) % 3;
}
wasN = n;
// Presentation mode
bool p = glfwGetKey(window, GLFW_KEY_P);
bool handleP = (!(wasP) && p);
if (handleP) {
holdP = !holdP;
if (holdP) {
std::cout << "Presentation mode ON" << std::endl;
} else {
std::cout << "Presentation mode OFF" << std::endl;
}
}
wasP = p;
// Change camera mode
bool c = glfwGetKey(window, GLFW_KEY_C);
bool handleC = (!(wasC) && c);
if (handleC) {
holdC = !holdC;
if (scene.gameState == GAME_SCREEN) {
createCommandBuffers();
}
}
wasC = c;
// Camera reset
bool r_key = glfwGetKey(window, GLFW_KEY_R);
if (!wasR && r_key) {
camera.yaw = STARTING_YAW;
camera.yawNew = STARTING_YAW;
camera.pitch = STARTING_PITCH;
camera.pitchNew = STARTING_PITCH;
}
wasR = r_key;
// Splash screen fading
if (scene.splashVisibility != 0.0f) {
scene.splashVisibility = glm::clamp(scene.splashVisibility - deltaT, 0.0f, 1.0f);
}
// 3rd person camera fix
if (!holdC) {
r.x = -r.x;
r.y = -r.y;
}
// Camera rotation
camera.pitchNew += ROT_SPEED * r.x * deltaT;
if (!holdP) {
camera.pitchNew = glm::clamp(camera.pitchNew, MIN_PITCH, MAX_PITCH);
}
camera.pitch = camera.pitch * glm::exp(-LAMBDA * deltaT) +
camera.pitchNew * (1 - glm::exp(-LAMBDA * deltaT)); // Pitch damping
if (!holdP) {
camera.pitch = glm::clamp(camera.pitch, MIN_PITCH, MAX_PITCH);
}
camera.yawNew += ROT_SPEED * r.y * deltaT;
if (!holdP) {
camera.yawNew = glm::clamp(camera.yawNew, MIN_YAW, MAX_YAW);
}
camera.yaw =
camera.yaw * glm::exp(-LAMBDA * deltaT) + camera.yawNew * (1 - glm::exp(-LAMBDA * deltaT)); // Yaw damping
if (!holdP) {
camera.yaw = glm::clamp(camera.yaw, MIN_YAW, MAX_YAW);
}
// Score update
int curr = (int) (abs(moto.pos.z) / ONE_POINT_SCORE_DISTANCE) + 1;
if (scene.currText != curr && scene.currText != 101) {
scene.currText = curr > 101 ? 101 : curr;
createCommandBuffers();
}
// Vehicles movement
updateMoto(deltaT, time, m, ux, uz);
updateCars(deltaT);
camera.rollNew = moto.roll;
camera.roll = camera.roll * glm::exp(-LAMBDA * deltaT) +
camera.rollNew * (1 - glm::exp(-LAMBDA * deltaT)); // Yaw damping
}
void SandroRun::viewHandler(glm::mat4 &ViewProj, glm::mat4 &World) {
World = glm::translate(glm::mat4(1), moto.pos);
ViewProj = glm::perspective(FOV_Y, Ar, NEAR_PLANE, FAR_PLANE);
ViewProj[1][1] *= -1;
if (holdC) {
// First person view
ViewProj *= glm::rotate(glm::mat4(1.0), (float) (camera.pitch - STARTING_PITCH), glm::vec3(1, 0, 0)) *
glm::rotate(glm::mat4(1.0), camera.yaw, glm::vec3(0, 1, 0)) *
glm::rotate(glm::mat4(1.0), -camera.roll / 2.0f, glm::vec3(0, 0, 1)) *
glm::translate(glm::mat4(1.0), -moto.pos + glm::vec3((-CAM_HEIGHT - .25f) * sin(-camera.roll),
(-CAM_HEIGHT - .25f) * cos(camera.roll) -
(CAM_HEIGHT / 2) * sin(moto.pitch),
-sin(moto.pitch)));
camera.pos = World * glm::vec4(0, CAM_HEIGHT, 0, 1);
} else {
// Third person view
glm::mat4 rot =
glm::rotate(glm::mat4(1), camera.yaw, glm::vec3(0, 1, 0)) *
glm::rotate(glm::mat4(1), camera.pitch, glm::vec3(1, 0, 0));
camera.pos = World * rot * glm::vec4(0, CAM_DIST, 0, 1);
glm::vec3 a = World * glm::vec4(0, 0, 0, 1) + glm::vec4(0, CAM_HEIGHT, 0, 0);
ViewProj *= glm::lookAt(camera.pos, a, glm::vec3(0, 1, 0));
}
}
void SandroRun::resetGame() {
// Camera position and orientation
camera.yaw = STARTING_YAW;
camera.yawNew = STARTING_YAW;
camera.pitch = STARTING_PITCH;
camera.pitchNew = STARTING_PITCH;
camera.pos = glm::vec3(0.0f, 0.0f, 0.0f);
// Moto position and orientation
moto.pos = glm::vec3(0.0f, 0.0f, 0.0f);
moto.speed = 0;
moto.roll = 0;
moto.pitch = 0;
moto.wheelPitch = 0;
// Game state
scene.currText = 0;
scene.gameState = INITIAL_SCREEN;
scene.splashVisibility = 1.0f;
scene.frontWorldLimit = -WORLD_LENGTH;
scene.backWorldLimit = 0;
scene.dayTime = DAY;
scene.startTime = 0;
wasN = false;
wasC = false;
wasR = false;
// Cars positions
initCars();
// Audio
refreshAudio();
}
void SandroRun::refreshAudio() {
// Audio
sound_state.mutex.lock();
sound_state.gameState = scene.gameState;
sound_state.reload = true;
sound_state.stop = false;
sound_state.mutex.unlock();
}