-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
450 lines (392 loc) · 18.8 KB
/
main.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <cmath>
#include <fstream>
#include <map>
#include <queue>
#include <stack>
#include <SOIL/SOIL.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "Utilities/graphics_utils.h"
#include "Utilities/fonts.h"
#include "Model/PenModel.h"
#include "Models/GameScene.h"
#include "Models/Penguin.h"
#include <irrKlang/irrKlang.h>
using namespace std;
GLfloat deltaTime = 0.0f; // Time between current frame and last frame
GLfloat lastFrame = 0.0f;
Camera camera(glm::vec3(0.0f, 4.0f, 5.0f));
long score = 0;
bool gameOver = false;
bool exitGame = false;
bool gameOverSoundPlayed= false;
GLFWwindow* window;
float lightX = -22, lightY = 35, lightZ = -30, lookAtX = 0,lookAtY = 0,lookAtZ = -25;
vector<Lane> lanesArray;
collisionStatus collisionState;
//For Rendering the DepthMap (Debuging and testing)
GLuint quadVAO = 0;
GLuint quadVBO;
<<<<<<< HEAD
int main(int argc, const char * argv[]) {
srand( time( NULL ) );
||||||| merged common ancestors
int main(int argc, const char * argv[]) {
=======
/**************
TESTING
**************/
int iX = 0, jZ = 0;
int penmove;
float targetPenX ;
bool isLookingForPathOrMoving = false;
Penguin* testPen;
struct bfsNode{
bfsNode* next;
bfsNode* parent;
int nodeX = 0, laneZIndex = 0;
int move = 0;
int accumulatedFramesCount =0;
bfsNode(){
next = parent = 0;
}
};
bfsNode* currentbfsNode;
bfsNode* test(bfsNode* currentbfsNode){
isLookingForPathOrMoving = true;
vector<vector<int> > visited(50, std::vector<int> ( 17, 0)); // 50 * 17 vector to hold visited
int requZ = currentbfsNode->laneZIndex + 6;
visited[currentbfsNode->laneZIndex][currentbfsNode->nodeX] = 1;
if(currentbfsNode->laneZIndex != testPen->getCurrentLane()){
cout <<"ERROR " << currentbfsNode->laneZIndex << " " << testPen->getCurrentLane()<<endl;
}
queue<bfsNode*> q;
q.push(currentbfsNode);
bfsNode* tmp;
float backupX = testPen->getPenX();
float backupZ = testPen->getPenZ();
int backupLaneIndex = testPen->getCurrentLane();
currentbfsNode->accumulatedFramesCount=0;
while(!q.empty()){
tmp = q.front();
q.pop();
if (tmp->laneZIndex == requZ) { // check if reached required
isLookingForPathOrMoving= false;
testPen->setPenZ(backupZ);
testPen->setPenX(backupX);
testPen->setCurrentLaneIndex(backupLaneIndex);
return tmp;
}
Lane nextLane, previousLane, currentLane;
nextLane = lanesArray[tmp->laneZIndex +1];
currentLane = lanesArray[tmp->laneZIndex];
previousLane = lanesArray[tmp->laneZIndex -1];
testPen->setPenZ(nextLane.laneZPos);
testPen->setPenX(tmp->nodeX-9);
testPen->setCurrentLaneIndex(tmp->laneZIndex +1);
float framesToBeAdded = 0;
if(nextLane.getLaneCarXPosition() > tmp->nodeX -9+2){
if(currentLane.type == SAFE_LANE && nextLane.type == SAFE_LANE)
framesToBeAdded = 25.0;
else if (currentLane.type == SAFE_LANE && nextLane.type == NORMAL_LANE ||
currentLane.type == NORMAL_LANE && nextLane.type == SAFE_LANE)
framesToBeAdded = 38.0;
else if (currentLane.type == NORMAL_LANE && nextLane.type == NORMAL_LANE)
framesToBeAdded = 52.0;
}else { // before
if(currentLane.type == SAFE_LANE && nextLane.type == SAFE_LANE)
framesToBeAdded = 25.0/3;
else if (currentLane.type == SAFE_LANE && nextLane.type == NORMAL_LANE ||
currentLane.type == NORMAL_LANE && nextLane.type == SAFE_LANE)
framesToBeAdded = 38.0/3;
else if (currentLane.type == NORMAL_LANE && nextLane.type == NORMAL_LANE)
framesToBeAdded = 52.0/3;
}
float horzFramesCount = 0;
if(currentLane.getLaneCarXPosition() > tmp->nodeX -9){
horzFramesCount = 40.0;
} else { // before
horzFramesCount = -40.0/2;
}
if(tmp->laneZIndex+1 < 50 && !visited[tmp->laneZIndex+1][tmp->nodeX] &&
testPen->detectCollisionWithAutoRun(lanesArray, framesToBeAdded) == noCollision
){
if(!(lanesArray[tmp->laneZIndex +1].type == SAFE_LANE && abs(lanesArray[tmp->laneZIndex +1].treeXpos - (tmp->nodeX-9)) < 1.3)){
bfsNode* newNode = new bfsNode;
newNode->nodeX = tmp->nodeX;
newNode->laneZIndex = tmp->laneZIndex+1;
newNode->move = 1;
newNode->parent = tmp;
newNode->accumulatedFramesCount = tmp->accumulatedFramesCount+framesToBeAdded;
tmp->next = newNode;
q.push(newNode);
visited[tmp->laneZIndex+1][tmp->nodeX] = 1;
}}
testPen->setCurrentLaneIndex(tmp->laneZIndex);
testPen->setPenZ(currentLane.laneZPos);
testPen->setPenX(tmp->nodeX-7);
if(tmp->nodeX+1 < 17 &&!visited[tmp->laneZIndex][tmp->nodeX+1] &&
testPen->detectCollisionWithAutoRun(lanesArray, horzFramesCount) == noCollision){
if((lanesArray[tmp->laneZIndex].type == LaneType::SAFE_LANE
&& (abs((tmp->nodeX-9)+1 - lanesArray[tmp->laneZIndex].treeXpos) > 1.3) || ((tmp->nodeX-9)+1 > lanesArray[tmp->laneZIndex].treeXpos)) || lanesArray[tmp->laneZIndex].type == LaneType::NORMAL_LANE){
bfsNode* newNode = new bfsNode;
newNode->nodeX = tmp->nodeX+1;
newNode->laneZIndex = tmp->laneZIndex;
newNode->parent = tmp;
newNode->move = 3;
newNode->accumulatedFramesCount = tmp->accumulatedFramesCount+20;
tmp->next = newNode;
q.push(newNode);
visited[tmp->laneZIndex][tmp->nodeX+1] = 1;
}
}
testPen->setCurrentLaneIndex(tmp->laneZIndex);
testPen->setPenZ(currentLane.laneZPos);
testPen->setPenX(tmp->nodeX-9);
if(tmp->nodeX-1 > 0 &&!visited[tmp->laneZIndex][tmp->nodeX-1]&&
testPen->detectCollisionWithAutoRun(lanesArray, horzFramesCount) == noCollision){
if((lanesArray[tmp->laneZIndex].type == LaneType::SAFE_LANE
&& (abs((tmp->nodeX-9)-1 - lanesArray[tmp->laneZIndex].treeXpos) > 1.3) || ((tmp->nodeX-9)-1 < lanesArray[tmp->laneZIndex].treeXpos)) || lanesArray[tmp->laneZIndex].type == LaneType::NORMAL_LANE){
bfsNode* newNode = new bfsNode;
newNode->nodeX = tmp->nodeX-1;
newNode->laneZIndex = tmp->laneZIndex;
newNode->parent = tmp;
newNode->move = 4;
tmp->next = newNode;
newNode->accumulatedFramesCount = tmp->accumulatedFramesCount+20;
q.push(newNode);
visited[tmp->laneZIndex][tmp->nodeX-1] = 1;
}
}
testPen->setPenZ(previousLane.laneZPos);
testPen->setCurrentLaneIndex(tmp->laneZIndex - 1);
testPen->setPenX((tmp->nodeX-9));
if(tmp->laneZIndex-1 > 0 && !visited[tmp->laneZIndex-1][tmp->nodeX] &&
testPen->detectCollisionWithAutoRun(lanesArray, framesToBeAdded) == noCollision){
if(!(lanesArray[tmp->laneZIndex -1].type == SAFE_LANE && abs(lanesArray[tmp->laneZIndex -1].treeXpos - (tmp->nodeX-9)) < 1.3)){
bfsNode* newNode = new bfsNode;
newNode->nodeX = tmp->nodeX;
newNode->laneZIndex = tmp->laneZIndex-1;
newNode->parent = tmp;
newNode->move = 2;
newNode->accumulatedFramesCount = tmp->accumulatedFramesCount+framesToBeAdded;
tmp->next = newNode;
q.push(newNode);
visited[tmp->laneZIndex-1][tmp->nodeX] = 1;
}
}
}
testPen->setPenZ(backupZ);
testPen->setPenX(backupX);
testPen->setCurrentLaneIndex(backupLaneIndex);
isLookingForPathOrMoving= false;
return 0;
}
int main(int argc, const char * argv[]) {
srand(time(NULL));
>>>>>>> autorun
// initialize game
GraphicsUtilities graphicsUtilities(&camera);
if(0 != graphicsUtilities.initializeGameWindow(Constants::gameWidth, Constants::gameHeight, 3, 3, window)){
return -1;
}
// Load SHADERS
Shader sceneShader("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/shader.vs", "/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/shader.frag");
Shader materialShader("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/objshader.vs", "/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/objshader.frag");
Shader textureShader("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/shadow_mapping.vs", "/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/shadow_mapping.frag");
Shader fontShader("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/font.vs", "/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/font.frag");
Shader simpleDepthShader("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/shadow_mapping_depth.vs", "/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Shaders/shadow_mapping_depth.frag");
// start the sound engine with default parameters
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
if (!engine)
{
printf("Could not startup engine\n");
return 0; // error starting up the engine
}
// Options
GLboolean shadows = true;
// Load gamescene
GameScene gameScene(sceneShader);
// Load MODELS
Penguin penguin(materialShader, "/Users/ibrahimradwan/Desktop/penguin.dae", camera);
penguin.setPenPosition(0, penguin.constantPenY, -3.3f); // Set initial posisiton
Car car(textureShader, "/Users/ibrahimradwan/Desktop/Small_car_obj/Small car.obj");
Car truck(textureShader, "/Users/ibrahimradwan/Desktop/cubus_deutz_rund/tlf16_rund.obj");
Coin coin(textureShader ,"/Users/ibrahimradwan/Desktop/coin/Gems/diamond_orange.obj");
Tree tree(textureShader, "/Users/ibrahimradwan/Desktop/Tree/tree_4.obj");
// Generate lanes
Utilities::generateLanesAlgorithm(lanesArray);
// Some variables for game logic go here::
GLuint frameCount = 0;
// Loading fonts
Fonts fonts(fontShader, Constants::gameWidth, Constants::gameHeight, "/Users/ibrahimradwan/Desktop/zorque.ttf");
// Load the Lane-cube into the fragment
GLuint VBO, VAO;
graphicsUtilities.bindCube(VBO, VAO);
GLuint VBOSafeLane, VAOSafeLane;
graphicsUtilities.bindCube(VBOSafeLane, VAOSafeLane, true);
//DepthMap initialization
GLuint depthMapFBO;
//depth texture
GLuint depthMap;
//DeapthMap Buffer Bindings
graphicsUtilities.DepthMapInitialization(depthMapFBO, depthMap, Constants::SHADOW_WIDTH, Constants::SHADOW_HEIGHT);
//depthMapInitializtion(depthMap, depthMapFBO,SHADOW_WIDTH,SHADOW_HEIGHT);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
//play Background Music
engine->play2D("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Sounds/CarHornBase.wav", true);
engine->play2D("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Sounds/180156__klankbeeld__traffic-horns-city-nervous-busy.wav", true);
engine->play2D("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Sounds/MagicMatch_GameTheme.ogg", true);
/*****************
TESTING
*****************/
currentbfsNode = new bfsNode();//initial point
currentbfsNode->laneZIndex = 2;
currentbfsNode->nodeX = 9;
targetPenX = penguin.getPenX();
testPen = &penguin;
/*****************
END TESTING
*****************/
while(!glfwWindowShouldClose(window) && !exitGame)
{
glfwPollEvents(); // Check for keyboard or mouse events
// Calculate frame time for smooth/quick movement on all devices
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
//Render depth of scene to texture (from light's perspective)
//Get light projection view matrix
glm::mat4 lightProjection, lightView;
glm::mat4 lightSpaceMatrix;
GLfloat near_plane = -1.0f, far_plane = 60.0f;
//Penguin Movement Attributes
bool movingForward = false, movingBackward = false, movingRight = false, movingLeft = false;
/**********
TESTING
**********/
if(!(penguin.movingLeft || penguin.movingRight || penguin.movingForwad || penguin.movingBackward|| penguin.isMoving)){ // Penguin isn't moving
if(abs(targetPenX- penguin.getPenX())>0.15){
if(penmove == 3){
movingRight = true;
} else if(penmove == 4){
movingLeft = true;
}
}
if(abs(targetPenX- penguin.getPenX())<0.15){
bfsNode* finalNode = test(currentbfsNode);
while(finalNode != 0){
if(finalNode->move){
if(finalNode->parent->move == 0){
currentbfsNode = finalNode;
penmove = finalNode->move;
}
}
finalNode = finalNode->parent;
}
currentbfsNode->parent = 0;
currentbfsNode->next = 0;
currentbfsNode->move = 0;
}
if(abs(targetPenX- penguin.getPenX())<0.15){
if(penmove == 1){
movingForward = true;
} else if(penmove == 2){
movingBackward = true;
} else if(penmove == 3){
movingRight = true;
targetPenX = penguin.getPenX() + 1;
} else if(penmove == 4){
movingLeft = true;
targetPenX = penguin.getPenX() - 1;
}
}
}
/**************************
TEST END
*************************/
//Call Render Everything functuion
graphicsUtilities.renderEverything(simpleDepthShader, lightProjection, lightView, lightSpaceMatrix, near_plane, far_plane, gameScene, penguin, movingForward, movingBackward, movingRight, movingLeft, lanesArray, frameCount, deltaTime, depthMapFBO, depthMap, graphicsUtilities, car, truck, coin, tree, VAO, VAOSafeLane, glm::vec3(lightX, lightY, lightZ), lookAtX, lookAtY, lookAtZ, camera);
//Update the Light Pos And the Light Look at
lightZ = penguin.getPenZ() + 8.3;
lookAtZ = penguin.getPenZ() + 36.3;
// Clear screen with certain background color
glViewport(0, 0, Constants::gameWidth*2, Constants::gameHeight*2);
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
// Clear depth and color buffers with each iteration so the new values can be applied without blocking
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Sart drawing
// Penguin drawing
graphicsUtilities.do_movement(deltaTime, movingForward, movingBackward, movingRight, movingLeft);
penguin.move(movingForward, movingBackward, movingRight, movingLeft,camera, deltaTime, lanesArray);
// penguin drawing
penguin.draw(camera.GetViewMatrix(), glm::radians(camera.Zoom), (float)Constants::gameHeight/Constants::gameWidth, 0.1f, 1000.0f, frameCount, (movingForward || movingRight || movingLeft || movingBackward), deltaTime, lanesArray);
// Draw the scene (lanes + cars + Diamonds + trees)
gameScene.draw(lightSpaceMatrix, depthMap, camera, glm::vec3(lightX, lightY, lightZ), shadows, camera.GetViewMatrix(), glm::radians(camera.Zoom), (float) Constants::gameHeight/(float)Constants::gameWidth, 0.1f, 1000.0f, VAO, VAOSafeLane, lanesArray, car, truck, coin, tree);
// Check if lanes generation needed
<<<<<<< HEAD
if(penguin.getCurrentLane() > 33){
cout << " nEXT is gENERATION"<<endl;
}
if (penguin.getCurrentLane() > 34){
||||||| merged common ancestors
if (penguin.getPenZ() < lanesArray[34].laneZPos){
=======
if (penguin.getCurrentLane() > 34){
>>>>>>> autorun
Utilities::addMoreLanes(lanesArray);
<<<<<<< HEAD
penguin.setCurrentLaneIndex(3);
penguin.setAdjacentLaneIndex(4);
||||||| merged common ancestors
penguin.setCurrentLaneIndex(2);
penguin.setAdjacentLaneIndex(3);
=======
penguin.setCurrentLaneIndex(3);
penguin.setAdjacentLaneIndex(4);
/******************
TESTING
*****************/
currentbfsNode->laneZIndex = 3;
>>>>>>> autorun
}
// Check forx collisions
collisionState=penguin.detectCollision(lanesArray);
if (collisionState == carCollision) {
gameOver = true;
}
frameCount++;
if(frameCount >= penguin.getPenguinMode()->getModelAnimations()[0].second.size()){
frameCount = 0;
}
graphicsUtilities.score(lanesArray,score, &penguin, engine);
// Display score
fonts.RenderText("Score: " + to_string(score), 25.0f, Constants::gameHeight - 60.0f, 0.8f, glm::vec3(1, 1, 0));
cout << "CURRENT LANE CAR LOCATION AND SPEED " << penguin.getCurrentLane() << " " << lanesArray[penguin.getCurrentLane()].getLaneCarXPosition() << " AND POST "<< lanesArray[penguin.getCurrentLane()].getLaneCarSpeed() <<endl;
if(gameOver) {
cout << "DEAD ON " << penguin.getCurrentLane() << " AND X " << penguin.getPenX()<<endl;
// Print lossing msg
fonts.RenderText("You lost!", Constants::gameWidth/2.0f - 80, Constants::gameHeight/ 2.0f, 0.8f, glm::vec3(1, 1, 0));
// Click enter to restart game
glfwSwapBuffers(window);
// Play gameover
if(!gameOverSoundPlayed){
engine->stopAllSounds();
engine->play2D("/Users/ibrahimradwan/Desktop/Graphics/CrossyRoad/opengl/opengl/Sounds/GameOver.wav");
gameOverSoundPlayed = true;
}
do {
glfwPollEvents();
} while((glfwGetKey(window, GLFW_KEY_ESCAPE)) != GLFW_PRESS);
exitGame = true;
}
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}