-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
710 lines (568 loc) · 21.5 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
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
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <array>
#define GLEW_STATIC
#include <GL/glew.h> // window management library
#include <GL/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> //
#include <SOIL/SOIL.h> // read image file
#include <chrono>
#include <thread>
#include "Shader.h"
#include "FirePartShader.h"
#include "Window.h"
#include "TowerObject.h"
#include "BulletObject.h"
#include "EnemyObject.h"
#include "Graph.h"
#include "Node.h"
#include "UIController.h"
#include "Renderable.h"
#include "FlameTower.h"
#include "SlowTower.h"
#include "MobileDefender.h"
// Macro for printing exceptions
#define PrintException(exception_object)\
std::cerr << exception_object.what() << std::endl
// Globals that define the OpenGL window and viewport
const std::string window_title_g = "Assignment 3";
const unsigned int window_width_g = 1000;
const unsigned int window_height_g = 600;
//const glm::vec3 viewport_background_color_g(0.0, 0.0, 0.0);
const glm::vec3 viewport_background_color_g(0.15, 0.17, 0.21);
// Global texture info
GLuint tex[9];
std::array<GLuint, 14> fakefont;
GLuint sprite_vbo;
GLuint sprite_ebo;
GLuint partic_vbo;
GLuint partic_ebo;
void drawParticles(GLuint particleprogram, int particlesize, TowerObject* t, float cameraZoom)
{
// Select proper shader program to use
glUseProgram(particleprogram);
//set displacement
int matrixLocation = glGetUniformLocation(particleprogram, "x");
int timeLocation = glGetUniformLocation(particleprogram, "time");
glm::mat4 rot = glm::mat4();
glm::mat4 world = glm::mat4();
float k = glfwGetTime();
//rot = glm::translate(rot, glm::vec3(0.0f, 0.0f, 0.0f));
glm::vec3 tPos = t->getPosition() * cameraZoom; // scale factor
tPos.x += 0.5f * cameraZoom * cos(glm::radians(t->getOrientation())); //move out from the tower a bit
tPos.y += 0.5f * cameraZoom * sin(glm::radians(t->getOrientation()));
rot = glm::translate(rot, tPos);
//rot = glm::rotate(rot, -k * 360 / 6.283f, glm::vec3(0, 0, 1));
rot = glm::rotate(rot, t->getOrientation() - 90.0f, glm::vec3(0, 0, 1));
rot = glm::scale(rot, glm::vec3(0.05, 0.05, 0.05));
// get ready to draw, load matrix
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, &rot[0][0]);
glUniform1f(timeLocation, k);
glBindTexture(GL_TEXTURE_2D, tex[4]);
// Draw
glDrawElements(GL_TRIANGLES, 6*particlesize, GL_UNSIGNED_INT, 0);
}
int CreateParticleArray(void) {
// Each particle is a square with four vertices and two triangles
// Number of attributes for vertices and faces
const int vertex_attr = 7; // 7 attributes per vertex: 2D (or 3D) position (2), direction (2), 2D texture coordinates (2), time (1)
// const int face_att = 3; // Vertex indices (3)
GLfloat vertex[] = {
// square (two triangles)
// Position Color Texcoords
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
};
GLfloat particleatt[1000 * vertex_attr];
float theta, r, tmod;
for (int i = 0; i < 1000; i++)
{
if (i % 4 == 0)
{
theta = (6.28*(rand() % 1000) / 1000.0f);//(2*(rand() % 10000) / 10000.0f -1.0f)*0.13f;
r = 0.1f + 0.25*(rand() % 10000) / 10000.0f;
tmod = (rand() % 10000) / 10000.0f;
}
// position
particleatt[i*vertex_attr + 0] = vertex[(i % 4) * 7 + 0];
particleatt[i*vertex_attr + 1] = vertex[(i % 4) * 7 + 1];
// velocity
particleatt[i*vertex_attr + 2] = sin(theta)*r;
particleatt[i*vertex_attr + 3] = abs(cos(theta)*r);
// phase
particleatt[i*vertex_attr + 4] = tmod;
// texture coordinate
particleatt[i*vertex_attr + 5] = vertex[(i % 4) * 7 + 5];
particleatt[i*vertex_attr + 6] = vertex[(i % 4) * 7 + 6];
}
GLuint face[] = {
0, 1, 2, // t1
2, 3, 0 //t2
};
GLuint manyface[1000 * 6];
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 6; j++)
manyface[i * 6 + j] = face[j] + i * 4;
}
GLuint vbo, ebo;
// Create buffer for vertices
glGenBuffers(1, &partic_vbo);
glBindBuffer(GL_ARRAY_BUFFER, partic_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(particleatt), particleatt, GL_STATIC_DRAW);
// Create buffer for faces (index buffer)
glGenBuffers(1, &partic_ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, partic_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(manyface), manyface, GL_STATIC_DRAW);
// Return number of elements in array buffer
return sizeof(manyface);
}
// Create the geometry for a square (with two triangles)
// Return the number of array elements that form the square
int CreateSquare(void) {
// The face of the square is defined by four vertices and two triangles
// Number of attributes for vertices and faces
// const int vertex_att = 7; // 7 attributes per vertex: 2D (or 3D) position (2), RGB color (3), 2D texture coordinates (2)
// const int face_att = 3; // Vertex indices (3)
GLfloat vertex[] = {
// square (two triangles)
// Position Color Texcoords
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
};
GLuint face[] = {
0, 1, 2, // t1
2, 3, 0 //t2
};
// Create buffer for vertices
glGenBuffers(1, &sprite_vbo);
glBindBuffer(GL_ARRAY_BUFFER, sprite_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
// Create buffer for faces (index buffer)
glGenBuffers(1, &sprite_ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(face), face, GL_STATIC_DRAW);
// Return number of elements in array buffer (6 in this case)
return sizeof(face) / sizeof(GLuint);
}
void setthisTexture(GLuint w, char *fname)
{
glBindTexture(GL_TEXTURE_2D, w);
int width, height;
unsigned char* image = SOIL_load_image(fname, &width, &height, 0, SOIL_LOAD_RGBA);
// Texture Wrapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Texture Filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
}
void setallTexture(void)
{
// tex = new GLuint[4];
glGenTextures(9, tex);
setthisTexture(tex[0], "Assets/defender.png");
setthisTexture(tex[1], "Assets/rock.png");
setthisTexture(tex[2], "Assets/laser2.png");
setthisTexture(tex[3], "Assets/saw.png");
setthisTexture(tex[4], "Assets/fire.png");
setthisTexture(tex[5], "Assets/sawFast.png");
setthisTexture(tex[6], "Assets/blueship.png");
setthisTexture(tex[7], "Assets/fireship.png");
setthisTexture(tex[8], "Assets/iceship.png");
glBindTexture(GL_TEXTURE_2D, tex[0]);
//fakefont
glGenTextures(fakefont.max_size(), fakefont.data());
for (int i = 0; i < fakefont.max_size(); ++i) {
std::string str = "Assets/fakefont/" + std::to_string(i) + ".png";
char* ch = &str[0];
setthisTexture(fakefont[i], ch);
}
glBindTexture(GL_TEXTURE_2D, fakefont[0]);
}
// random float between LO and HI
float getRand(float LO, float HI) {
return LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)));
}
// gives the given game object a path where the start is defined by the object's location and the end is either found by updating the graph on click, or set manually beforehand
void doPath(Graph &g, EnemyObject* obj, bool shouldUpdate) {
// get the node that the game object is on top of
int nodeId = 0;
if (obj->getCurrNode() != nullptr) {
nodeId = g.getNodeIdFromCoords(obj->getCurrNode()->getX(), -obj->getCurrNode()->getY());
}
// set the start of the path to this node
g.setStart(nodeId);
// set end through the graph's update method (clicks) if needed
if (shouldUpdate) {
g.update();
}
g.pathfind();
obj->setPath(g.getPath());
}
void doPath(Graph &g, MobileDefender* obj, bool shouldUpdate) {
// get the node that the game object is on top of
int nodeId = g.getNodeIdFromCoords(obj->getPosition().x, -obj->getPosition().y);
// set the start of the path to this node
g.setStart(nodeId);
// set end through the graph's update method (clicks) if needed
if (shouldUpdate) {
g.update();
}
g.pathfind();
obj->setPath(g.getPath());
}
// rand int between hi and lo
int getRand(int LO, int HI) {
return rand() % (HI - LO + 1);
}
// Main function that builds and runs the game
int main(void) {
try {
// Seed for generating random numbers with rand()
srand((unsigned int)time(0));
// Setup window
Window window(window_width_g, window_height_g, window_title_g);
// Set up z-buffer for rendering
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
// Enable Alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Create geometry of the square
int size = CreateSquare();
// Set up shaders
Shader spriteShader("Shader/shader.vert", "Shader/shader.frag");
CreateParticleArray();
FirePartShader* fireShader = new FirePartShader();
// Set up the textures
setallTexture();
// setup camera parameters outside now
float cameraZoom = 0.2f;
glm::vec3 cameraTranslatePos(0.f);
/*------------------ GAME SPECIFIC STUFF --------------------------*/
//Setup 40x30 graph
Graph g = Graph(40, 30, GameObject(glm::vec3(0.0f), tex[4], size));
// for mobile defender to be seen later
int p = getRand(0, 1199);
Node* pNode = g.getNode(p);
bool defender = false;
// for towers
std::vector<TowerObject*> towerObjects;
// enemy object info
std::vector<EnemyObject*> enemyObjects;
std::queue<EnemyObject*> spawnWaitingRoom;
// different spawn point & path for each enemy
Node* npcStart = g.getNode(0); // ^ not anymore
g.setEnd(1199);
glm::vec3 endNodeLoc(g.getNode(g.getEndId())->getX(), g.getNode(g.getEndId())->getY(), 0.f);
// enemies
EnemyObject* tempEnemy = nullptr;
for (int i = 0; i < 12; ++i) {
// different enemy types (hush yes they are)
if (i > 7) {
tempEnemy = new EnemyObject(glm::vec3(npcStart->getX(), npcStart->getY(), 0.0f), tex[5], size, 3.3f);
tempEnemy->setHP(200.f);
}
else if (i > 3) {
tempEnemy = new EnemyObject(glm::vec3(npcStart->getX(), npcStart->getY(), 0.0f), tex[1], size, 1.1f);
tempEnemy->setHP(1000.f);
}
else {
tempEnemy = new EnemyObject(glm::vec3(npcStart->getX(), npcStart->getY(), 0.0f), tex[3], size, 2.2f);
}
// first enemy can be spawned, the rest must wait
if (i == 0) {
doPath(g, tempEnemy, false);
enemyObjects.push_back(tempEnemy);
}
else {
tempEnemy->setPath(g.getPath());
spawnWaitingRoom.push(tempEnemy);
}
tempEnemy->setPosition(tempEnemy->getPosition() - glm::vec3(1.f, 0.f, 0.f)); //small offset
}
// uuuu iiiii
std::vector<GLuint> towerTextures;
for (int i = 6; i < 9; ++i) {
towerTextures.push_back(tex[i]); //textures for placeable towers are contiguous at end of regular texture array
}
UIController ui(fakefont.data(), size, towerTextures);
Renderable cursor(glm::vec3(20.0f, 20.0f, 0.0f), towerTextures[0], size);
cursor.setScale(glm::vec3(5.f, 4.f, 0.f));
// </ui>
// shopping control
bool shopping = false;
int towerChoice = -1;
double spawnTime = 1.25;
double timeSinceLastSpawn = 0;
// Run the main loop
double lastTime = glfwGetTime();
while (!glfwWindowShouldClose(window.getWindow())) {
// Clear background
window.clear(viewport_background_color_g);
glDepthMask(GL_TRUE);
// Calculate delta time
double currentTime = glfwGetTime();
double deltaTime = currentTime - lastTime;
lastTime = currentTime;
// spawning
if (spawnWaitingRoom.size() > 0) {
timeSinceLastSpawn += deltaTime;
if (timeSinceLastSpawn > spawnTime) {
enemyObjects.insert(enemyObjects.begin(), spawnWaitingRoom.front());
timeSinceLastSpawn = 0; // reset spawn timer
spawnWaitingRoom.pop(); // pop that bad boi
if (spawnWaitingRoom.size() == 8) spawnTime = 0.75;
if (spawnWaitingRoom.size() == 4) spawnTime = 2.5;
}
}
/*------------------------------ INPUT ------------------------------*/
// first spawn of mobile defender
if (glfwGetKey(window.getWindow(), GLFW_KEY_X) == GLFW_PRESS && !defender) {
towerObjects.push_back(new MobileDefender(glm::vec3(pNode->getX(), pNode->getY(), 0.0f), tex[0], size, tex[2]));
defender = true;
}
// camera controls
if (glfwGetKey(window.getWindow(), GLFW_KEY_UP) == GLFW_PRESS) {
cameraZoom += 1.0 * deltaTime;
}
if (glfwGetKey(window.getWindow(), GLFW_KEY_DOWN) == GLFW_PRESS) {
cameraZoom = std::max(0.01f, cameraZoom - 1.0f * (float)deltaTime);
}
if (glfwGetKey(window.getWindow(), GLFW_KEY_W) == GLFW_PRESS) {
cameraTranslatePos.y -= 3.0 * deltaTime;
}
if (glfwGetKey(window.getWindow(), GLFW_KEY_S) == GLFW_PRESS) {
cameraTranslatePos.y += 3.0 * deltaTime;
}
if (glfwGetKey(window.getWindow(), GLFW_KEY_A) == GLFW_PRESS) {
cameraTranslatePos.x += 3.0 * deltaTime;
}
if (glfwGetKey(window.getWindow(), GLFW_KEY_D) == GLFW_PRESS) {
cameraTranslatePos.x -= 3.0 * deltaTime;
}
// decide how to handle mouse interaction
if (shopping) { // maybe place a new tower, or cancel
double xpos, ypos;
glfwGetCursorPos(Window::getWindow(), &xpos, &ypos);
Node* node = nullptr;
glm::vec3 cursorPos;
// draw the tower cursor at the right spot
if ((xpos > 30 && xpos < 740 && ypos > 25 && ypos < 570)) {
int n = g.selectNode(xpos, ypos);
node = (n == -1) ? nullptr : g.getNode(n);
cursorPos = (node == nullptr) ? glm::vec3(20.f) : glm::vec3(node->getX(), node->getY(), 0.f);
cursor.setPos(cursorPos);
}
// if they click either place the tower or dont
if (glfwGetMouseButton(Window::getWindow(), GLFW_MOUSE_BUTTON_LEFT)) {
cursor.setPos(glm::vec3(20.f, 20.f, 0.f)); // the temp cursor can go away
if (node != nullptr && !node->isObstacle()) {
// choice of tower type
switch (towerChoice)
{
case 0:
towerObjects.push_back(new TowerObject(glm::vec3(node->getX(), node->getY(), 0.0f), towerTextures[0], size, tex[2]));
break;
case 1:
towerObjects.push_back(new FlameTower(glm::vec3(node->getX(), node->getY(), 0.0f), towerTextures[1], size, tex[2]));
break;
case 2:
towerObjects.push_back(new SlowTower(glm::vec3(node->getX(), node->getY(), 0.0f), towerTextures[2], size, tex[2]));
default:
break;
}
// if we need to pathfind or not
bool onPath = false;
for (Node* p : enemyObjects.back()->getPath()) {
if (p == node) {
onPath = true;
break;
}
}
if (onPath) {
bool trueOnlyOnce = true;
// gotta replan path since obstacle added
for (int i = enemyObjects.size() - 1; i > -1; --i) {
// the main event
doPath(g, enemyObjects[i], trueOnlyOnce);
trueOnlyOnce = false; // compiler pls
}
}
else {
// still set the node as an obstacle
g.update();
}
shopping = false; // end shopping
ui.changeMoney(-ui.getShopCost(towerChoice)); // rip money
}
else {
float cursor_x_pos = (xpos / (float)(window_width_g / 2)) - 1.0f; //transforms cursor position to screen coordinates
float cursor_y_pos = (ypos / (float)(window_height_g / 2)) - 1.0f;
cursor_x_pos /= 0.2f;
cursor_y_pos /= 0.2f;
// "delete button"
if (glm::length(glm::vec3(cursor_x_pos, -cursor_y_pos, 0.f) - glm::vec3(4.7f, 3.7f, 0.0f)) < .15f) {
shopping = false;
}
}
}
} // set to "shopping" if warranted
else if (glfwGetMouseButton(Window::getWindow(), GLFW_MOUSE_BUTTON_LEFT)) {
double xpos, ypos;
glfwGetCursorPos(Window::getWindow(), &xpos, &ypos);
float cx_pos = (xpos / (float)(window_width_g / 2)) - 1.0f; //transforms cursor position to screen coordinates
float cy_pos = (ypos / (float)(window_height_g / 2)) - 1.0f;
cx_pos /= 0.2f;
cy_pos /= 0.2f;
glm::vec3 cPos(cx_pos, cy_pos, 0.f);
std::vector<glm::vec3> ships{ glm::vec3(3.7f, -2.7f, 0.f), glm::vec3(3.7f, .3f, 0.f), glm::vec3(3.7f, 3.3f, 0.f) };
for (int i = 0; i < 3; ++i) {
if (glm::length(cPos - ships[i]) < .65f && ui.getCurrentMoney() >= ui.getShopCost(i)) {
shopping = true;
cursor.setTex(towerTextures[i]);
towerChoice = i;
break;
}
}
}
/*------------------------------ UPDATE ------------------------------*/
//Update and render "all" targets
for (int i = 0; i < enemyObjects.size(); ++i) {
// Get the current object
EnemyObject* enemy = enemyObjects[i];
// Updates enemy
enemy->update(deltaTime);
// the object is doing a path -> see if it can move on to the next node
if (!enemy->getFinished()) {
Node* n = enemy->getCurrNode();
// getting distance between the object and the node it is heading towards
float d = glm::length(enemy->getPosition() - glm::vec3(n->getX(), n->getY(), 0.0f));
if (d < 0.05) {
enemy->nextNode();
}
}
// the path is finished -> hit player and despawn
else {
enemy->kill = true;
ui.changeHealth(-1);
}
}
// target selection for towers, outside of loop b/c same for each tower
auto min_e = std::min_element(enemyObjects.begin(), enemyObjects.end(), [endNodeLoc](EnemyObject*& e1, EnemyObject*& e2) {
return glm::length(e1->getPosition() - endNodeLoc) < glm::length(e2->getPosition() - endNodeLoc);
});
// towers, this also handles update of bullets
for (TowerObject* t : towerObjects) {
if (enemyObjects.empty())
break;
if (t->getType() == "flame") {
((FlameTower*)t)->update(deltaTime, enemyObjects); // note can use polymorphism no time to update - G
((FlameTower*)t)->updateBullets(deltaTime, enemyObjects);
}
else if (t->getType() == "slow") {
((SlowTower*)t)->update(deltaTime, enemyObjects);
}
else if (t->getType() == "mobile") {
MobileDefender* m = (MobileDefender*)t;
if (m->getFinished()) {
// pick new destination
int q = enemyObjects.back()->getCurrNode()->getId();
// end of new path
g.setEnd(q);
// actually gives enemy the path
doPath(g, m, false);
}
m->update(deltaTime, enemyObjects.back()); // maybe set to min_e (closest to the end)
}
else {
t->update(deltaTime, (*min_e)); // regular towers
}
}
// removing elements from the game array while iterating, don't think could do in "for i=x" loop
for (std::vector<EnemyObject*>::iterator it = enemyObjects.begin(); it != enemyObjects.end();) {
if ((*it)->kill) {
if ((*it)->merked) ui.changeMoney((*it)->value); // specifically killed by tower instead of just deleted
delete (*it);
it = enemyObjects.erase(it);
}
else {
++it;
}
}
/*------------------------------ RENDER ------------------------------*/
// Select proper shader program to use
spriteShader.enable();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, sprite_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite_ebo);
spriteShader.attributeBinding();
// big yeet for UI
glm::mat4 viewMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(0.2f));
spriteShader.setUniformMat4("viewMatrix", viewMatrix);
glViewport(0, 0, 1000, 600);
ui.render(spriteShader);
// best function ever
glViewport(0, 0, 800, 600);
// setup camera to zoom/pan for regular game rendering
g.setScreenScale(cameraZoom);
viewMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(cameraZoom, cameraZoom, cameraZoom)) * glm::translate(glm::mat4(1.0f), cameraTranslatePos);
spriteShader.setUniformMat4("viewMatrix", viewMatrix);
for (EnemyObject* e : enemyObjects) {
e->render(spriteShader);
}
// render towers & bullets
for (TowerObject* t : towerObjects) {
t->render(spriteShader);
}
if (shopping) { // ok to not reset colorMod because this is the last thing the spriteShader deals with and the first of the loop will reset it
spriteShader.setUniform4f("colorMod", glm::vec4(0.1f, 0.1f, 0.1f, -.5f));
cursor.render(spriteShader);
}
//g.render(spriteShader);
// flame throwaaaa
if (!enemyObjects.empty() && !towerObjects.empty()) {
//get ready to draw particles
//glBlendFunc(GL_ONE, GL_ONE);
glDepthMask(GL_FALSE); // draw particles without writing to depth buffer
glBindBuffer(GL_ARRAY_BUFFER, partic_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, partic_ebo);
fireShader->attributeBinding();
for (int i = 0; i < towerObjects.size(); ++i) {
// float fireDistance = (enemyObjects.empty()) ? 3.5f : glm::length(towerObjects.back()->getPosition() - enemyObjects.back()->getPosition());
if (towerObjects[i]->getType() == "flame" && ((FlameTower*)towerObjects[i])->isFiring()) {//(fireDistance < 3.f / (cameraZoom*5.f)) {
drawParticles(fireShader->getShaderID(), 1000, towerObjects[i], cameraZoom);
}
}
glDepthMask(GL_TRUE); // fixes things
}
// Update other events like input handling
glfwPollEvents();
// Push buffer drawn in the background onto the display
glfwSwapBuffers(window.getWindow());
} // while loop
// clean up memory
for (EnemyObject* e : enemyObjects) {
delete e;
}
for (TowerObject* t : towerObjects) {
delete t;
}
}
catch (std::exception &e) {
// print exception and sleep so error can be read
PrintException(e);
std::this_thread::sleep_for(std::chrono::milliseconds(100000));
}
return 0;
}