-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
1052 lines (870 loc) · 32.4 KB
/
Game.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Game.h"
//////////
//CONSTRUCTORS
//////////
Game::Game()
{
//Init glfw for widow handling
if (!glfwInit())
exit(EXIT_FAILURE);
//Set the default game state
GAME_STATE = STATE_MAINMENU;
world = new Node;
sceneGraph = new Node;
world->AttachNode(sceneGraph);
//Once initialized the game is now running
isRunning = true;
gameOver = false;
hasBeenInit = false;
//load sounds
soundSystem.loadSound("Resources/sound_assets.txt");
}
Game::~Game()
{
}
//////////
//INITIALISERS
//////////
//Initializes for gameplay
void Game::initGameplay(void)
{
GAME_STATE = STATE_GAMEPLAY;
projectionMatrix = glm::perspective(90.0f, (float)1280 / (float)720, 0.1f, 750.0f);
particleManager.getEmitterList().clear();
//Create the full screen quad
fullScreenQuad = ShapeFullScreenQuad();
diffuseProgram = new GLSLProgram;
int result = 1;
GLSLShader diffuseShader_V, diffuseShader_G, diffuseShader_F;
result *= diffuseShader_V.CreateShaderFromFile(GLSL_VERTEX_SHADER, "Resources/Shaders/pass_v.glsl");
result *= diffuseShader_G.CreateShaderFromFile(GLSL_GEOMETRY_SHADER,"Resources/Shaders/pass_g.glsl");
result *= diffuseShader_F.CreateShaderFromFile(GLSL_FRAGMENT_SHADER,"Resources/Shaders/Diffuse_f.glsl");
result *= diffuseProgram->AttachShader(&diffuseShader_V);
result *= diffuseProgram->AttachShader(&diffuseShader_G);
result *= diffuseProgram->AttachShader(&diffuseShader_F);
result *= diffuseProgram->LinkProgram();
result *= diffuseProgram->ValidateProgram();
diffuseShader_F.Release();
diffuseShader_G.Release();
diffuseShader_V.Release();
//get uniform variables
uniform_MVP = diffuseProgram->GetUniformLocation("MVP");
uniform_texture = diffuseProgram->GetUniformLocation("objectTexture");
uniform_normalMap = diffuseProgram->GetUniformLocation("objectNormalMap");
lightProgram = new GLSLProgram;
result = 1;
GLSLShader lightShader_V, lightShader_G, lightShader_F;
result *= lightShader_V.CreateShaderFromFile(GLSL_VERTEX_SHADER, "Resources/Shaders/light_v.glsl");
result *= lightShader_G.CreateShaderFromFile(GLSL_GEOMETRY_SHADER, "Resources/Shaders/light_g.glsl");
result *= lightShader_F.CreateShaderFromFile(GLSL_FRAGMENT_SHADER, "Resources/Shaders/light_f.glsl");
result *= lightProgram->AttachShader(&lightShader_V);
result *= lightProgram->AttachShader(&lightShader_G);
result *= lightProgram->AttachShader(&lightShader_F);
result *= lightProgram->LinkProgram();
result *= lightProgram->ValidateProgram();
lightShader_F.Release();
lightShader_G.Release();
lightShader_V.Release();
//get uniform variables
uniform_light_MVP = lightProgram->GetUniformLocation("MVP");
uniform_light_inverse_P = lightProgram->GetUniformLocation("inverseP");
uniform_light_qMap = lightProgram->GetUniformLocation("qMap");
meshSkinProgram = new GLSLProgram;
result = 1;
GLSLShader meshSkinShader_V, meshSkinShader_G, meshSkinShader_F;
result *= meshSkinShader_V.CreateShaderFromFile(GLSL_VERTEX_SHADER, "Resources/Shaders/MeshSkinning_v.glsl");
result *= meshSkinShader_G.CreateShaderFromFile(GLSL_GEOMETRY_SHADER, "Resources/Shaders/pass_g.glsl");
result *= meshSkinShader_F.CreateShaderFromFile(GLSL_FRAGMENT_SHADER, "Resources/Shaders/Diffuse_f.glsl");
result *= meshSkinProgram->AttachShader(&meshSkinShader_V);
result *= meshSkinProgram->AttachShader(&meshSkinShader_G);
result *= meshSkinProgram->AttachShader(&meshSkinShader_F);
result *= meshSkinProgram->LinkProgram();
result *= meshSkinProgram->ValidateProgram();
meshSkinShader_F.Release();
meshSkinShader_G.Release();
meshSkinShader_V.Release();
uniform_meshSkin_MVP = meshSkinProgram->GetUniformLocation("MVP");
uniform_meshSkin_texture = meshSkinProgram->GetUniformLocation("objectTexture");
uniform_meshSkin_normalMap = meshSkinProgram->GetUniformLocation("objectNormalMap");
uniform_meshSkin_boneMat = meshSkinProgram->GetUniformLocation("boneMatricies");
//Set up the HUD
{
HUD = new GameObject;
//Set up the pass though shader
HUDProgram = new GLSLProgram;
int result = 1;
GLSLShader HUDShader_V,HUDShader_G, HUDShader_F;
result *= HUDShader_V.CreateShaderFromFile(GLSL_VERTEX_SHADER, "Resources/Shaders/pass_v.glsl");
result *= HUDShader_G.CreateShaderFromFile(GLSL_GEOMETRY_SHADER,"Resources/Shaders/pass_g.glsl");
result *= HUDShader_F.CreateShaderFromFile(GLSL_FRAGMENT_SHADER,"Resources/Shaders/HUD_f.glsl");
result *= HUDProgram->AttachShader(&HUDShader_V);
result *= HUDProgram->AttachShader(&HUDShader_G);
result *= HUDProgram->AttachShader(&HUDShader_F);
result *= HUDProgram->LinkProgram();
result *= HUDProgram->ValidateProgram();
HUDShader_F.Release();
HUDShader_G.Release();
HUDShader_V.Release();
//get uniform variables
uniform_HUD_MVP = HUDProgram->GetUniformLocation("MVP");
uniform_HUD_texture = HUDProgram->GetUniformLocation("objectTexture");
uniform_HUD_FaceDirection = HUDProgram->GetUniformLocation("flipDirection");
//Load the HUD texture
HUDBackgroundHandle = loadTexture("Resources/Textures/HUDback.png");
HUDBarRedHandle = loadTexture("Resources/Textures/RedBarSampler.png");
HUDBarBlueHandle = loadTexture("Resources/Textures/RedBarSampler.png");
HUD->AttachModel(ShapeHUDQuad(1.0f,0.1f));
HUD->GetNode()->TranslateNode(glm::vec3(0,0.9f,0));
sceneGraph->AttachNode(HUD->GetNode());
for (unsigned int i = 0; i < 4; ++i)
{
HUDBars[i] = new GameObject;
if (i == 0 || i == 1)
HUDBars[i]->AttachModel(ShapeHUDQuad(0.7f, 0.02f, 1.0f, 1));
else
HUDBars[i]->AttachModel(ShapeHUDQuad(0.7f, 0.02f, 1.0f, -1));
HUD->GetNode()->AttachNode(HUDBars[i]->GetNode());
//Translate the HUD bars around the screen
//TODO: Need to find a better way of doing this
float horizontal = 0.15f;
float vertical = 0.046f;
if (i == 0)
HUDBars[i]->GetNode()->TranslateNode(glm::vec3(-horizontal, vertical, 0));
else if (i == 1)
HUDBars[i]->GetNode()->TranslateNode(glm::vec3(-horizontal, -vertical, 0));
else if (i == 2)
HUDBars[i]->GetNode()->TranslateNode(glm::vec3(horizontal, vertical, 0));
else if (i == 3)
HUDBars[i]->GetNode()->TranslateNode(glm::vec3(horizontal, -vertical, 0));
}
}
//Create the game object for the main camera
mainCamera = new Camera;
mainCamera->SetPosition(glm::vec3(0.0f, 0.0f, 5.0f));
sceneGraph->AttachNode(mainCamera->GetNode());
//Create a game object for player1
player1 = new Player;
//OBJModel player1Model = OBJModel("Resources/Models/Robot.obj", true);
OBJModel player1Model = OBJModel("Resources/Animations/RunAnimation/run1.obj", true);
player1->AttachModel(player1Model.GetVBO());
player1->AttachBones("Resources/Bones/skin.weightMap", player1Model.GetTexcoords());
player1->AttachTexture(loadTexture("Resources/Textures/Shputnik_Texture_red.png"));
player1->SetPosition(glm::vec3(-17, 0, -15));
player1->GetNode()->SetRotation(glm::vec3(90,0,0));
sceneGraph->AttachNode(player1->GetNode());
sceneGraph->Update();
player1->bvhTest();
player1->GetMorphTargets()->LoadAnimations();
//Create a game object for player2
player2 = new Player;
OBJModel player2Model = OBJModel("Resources/Animations/RunAnimation/run1.obj", true);
player2->AttachModel(player2Model.GetVBO());
player2->AttachBones("Resources/Bones/skin.weightMap", player2Model.GetTexcoords());
player2->AttachTexture(loadTexture("Resources/Textures/Shputnik_Texture_blue.png"));
player2->AttachNormalMap(loadTexture("Resources/NormalMaps/testMap.jpg"));
player2->SetPosition(glm::vec3(17, 0, -15));
player2->GetNode()->SetRotation(glm::vec3(-90, 0, 0));
sceneGraph->AttachNode(player2->GetNode());
sceneGraph->Update();
player2->bvhTest();
player2->GetMorphTargets()->LoadAnimations();
//Load the background objects into a asset list
frontWorldAssetsObjects.Load("Resources/frontWorldAssets.txt");
frontWorldAssetsObjects.AttachAllObjectsToNode(sceneGraph);
backWorldAssetsObjects.Load("Resources/backWorldAssets.txt");
backWorldAssetsObjects.AttachAllObjectsToNode(sceneGraph);
//Setting up the outline shader for use with toon shading
OutlineProgram = new GLSLProgram;
result = 1;
GLSLShader outlineShader_V, outlineShader_G, outlineShader_F;
result *= outlineShader_V.CreateShaderFromFile(GLSL_VERTEX_SHADER, "Resources/Shaders/pass_v.glsl");
result *= outlineShader_G.CreateShaderFromFile(GLSL_GEOMETRY_SHADER,"Resources/Shaders/pass_g.glsl");
result *= outlineShader_F.CreateShaderFromFile(GLSL_FRAGMENT_SHADER,"Resources/Shaders/outline_f.glsl");
result *= OutlineProgram->AttachShader(&outlineShader_V);
result *= OutlineProgram->AttachShader(&outlineShader_F);
result *= OutlineProgram->LinkProgram();
result *= OutlineProgram->ValidateProgram();
outlineShader_F.Release();
outlineShader_G.Release();
outlineShader_V.Release();
uniform_outline_MVP = OutlineProgram->GetUniformLocation("MVP");
uniform_outline_scene = OutlineProgram->GetUniformLocation("scene");
//set up particle shaders
ParticleProgram = new GLSLProgram;
result = 1;
GLSLShader particleShader_V, particleShader_G, particleShader_F;
result *= particleShader_V.CreateShaderFromFile(GLSL_VERTEX_SHADER, "Resources/Shaders/particle_v.glsl");
result *= particleShader_G.CreateShaderFromFile(GLSL_GEOMETRY_SHADER, "Resources/Shaders/particle_g.glsl");
result *= particleShader_F.CreateShaderFromFile(GLSL_FRAGMENT_SHADER, "Resources/Shaders/particle_f.glsl");
result *= ParticleProgram->AttachShader(&particleShader_V);
result *= ParticleProgram->AttachShader(&particleShader_G);
result *= ParticleProgram->AttachShader(&particleShader_F);
result *= ParticleProgram->LinkProgram();
result *= ParticleProgram->ValidateProgram();
particleShader_F.Release();
particleShader_G.Release();
particleShader_V.Release();
uniform_particle_MVP = ParticleProgram->GetUniformLocation("MVP");
uniform_particle_modelview = ParticleProgram->GetUniformLocation("modelview");
uniform_particle_projection = ParticleProgram->GetUniformLocation("projection");
uniform_particle_distort = ParticleProgram->GetUniformLocation("distortion");
uniform_particle_squaresize = ParticleProgram->GetUniformLocation("squareSize");
uniform_particle_texture = ParticleProgram->GetUniformLocation("objectTexture");
squareSize = new float;
*squareSize = 5.f;
//Set up the first pass Frame buffer
firstPass = new FrameBuffer;
firstPass->Initialize(windowWidth, windowHeight, 3, true, false);
lightPass = new FrameBuffer;
lightPass->Initialize(windowWidth, windowHeight, 2, false, false);
finalPass = new FrameBuffer;
finalPass->Initialize(windowWidth, windowHeight, 2, true, false);
mainLight = new Light;
mainLight->SetColour(glm::vec3(1, 1, 1));
mainLight->AttachModel(OBJModel("Resources/Models/Ball.obj", false).GetVBO());
mainLight->SetPosition(glm::vec3(0.0f, 1.0f, 1.0f));
mainLight->GetNode()->SetScale(glm::vec3(0.1f, 0.1f, 0.1f));
mainLight->GetNode()->SetScale(glm::vec3(5.0f, 5.0f, 5.0f));
sceneGraph->AttachNode(mainLight->GetNode());
qMap_handle = loadTexture("Resources/Textures/qMap.png");
soundSystem.playSound(0, BGM_FIGHT_CHANNEL);
particleManager.addEmitter(player1->GetNode(), SMOKE);
particleManager.addEmitter(player1->GetNode(), SPARK);
particleManager.addEmitter(player2->GetNode(), SMOKE);
particleManager.addEmitter(player2->GetNode(), SPARK);
particleManager.addEmitter(player1->GetNode(), SHIELD);
particleManager.addEmitter(player2->GetNode(), SHIELD);
particleManager.addEmitter(player1->GetNode(), IMPACT);
particleManager.addEmitter(player2->GetNode(), IMPACT);
particleManager.addEmitter(player1->GetNode(), SHADOW);
particleManager.addEmitter(player2->GetNode(), SHADOW);
particleManager.addEmitter(player1->GetNode(), BOOM);
particleManager.addEmitter(player2->GetNode(), BOOM);
particleManager.getEmitterList()[8]->ActivateEmitter();
particleManager.getEmitterList()[9]->ActivateEmitter();
blastActive1 = 0;
blastActive2 = 0;
redStartPlayed = false;
blueStartPlayed = false;
soundCounter = 0;
gameOver = false;
timeAfterGameOver = 0.0f;
sceneGraph->Update();
hasBeenInit = true;
}
void Game::resetGameplay(void)
{
//reset the players
player1->SetHealth(player1->GetMaxHP());
player1->SetPosition(glm::vec3(0, 0, 0));
player2->SetHealth(player2->GetMaxHP());
player2->SetPosition(glm::vec3(0, 0, 0));
gameOver = false;
timeAfterGameOver = 0.0f;
particleManager.getEmitterList()[0]->DeactivateEmitter();
particleManager.getEmitterList()[1]->DeactivateEmitter();
particleManager.getEmitterList()[2]->DeactivateEmitter();
particleManager.getEmitterList()[3]->DeactivateEmitter();
}
//Initializes for menu
void Game::initMainMenu(void)
{
GAME_STATE = STATE_MAINMENU;
gameMenu = new Menu;
gameMenu->AttachBackground("Resources/Textures/MainMenu.png");
gameMenu->AttachWindow(gameWindow);
}
//Open a glfw window with defined size
void Game::OpenWindow(int width, int height)
{
//Set the size of the window to be stored.
windowWidth = width;
windowHeight = height;
////Create window
//int count;
//GLFWmonitor** monitors = glfwGetMonitors(&count);
//if (count > 1)
// gameWindow = glfwCreateWindow(width, height, "Robopocalypse", monitors[1], NULL);
//else
// gameWindow = glfwCreateWindow(width, height, "Robopocalypse", monitors[0], NULL);
gameWindow = glfwCreateWindow(width, height, "Robopocalypse", NULL, NULL);
//If window didnt open
if (!gameWindow)
{
glfwTerminate();
std::cout << "Error opening GLFW window\n";
}
//Set the window as the current one
glfwMakeContextCurrent(gameWindow);
}
//////////
//SETTERS
//////////
//////////
//GETTERS
//////////
//////////
//OTHERS
//////////
//Update the game
void Game::Update(void)
{
glfwPollEvents();
if (!gameOver)
{
//If the window should close, user presses the exit button, the game is no longer running and shutsdown at the start of gameloop
if (glfwWindowShouldClose(gameWindow))
isRunning = false;
playerInput();
player1->update(player2, pl1SFX);
player2->update(player1, pl2SFX);
static float val = 0.0f;
static bool way = true;
if (way)
val += 0.30f;
else
val -= 0.01f;
static float val2 = 0.0f;
val2 += 0.3f;
player1->GetMorphTargets()->Update(&val);
player2->GetMorphTargets()->Update(&val2);
player1->GetModel()->AddVerticies(player1->GetMorphTargets()->GetFinalVerts());
player2->GetModel()->AddVerticies(player2->GetMorphTargets()->GetFinalVerts());
soundSystem.setChannelPos(SFX_PLAYER1_CHANNEL, player1->getPos());
soundSystem.setChannelPos(SFX_PLAYER2_CHANNEL, player2->getPos());
if (player1->GetHP() < player1->GetMaxHP()*0.5f){
particleManager.getEmitterList()[0]->ActivateEmitter();
particleManager.getEmitterList()[1]->ActivateEmitter();
}
if (player2->GetHP() < player2->GetMaxHP()*0.5f){
particleManager.getEmitterList()[2]->ActivateEmitter();
particleManager.getEmitterList()[3]->ActivateEmitter();
}
++soundCounter;
if (!redStartPlayed){
pl1SFX = START_DIALOGUE1;
redStartPlayed = true;
}
else if (!blueStartPlayed && soundCounter == 180){
pl2SFX = START_DIALOGUE2;
blueStartPlayed = true;
}
if (pl1SFX != EMPTY_P_SFX)
soundSystem.playSound(pl1SFX, SFX_PLAYER1_CHANNEL);
if (pl2SFX != EMPTY_P_SFX)
soundSystem.playSound(pl2SFX, SFX_PLAYER2_CHANNEL);
mainCamera->SetTarget(glm::vec3((player1->getPos().x + player2->getPos().x) / 2.f, 0, 0));
mainCamera->SetPosition(glm::vec3((player1->getPos().x + player2->getPos().x) / 2.f, 0, 5));
HUDBars[0]->AttachModel(ShapeHUDQuad(0.7f, 0.02f, (float)player1->GetHP() / (float)player1->GetMaxHP(), 1));
HUDBars[1]->AttachModel(ShapeHUDQuad(0.7f, 0.02f, (float)player1->GetSP() / (float)player1->GetMaxSP(), 1));
HUDBars[2]->AttachModel(ShapeHUDQuad(0.7f, 0.02f, (float)player2->GetHP() / (float)player2->GetMaxHP(), -1));
HUDBars[3]->AttachModel(ShapeHUDQuad(0.7f, 0.02f, (float)player2->GetSP() / (float)player2->GetMaxSP(), -1));
soundSystem.updateSound();
if (player1->getCurrentAction() == BLOCK){
particleManager.getEmitterList()[4]->ActivateEmitter();
}
else {
particleManager.getEmitterList()[4]->DeactivateEmitter();
}
if (player2->getCurrentAction() == BLOCK){
particleManager.getEmitterList()[5]->ActivateEmitter();
}
else {
particleManager.getEmitterList()[5]->DeactivateEmitter();
}
if (player1->getHit() != 0 && player1->getHit() < 2){
particleManager.getEmitterList()[6]->ActivateEmitter();
}
else {
particleManager.getEmitterList()[6]->DeactivateEmitter();
}
if (player2->getHit() != 0 && player2->getHit() < 2){
particleManager.getEmitterList()[7]->ActivateEmitter();
}
else {
particleManager.getEmitterList()[7]->DeactivateEmitter();
}
if (player1->getCurrentAction() == BLAST){
particleManager.getEmitterList()[10]->ActivateEmitter();
++blastActive1;
}
else {
particleManager.getEmitterList()[10]->DeactivateEmitter();
blastActive1 = 0;
}
if (player2->getCurrentAction() == BLAST){
particleManager.getEmitterList()[11]->ActivateEmitter();
++blastActive2;
}
else {
particleManager.getEmitterList()[11]->DeactivateEmitter();
blastActive2 = 0;
}
sceneGraph->Update();
particleManager.update(player1, player2);
if (player1->GetHP() <= 0.0f)
{
gameOver = 2;
timeAfterGameOver = 0.0f;
soundSystem.playSound(END_DIALOGUE2, SFX_PLAYER1_CHANNEL);
}
if (player2->GetHP() <= 0.0f)
{
gameOver = 1;
timeAfterGameOver = 0.0f;
soundSystem.playSound(END_DIALOGUE1, SFX_PLAYER1_CHANNEL);
}
}
else
{
if (timeAfterGameOver > 10.0f)
{
this->GAME_STATE = STATE_MAINMENU;
//initMainMenu();
}
timeAfterGameOver += 0.1f;
}
}
void Game::playerInput(void){
//joystick player 1
if (glfwJoystickPresent(0) == GL_TRUE){
//initialize player 1 inputs
int * joySize;
joySize = new int;
const float* joystickPointer = glfwGetJoystickAxes(0, joySize);
int * buttonSize;
buttonSize = new int;
const unsigned char* buttonPointer = glfwGetJoystickButtons(0, buttonSize);
if ((*joystickPointer <= -0.25f) || (*(buttonPointer + 13) == 1)){
player1->controllerInput(MOVE_LEFT);
}
else if ((*joystickPointer >= 0.25f) || (*(buttonPointer + 11) == 1)){
player1->controllerInput(MOVE_RIGHT);
}
if ((*(joystickPointer + 1) <= -0.50f) || (*(buttonPointer + 10) == 1)){
player1->controllerInput(BLOCK);
}
if (*(buttonPointer + 4) == 1){
player1->controllerInput(DASH_LEFT);
}
else if (*(buttonPointer + 5) == 1){
player1->controllerInput(DASH_RIGHT);
}
else if ((*(joystickPointer + 1) >= 0.90f) || (*(buttonPointer + 12) == 1)){
player1->controllerInput(JUMP);
}
else if (*(buttonPointer + 2) == 1){
player1->controllerInput(PUNCH);
}
else if (*buttonPointer == 1){
player1->controllerInput(KICK);
}
else if (*(buttonPointer + 3) == 1){
//player1->controllerInput(LASER);
}
else if (*(buttonPointer + 1) == 1){
player1->controllerInput(BLAST);
}
free(joySize);
free(buttonSize);
}
else
{
//player 1 input
if (glfwGetKey(gameWindow, 'S') == GLFW_PRESS){
player1->controllerInput(MOVE_LEFT);
}
else if (glfwGetKey(gameWindow, 'F') == GLFW_PRESS){
player1->controllerInput(MOVE_RIGHT);
}
else if (glfwGetKey(gameWindow, 'E') == GLFW_PRESS){
player1->controllerInput(JUMP);
}
else if (glfwGetKey(gameWindow, 'D') == GLFW_PRESS){
player1->controllerInput(BLOCK);
}
else if (glfwGetKey(gameWindow, 'W') == GLFW_PRESS){
player1->controllerInput(DASH_LEFT);
}
else if (glfwGetKey(gameWindow, 'R') == GLFW_PRESS){
player1->controllerInput(DASH_RIGHT);
}
else if (glfwGetKey(gameWindow, 'Q') == GLFW_PRESS){
player1->controllerInput(PUNCH);
}
else if (glfwGetKey(gameWindow, 'A') == GLFW_PRESS){
player1->controllerInput(KICK);
}
else if (glfwGetKey(gameWindow, 'T') == GLFW_PRESS){
//player1->controllerInput(LASER);
}
else if (glfwGetKey(gameWindow, 'G') == GLFW_PRESS){
player1->controllerInput(BLAST);
}
}
if (glfwJoystickPresent(1) == GL_TRUE){
//initialize player 2 inputs
int * joySize;
joySize = new int;
const float* joystickPointer = glfwGetJoystickAxes(1, joySize);
int * buttonSize;
buttonSize = new int;
const unsigned char* buttonPointer = glfwGetJoystickButtons(1, buttonSize);
//joystick player 2
if ((*joystickPointer <= -0.25f) || (*(buttonPointer + 13) == 1)){
player2->controllerInput(MOVE_LEFT);
}
else if ((*joystickPointer >= 0.25f) || (*(buttonPointer + 11) == 1)){
player2->controllerInput(MOVE_RIGHT);
}
if ((*(joystickPointer + 1) <= -0.50f) || (*(buttonPointer + 10) == 1)){
player2->controllerInput(BLOCK);
}
if (*(buttonPointer + 4) == 1){
player2->controllerInput(DASH_LEFT);
}
else if (*(buttonPointer + 5) == 1){
player2->controllerInput(DASH_RIGHT);
}
else if ((*(joystickPointer + 1) >= 0.90f) || (*(buttonPointer + 12) == 1)){
player2->controllerInput(JUMP);
}
else if (*(buttonPointer + 2) == 1){
player2->controllerInput(PUNCH);
}
else if (*buttonPointer == 1){
player2->controllerInput(KICK);
}
else if (*(buttonPointer + 3) == 1){
//player2->controllerInput(LASER);
}
else if (*(buttonPointer + 1) == 1){
player2->controllerInput(BLAST);
}
free(joySize);
free(buttonSize);
}
else
{
//player 2 input
if (glfwGetKey(gameWindow, 'J') == GLFW_PRESS){
player2->controllerInput(MOVE_LEFT);
}
else if (glfwGetKey(gameWindow, 'L') == GLFW_PRESS){
player2->controllerInput(MOVE_RIGHT);
}
else if (glfwGetKey(gameWindow, 'I') == GLFW_PRESS){
player2->controllerInput(JUMP);
}
else if (glfwGetKey(gameWindow, 'K') == GLFW_PRESS){
player2->controllerInput(BLOCK);
}
else if (glfwGetKey(gameWindow, 'U') == GLFW_PRESS){
player2->controllerInput(DASH_LEFT);
}
else if (glfwGetKey(gameWindow, 'O') == GLFW_PRESS){
player2->controllerInput(DASH_RIGHT);
}
else if (glfwGetKey(gameWindow, 'Y') == GLFW_PRESS){
//player2->controllerInput(LASER);
}
else if (glfwGetKey(gameWindow, 'H') == GLFW_PRESS){
player2->controllerInput(BLAST);
}
else if (glfwGetKey(gameWindow, 'P') == GLFW_PRESS){
player2->controllerInput(PUNCH);
}
else if (glfwGetKey(gameWindow, ';') == GLFW_PRESS){
player2->controllerInput(KICK);
}
}
}
//Render the game
void Game::Render(void)
{
if (!gameOver)
{
firstPass->Activate();
//Clear the colour and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Update the view matrix
mainCamera->Update(viewMatrix);
//Activate the shader and render the player
diffuseProgram->Activate();
{
firstPass->SetTexture(0);
firstPass->SetTexture(1);
firstPass->SetTexture(2);
//Render all of the background objects
projectionMatrix = glm::perspective(90.0f, (float)1280 / (float)720, 0.1f, 1000.0f);
for (unsigned int i = 0; i < backWorldAssetsObjects.GetSize(); ++i)
{
//firstPass->SetTexture(0);
PreRender(backWorldAssetsObjects.GetObjectAtIndex(i));
}
//Render all of the foreground objects
projectionMatrix = glm::perspective(90.0f, (float)1280 / (float)720, 0.1f, 200.0f);
for (unsigned int i = 0; i < frontWorldAssetsObjects.GetSize(); ++i)
{
//firstPass->SetTexture(0);
PreRender(frontWorldAssetsObjects.GetObjectAtIndex(i));
}
projectionMatrix = glm::perspective(90.0f, (float)1280 / (float)720, 0.1f, 50.0f);
//Render the two player game objects
PreRender(player1);
PreRender(player2);
//PreRender(mainLight);
////This is for debugging only and will be removed later on.
//PreRender(player1->GetCollisionBoxes());
//PreRender(player2->GetCollisionBoxes());
}
diffuseProgram->Deactivate();
ParticleProgram->Activate();
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
PreRender(particleManager.getEmitterList());
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
} ParticleProgram->Deactivate();
firstPass->Deactivate();
lightPass->Activate();
lightPass->SetTexture(0);
lightProgram->Activate();
{
//Set textures for the FBO
firstPass->SetTexture(0);
firstPass->BindColour(0);
firstPass->SetTexture(1);
firstPass->BindColour(1);
firstPass->SetTexture(3);
firstPass->BindColour(3);
//pass in qMap
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, qMap_handle);
glUniform1i(uniform_light_qMap, 3);
PreRenderLight(mainLight);
//Unbind the FBO textures
firstPass->SetTexture(2);
firstPass->UnbindTextures();
firstPass->SetTexture(1);
firstPass->UnbindTextures();
firstPass->SetTexture(0);
firstPass->UnbindTextures();
}
lightProgram->Deactivate();
lightPass->Deactivate();
glDisable(GL_DEPTH_TEST);
//Enable new shader
OutlineProgram->Activate();
{
//Set textures for the FBO
firstPass->SetTexture(0);
firstPass->BindColour(0);
glUniformMatrix4fv(uniform_outline_MVP, 1, 0, glm::value_ptr(glm::mat4(1)));
fullScreenQuad->ActivateAndRender();
//Unbind the FBO textures
firstPass->SetTexture(0);
firstPass->UnbindTextures();
}
OutlineProgram->Deactivate();
//Render the HUD on top of everything else.
RenderHUD();
}
else
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
static GLuint winImageHandle;
if (gameOver == 1)
winImageHandle = ilutGLLoadImage("Resources/Textures/win1.jpg");
else
winImageHandle = ilutGLLoadImage("Resources/Textures/win2.jpg");
OutlineProgram->Activate();
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, winImageHandle);
glUniform1i(glGetUniformLocation(OutlineProgram->GetHandle(), "scene"), 0);
glUniformMatrix4fv(uniform_outline_MVP, 1, 0, glm::value_ptr(glm::mat4(1)));
fullScreenQuad->ActivateAndRender();
}OutlineProgram->Deactivate();
}
GLSLProgram::Deactivate();
FrameBuffer::Deactivate();
VertexBuffer::Deactivate();
//Swap front and back buffers
glfwSwapBuffers(gameWindow);
}
//Render the HUD of the game
void Game::RenderHUD(void)
{
HUDProgram->Activate();
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, HUDBackgroundHandle);
glUniform1i(uniform_HUD_texture, 0);
glUniformMatrix4fv(uniform_HUD_MVP, 1, 0, glm::value_ptr(HUD->GetNode()->GetWorldTransform()));
glUniform2fv(uniform_HUD_FaceDirection, 1, glm::value_ptr(glm::vec2(1.0f, 1.0f)));
HUD->Render();
for (unsigned int i = 0; i < 4; ++i)
{
glUniformMatrix4fv(uniform_HUD_MVP, 1, 0, glm::value_ptr(HUDBars[i]->GetNode()->GetWorldTransform()));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, HUDBarRedHandle);
glUniform1i(uniform_HUD_texture, 0);
if (i == 0 || i == 1)
glUniform2fv(uniform_HUD_FaceDirection, 1, glm::value_ptr(glm::vec2(1.0f, 1.0f)));
else
glUniform2fv(uniform_HUD_FaceDirection, 1, glm::value_ptr(glm::vec2(-1.0f, 1.0f)));
HUDBars[i]->Render();
}
}
HUDProgram->Deactivate();
}
void Game::PreRender(GameObject* object)
{
//if the object has bones that need to be transformed
//if (object->GetModel()->UsingBones())
//{
// //object->animations.Update(object->GetNode());
// glm::mat4 skinningOutputList[MAX_BONE_SIZE];
//
// for (unsigned int i = 0; i < MAX_BONE_SIZE; ++i)
// {
// skinningOutputList[i] = object->animations.GetBoneTransformations()[i];
// }
//
// meshSkinProgram->Activate();
//
// glUniformMatrix4fv(uniform_meshSkin_boneMat, 64, 0, (float*)skinningOutputList);
//
// modelViewProjectionMatrix = object->UpdateModelViewProjection(projectionMatrix, viewMatrix);
// glUniformMatrix4fv(uniform_meshSkin_MVP, 1, 0, glm::value_ptr(modelViewProjectionMatrix));
// //glUniform3fv(uniform_LightPos, 1, glm::value_ptr(glm::inverse(object->GetNode()->GetWorldTransform()) * LIGHTPOS));
// //glUniform3fv(uniform_LightColour, 1, glm::value_ptr(glm::vec3(1, 1, 1)));
//
// //Pass in texture
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, object->GetTextureHandle());
// glUniform1i(uniform_meshSkin_texture, 0);
//
// //Pass in normal map
// glActiveTexture(GL_TEXTURE1);
// glBindTexture(GL_TEXTURE_2D, object->GetNormalMapHandle());
// glUniform1i(uniform_meshSkin_normalMap, 1);
//
// ////pass in qMap
// //glActiveTexture(GL_TEXTURE2);
// //glBindTexture(GL_TEXTURE_2D, qMap_handle);
// //glUniform1i(uniform_qMap, 2);
//
// object->Render();
//
// meshSkinProgram->Deactivate();
//}
//
//else
{
modelViewProjectionMatrix = object->UpdateModelViewProjection(projectionMatrix, viewMatrix);
glUniformMatrix4fv(uniform_MVP, 1, 0, glm::value_ptr(modelViewProjectionMatrix));
//glUniform3fv(uniform_LightPos, 1, glm::value_ptr(glm::inverse(object->GetNode()->GetWorldTransform()) * LIGHTPOS));
//glUniform3fv(uniform_LightColour, 1, glm::value_ptr(glm::vec3(1, 1, 1)));
//Pass in texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, object->GetTextureHandle());
glUniform1i(uniform_texture, 0);
//Pass in normal map
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, object->GetNormalMapHandle());
glUniform1i(uniform_normalMap, 1);
////pass in qMap
//glActiveTexture(GL_TEXTURE2);
//glBindTexture(GL_TEXTURE_2D, qMap_handle);
//glUniform1i(uniform_qMap, 2);
object->Render();
}
}
void Game::PreRenderLight(Light* object)
{
modelViewProjectionMatrix = object->UpdateModelViewProjection(projectionMatrix, viewMatrix);
glUniformMatrix4fv(uniform_light_MVP, 1, 0, glm::value_ptr(modelViewProjectionMatrix));
glUniformMatrix4fv(uniform_light_inverse_P, 1, 0, glm::value_ptr(glm::inverse(projectionMatrix)));
object->Render();
}
//void Game::PreRender(GameObject* object, Light* light)
//{
// modelViewProjectionMatrix = object->UpdateModelViewProjection(projectionMatrix, viewMatrix);
// glUniformMatrix4fv(uniform_MVP, 1, 0, glm::value_ptr(modelViewProjectionMatrix));
// glUniform3fv(uniform_LightPos, 1, glm::value_ptr(glm::inverse(object->GetNode()->GetWorldTransform()) * glm::vec4(light->GetNode()->GetWorldPosition(),1)));
// glUniform3fv(uniform_LightColour, 1, glm::value_ptr(light->GetColour()));
//
// PreRender(object);
//}
//This draws a vector of hitboxes, only used for debugging
void Game::PreRender(std::vector<CollisionBox> hitboxes)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
for (unsigned int i = 0; i < hitboxes.size(); ++i)
{
modelViewProjectionMatrix = projectionMatrix * viewMatrix * hitboxes[i].GetSceneGraphObject()->GetWorldTransform();
glUniformMatrix4fv(uniform_MVP, 1, 0, glm::value_ptr(modelViewProjectionMatrix));
hitboxes[i].GetVertexBuffer()->ActivateAndRender();
}
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
void Game::PreRender(std::vector<ParticleEmitter*> emitterList){
for (int i = 0, size = emitterList.size(); i<size; ++i){
ParticleType type = particleManager.getType()[i];
if (type == SMOKE){
*squareSize = 40.f;
}
else if (type == SPARK || type == IMPACT){
*squareSize = 25.f;
}
else if (type == SHIELD){
*squareSize = 150.f;
}
else if ((type == BOOM) && (emitterList[i]->GetNode()->GetParent() == player1->GetNode())){
*squareSize = 7.f * (float)blastActive1;
}
else if ((type == BOOM) && (emitterList[i]->GetNode()->GetParent() == player2->GetNode())){
*squareSize = 7.f * (float)blastActive2;
}
else if (type == SHADOW){