-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainGame.cpp
More file actions
365 lines (303 loc) · 10.4 KB
/
MainGame.cpp
File metadata and controls
365 lines (303 loc) · 10.4 KB
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
#include "MainGame.h"
#include <SDL2/SDL.h>
#include <iostream>
#include <ctime>
#include <random>
#include <algorithm>
#include <Myengine/Myengine.h>
#include <Myengine/Timer.h>
#include <Myengine/Errors.h>
#include "Zombie/Zombie.h"
#include "Zombie/Gun.h"
const float HUMAN_SPEED = 1.0f;
const float ZOMBIE_SPEED = 1.3f;
MainGame::MainGame() :
_screenWidth(1024),
_screenHeight(768),
_gameState(GameState::PLAY),
_player(nullptr),
_numHumansKilled(0),
_numZombiesKilled(0)
{
}
MainGame::~MainGame()
{
for (int i = 0; i < _levels.size(); i++) {
delete _levels[i];
}
}
void MainGame::run()
{
initSystems();
initLevel();
gameLoop();
}
void MainGame::initSystems()
{
Myengine::init();
_window.createWindow("Zombie Game", _screenWidth, _screenHeight, 0);
glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
initShaders();
_agentSpriteBatch.init();
_camera.init(_screenWidth, _screenHeight);
}
void MainGame::initLevel()
{
_levels.push_back(new Level("levels/level1.txt"));
_currentLevel = 0;
_player = new Player();
_player->init(4.0f, _levels[_currentLevel]->getStartPlayerPos(), &_inputManger, &_camera, &_bullets);
_humans.push_back(_player);
static std::mt19937 randomEngine;
randomEngine.seed(time(nullptr));
static std::uniform_int_distribution<int> randX(2, _levels[_currentLevel]->getWidth() - 2);
static std::uniform_int_distribution<int> randY(2, _levels[_currentLevel]->getHeight() - 2);
// Add all the random humans
for (int i = 0; i < _levels[_currentLevel]->getNumHumans(); i++) {
_humans.push_back(new Human);
glm::vec2 pos(randX(randomEngine) * TILE_WIDTH, randY(randomEngine) * TILE_WIDTH);
_humans.back()->init(HUMAN_SPEED, pos);
}
// Add zombies
const std::vector<glm::vec2>& zombiePositions = _levels[_currentLevel]->getZombieStartPositions();
for (int i = 0; i < zombiePositions.size(); i++) {
_zombies.push_back(new Zombie);
_zombies.back()->init(ZOMBIE_SPEED, zombiePositions[i]);
}
// set up the player guns
const float BULLET_SPEED = 20.0f;
_player->addGun(new Gun("Magnum", 10, 1, 5.0f, BULLET_SPEED, 30));
_player->addGun(new Gun("Shotgun", 60, 7, 20.0f, BULLET_SPEED, 4));
_player->addGun(new Gun("MP5", 2, 1, 10.0f, BULLET_SPEED, 20));
}
void MainGame::initShaders()
{
_textureProgram.compileShaders("shaders/colorShading.vert",
"shaders/colorShading.frag");
_textureProgram.addAttribute("vertexPosition");
_textureProgram.addAttribute("vertexColor");
_textureProgram.addAttribute("vertexUV");
_textureProgram.linkShaders();
}
void MainGame::gameLoop()
{
const float DESIRED_FPS = 60.0f;
const int MAX_PHYSICS_STEPS = 6;
Myengine::FpsLimiter fpsLimiter;
fpsLimiter.setMaxFps(60.0f);
const float MS_PER_SECOND = 1000;
const float DESIRED_FRAMETIME = MS_PER_SECOND/ 60;
const float MAX_DELTA_TIME = 1.0f;
float prevTicks = SDL_GetTicks();
while (_gameState == GameState::PLAY) {
fpsLimiter.begin();
float newTicks = SDL_GetTicks();
float frameTime = newTicks - prevTicks;
prevTicks = newTicks;
float totalDeltaTime = frameTime / DESIRED_FRAMETIME;
checkVictory();
_inputManger.update();
processInput();
int i = 0;
while (totalDeltaTime > 0.0f && i < MAX_PHYSICS_STEPS) {
float deltaTime = std::min(totalDeltaTime, MAX_DELTA_TIME);
updateAgents(deltaTime);
updateBullets(deltaTime);
totalDeltaTime -= deltaTime;
i++;
}
_camera.setPosition(_player->getPosition());
_camera.update();
drawGame();
_fps = fpsLimiter.end();
}
}
void MainGame::updateAgents(float deltaTime)
{
// update all humans
for (int i = 0; i <_humans.size(); i++) {
_humans[i]->update(_levels[_currentLevel]->getLevelData(),
_humans,
_zombies,
deltaTime);
}
//Dont forget to update zombies
for (int i = 0; i <_zombies.size(); i++) {
_zombies[i]->update(_levels[_currentLevel]->getLevelData(),
_humans,
_zombies,
deltaTime);
}
// update zombie collisions
for (int i= 0; i < _zombies.size(); i++) {
// collide with other zombies
for (int j = i+1; j < _zombies.size(); j++) {
_zombies[i]->collideWithAgent(_zombies[j]);
}
// collide with other humans
for (int j = 1; j < _humans.size(); j++) {
if (_zombies[i]->collideWithAgent(_humans[j])) {
// add the new zombie
_zombies.push_back(new Zombie);
_zombies.back()->init(ZOMBIE_SPEED, _humans[j]->getPosition());
// and delete the human
delete _humans[j];
_humans[j] = _humans.back();
_humans.pop_back();
}
}
// Collide with Player
if (_zombies[i]->collideWithAgent(_humans[0])) {
Myengine::fatalError("You Lose!");
}
}
// update Human collisions
for (int i= 0; i < _humans.size(); i++) {
for (int j = i+1; j < _humans.size(); j++) {
_humans[i]->collideWithAgent(_humans[j]);
}
}
}
void MainGame::updateBullets(float deltaTime)
{
// update and collde with world
for (int i = 0; i < _bullets.size();) {
// if update returns true the bullet collided with a wall
// if so we remove the bullet
if (_bullets[i].update(_levels[_currentLevel]->getLevelData(), deltaTime)) {
_bullets[i] = _bullets.back();
_bullets.pop_back();
} else {
i++;
}
}
bool wasBulletRemoved;
// Collide with zombies or humans
for (int i = 0; i < _bullets.size(); i++) {
wasBulletRemoved = false;
// check for zombies
for (int j = 0; j < _zombies.size();) {
// damage zombie and kill if out of health
if (_bullets[i].collideWithAgent(_zombies[j])) {
// Damage the zombie
if(_zombies[j]->applyDamage(_bullets[i].getDamage())) {
delete _zombies[j];
_zombies[j] = _zombies.back();
_zombies.pop_back();
_numZombiesKilled++;
} else {
j++;
}
// remove bullet
_bullets[i] = _bullets.back();
_bullets.pop_back();
wasBulletRemoved = true;
i--; // to make sure we dont skip the bullet
// since the bullet was hit we can skip its comparisin with other zombies
break;
} else {
j++;
}
}
// Check for humans if the bullet didnt hit any zombies
if (wasBulletRemoved == false) {
for (int j = 1; j < _humans.size();) {
// damage human and kill if out of health
if (_bullets[i].collideWithAgent(_humans[j])) {
// Damage the human
if(_humans[j]->applyDamage(_bullets[i].getDamage())) {
delete _humans[j];
_humans[j] = _humans.back();
_humans.pop_back();
_numHumansKilled++;
} else {
j++;
}
// remove bullet
_bullets[i] = _bullets.back();
_bullets.pop_back();
wasBulletRemoved = true;
i--; // to make sure we dont skip the bullet
// since the bullet was hit we can skip its comparisin with other humans
break;
} else {
j++;
}
}
}
}
}
void MainGame::checkVictory() {
// TODO: support for multiple levels!
if (_zombies.empty()) {
std::printf("*** You won! *** \n You killed %d humans and %d zombies.\n There are %d/%d humans remaining",
_numHumansKilled,
_numZombiesKilled,
_humans.size() - 1,
_levels[_currentLevel]->getNumHumans());
Myengine::fatalError("");
}
}
void MainGame::processInput()
{
SDL_Event evnt;
while (SDL_PollEvent(&evnt)) {
switch (evnt.type) {
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
_inputManger.setMouseCoords(evnt.motion.x, evnt.motion.y);
break;
case SDL_KEYDOWN:
_inputManger.pressKey(evnt.key.keysym.sym);
break;
case SDL_KEYUP:
_inputManger.releaseKey(evnt.key.keysym.sym);
break;
case SDL_MOUSEBUTTONDOWN:
_inputManger.pressKey(evnt.button.button);
break;
case SDL_MOUSEBUTTONUP:
_inputManger.releaseKey(evnt.button.button);
break;
}
}
}
void MainGame::drawGame()
{
// set base depth here
glClearDepth(1.0);
// Clear the color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_textureProgram.use();
// Draw code goes here
glActiveTexture(GL_TEXTURE0);
// Make sure the texture uses texture 0
GLint textureUniform = _textureProgram.getUniformLocation("mySampler");
glUniform1i(textureUniform, 0);
// Grab and set the camera Matrix
glm::mat4 projectionMatrix = _camera.getCameraMatrix();
GLint pUniform = _textureProgram.getUniformLocation("P");
glUniformMatrix4fv(pUniform, 1, GL_FALSE, &projectionMatrix[0][0]);
// draw the level
_levels[_currentLevel]->draw();
// SpriteBatch Begin - Agent
_agentSpriteBatch.begin();
// Draw the humans
for (int i = 0; i < _humans.size(); i++) {
_humans[i]->draw(_agentSpriteBatch);
}
// Draw the zombies
for (int i = 0; i < _zombies.size(); i++) {
_zombies[i]->draw(_agentSpriteBatch);
}
// Draw bullets
for (int i = 0 ; i < _bullets.size(); i++) {
_bullets[i].draw(_agentSpriteBatch);
}
_agentSpriteBatch.end();
_agentSpriteBatch.renderBatch();
_textureProgram.unUse();
_window.swapBuffer();
}