-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
462 lines (398 loc) · 10.2 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
#include "game.h"
#include "lizandroP.h"
Game::Game(Seconds* s)
{
timer = s;
lastTime = s->Get();
noScoreReport = 0;
ParseLevel("lev1.svg", this, 3.0);
for (unsigned int i = 0; i < walls.size(); i++) {
walls[i].game = this;
}
Init();
//level1.load("Level1.obj");
floor.load("floor.obj");
}
void Game::Init()
{
// RESET
int m = this->mobs.size();
for (int i = 0; i < m; i++) {
this->mobs[i]->death(this);
}
bullets.clear();
bulletHoles.clear();
//SET
temperature = 25.0; // temperature in celsius
lastTime = timer->Get();
displayGameOverOrWon = -5;
togGamOverDisplay = false;
gameCounter = 0;
gameRunning = 0;
srand(time(NULL));
velocityX = velocityY = 0.0f;
killStreak = 0;
maxKillStreak = 0;
moveX = moveY = 0;
aiming = 0;
depth = .15f;
zoom = 0;
maxZoom = .2f;
minZoom = .15f;
togPortal =0;
setPortal =0;
nbullets = 10;
maxbullets = 10;
partyMode = 0;
nkills=0;
guntype =0;
hitAnim = 0;
dmgAnim = 0;
gundamage= 10;
mobNum = 0;
mobDist= 0.0;
lkey = 0;
shots = 0;
hits = 0;
currscore =0;
servMessage.clear();
playerHP = 60;
maxHP = 60;
setReloadDelay =0;
position = Vec(0,2,0);
direction = Vec(0.0,0.0,0.0);
setGun(this,0);
respawn_mobs(this, 10);
for (unsigned int i = 0; i < mobs.size(); i++) {
Vec* spt = &spawnPts[i % spawnPts.size()];
mobs[i]->spawn(spt);
mobs[i]->setTick();
}
//startAstar(this);
}
int Game::Continue()
{
if (gameRunning == 0) {
gameRunning = 1;
raptorsound();
return 1;
}
if (displayGameOverOrWon == 1 && timer->Get() > lastTime + 3.0) {
displayGameOverOrWon = 0;
return 1;
}
return 0;
}
void Game::Move()
{
if (gameRunning == 0)
return;
if (hitAnim > 0)
hitAnim--;
if(setReloadDelay > 0)
setReloadDelay -= 1;
if (zoom == 1 && depth < maxZoom) {
depth += (maxZoom - minZoom) / 15;
}else if (zoom == 0 && depth > minZoom) {
depth -= (maxZoom - minZoom) / 15;
}
if (moveY == 0 || moveX == 0) {
// moving orthogonally
velocityX = (float) moveX;
velocityY = (float) moveY;
} else {
// moving diagonally
velocityX = (float) moveX / 1.414;
velocityY = (float) moveY / 1.414;
}
// slow down when aiming
float speed = 0.2f - (float) aiming * 0.1f;
// do not change ox and oz if the player is not moving
if (nx != position.x || nz != position.z) {
oz = position.z;
ox = position.x;
}
// assign new position and save that position in nx and nz
position.z -= (velocityX * cos(direction.x)
+ velocityY * -sin(direction.x)) * speed;
position.x -= (velocityY * cos(direction.x)
+ velocityX * sin(direction.x)) * speed;
nz =position.z;
nx =position.x;
int nMobs = mobs.size();
while (nMobs < 5) {
SpawnNewMob();
nMobs = mobs.size();
}
for (int i = 0; i < nMobs; i++) {
for (unsigned int j = 0; j < walls.size(); j++) {
Vec temp;
Vec* velocity = mobs[i]->getVel();
walls[j].Collide(mobs[i]->getLoc(), 2.0, &temp);
*velocity = Reflect(*velocity, temp);
}
if (mobs[i]->Collide(&position) == 1) {
this->playerHP -= 5;
mobs[i]->damage(10, this);
this->dmgAnim = 20;
killStreak = 0;
cout << "Raptor " << i << " damaged player 5 points. " << endl;
cout << "Player has " << playerHP << " health remaining." << endl;
}
//for (unsigned int j = 0; j < mobs.size(); j++) {
// mobs[j]->Collide(mobs[i]->getLoc());
//}
//Actually happens in view->render, because it needs the map.
mobs[i]->move(this);
float ang = atan2(mobs[i]->getVel()->x,
mobs[i]->getVel()->z);
ang = ang / 3.14159 * 180.0 + 90.0;
float lastAng = mobs[i]->lastFace.x;
float diff = ang - lastAng;
if (diff > 180.0 || diff < -180.0)
diff *= -1.0;
float delta = 5.0;
if (diff > 0.1) {
if (diff < delta)
lastAng = ang;
else
lastAng += delta;
} else if (diff < -0.1) {
if (diff > delta)
lastAng = ang;
else
lastAng -= delta;
} else {
lastAng = ang;
}
lastAng = fmod(lastAng,360.0);
if (lastAng < 0.0)
lastAng+=360.0;
mobs[i]->body.rot(0,0,lastAng,0);
mobs[i]->lastFace.x = lastAng;
if (mobs[i]->getTick() > gameCounter || mobs[i]->dino == 0)
continue;
mobs[i]->setTick(gameCounter + (15 * RAND) + 30);
Vec sightDir = -1.0 * (*mobs[i]->getLoc() - position);
float closestSight = sightDir.Magnitude();
int wallHit = 0;
int wallCount = walls.size();
for (int j = 0; j < wallCount; j++) {
if (walls[j].Ray(*mobs[i]->getLoc(), sightDir, &closestSight) == 1)
wallHit = 1;
}
if (wallHit == 0) {
Bullet b;
dino_sound();
// 0 to 20-->easy, 20-59 -->harder, 60+ impossible
float difficulty = ((float)nkills - 20.0);
if (difficulty < 0.0)
difficulty = 0.0;
float mobErr = 0.2 - (difficulty / 20.0 * 0.1);
Vec err = Vec((2.0 * RAND - 0.1) * mobErr,
(2.0 * RAND - 0.1) * mobErr,
(2.0 * RAND - 0.1) * mobErr);
sightDir.Normalize();
sightDir = sightDir + err;
sightDir = sightDir * closestSight;
b.origin = *mobs[i]->getLoc() + Vec(0,-1,0);
b.direction = sightDir;
b.end = b.origin + b.direction;
b.age = 30;
bullets.push_back(b);
float tmp = 9e9;
if (RaySphere(b.origin, b.direction, position, 1, &tmp)) {
playerHP -= 5;
dmgAnim = 20;
killStreak = 0;
}
if (playerHP <= 0) {
playerHP = 0;
// set displayGameOverOrWon only once
if (togGamOverDisplay == false) {
togGamOverDisplay = true;
displayGameOverOrWon = 3;
setReloadDelay = 0;
lastTime = timer->Get();
}
// use to display the mesage on screen that the player
// wone or lost the game
if(displayGameOverOrWon == 0) {
Init();
} else if (displayGameOverOrWon == 2 && !noScoreReport) {
// Make sure the game over screen comes up before
// we block with this web request.
Web w;
string s = w.Score(name,
nkills,
shots,
hits,
gameCounter,
maxKillStreak);
vector<string> svec = Split(s,",");
if (svec.size() > 1) {
servMessage = svec;
}
}
if (displayGameOverOrWon > 1)
displayGameOverOrWon -= 1;
}
}
}
for (unsigned int i = 0; i < walls.size(); i++) {
walls[i].Collide(&position);
}
for (unsigned int i = 0; i < bullets.size(); i++) {
if (bullets[i].age-- < 1) {
bullets[i] = bullets.back();
bullets.pop_back();
i--;
}
}
// handle portals and portal placement
if (togPortal == 1) {
float rotx = direction.x;
float roty = direction.y - PI / 2.0;
// player defined portals
defaultPortl.reLocateOBJ(position.x,
position.y,
position.z,
position.x,
position.y,
position.z);
if (setPortal == 1) {
setPortal ^= 1;
defaultPortl.loc(position.x, position.y, position.z,
//ox, 2, oz
position.x+sin(rotx) * sin(roty),
2,
position.z+cos(rotx) * sin(roty));
}
// game defined portals player and mob can use it
stPor1.reLocateOBJ(position.x,
position.y,
position.z,
position.x,
position.y,
position.z);
stPor2.reLocateOBJ(position.x,
position.y,
position.z,
position.x,
position.y,
position.z);
if (position.y != 2)
position.y = 2;
}
if (dmgAnim > 0)
dmgAnim--;
gameCounter++;
}
void Game::SpawnNewMob()
{
// Spawn mob at a random point away from player and other mobs
int rnd;
int counter = 20;
int prox = 1;
double thresh = 20.0;
while (prox == 1 && counter-- > 0) {
prox = 0;
rnd = RAND * ((float)spawnPts.size() + .9999);
if ((spawnPts[rnd] - position).Magnitude() < thresh*2.5) {
prox = 1;
continue;
}
}
respawn_mobs(this, 1);
mobs[mobs.size()-1]->spawn(&spawnPts[rnd]);
}
void Game::Shoot()
{
shots++;
float rotx = direction.x;
float roty = direction.y - PI / 2.0;
float trailLen = 5.0;
Vec hitNormal;
int numBullets = 1;
//if (this->guntype == 2)
//numBullets = 6;
for (int i = 0 ; i < numBullets; i++) {
Bullet b;
Vec origin = position - Vec(0,.1,0);
Vec direction = Vec(sin(rotx) * sin(roty) * trailLen,
cos(roty) * trailLen,
cos(rotx) * sin(roty) * trailLen);
// -----------Check collision----------------
float closest = 9e9;
int wallHit = 0;
int wallCount = walls.size();
for (int i = 0; i < wallCount; i++) {
if (walls[i].Ray(origin, direction, &closest, &hitNormal) == 1)
wallHit = 1;
}
/*
// &hitNormal is optional, I'm using it for bulletholes
// Here's how'd you use this.
// You may not even need "hit" if you are just comparing wall dist to
// some mob's dist unless that mob was > 9e9 away for some reason.
if (hit == 1) {
cout << "hit distant: " << closest << endl;
} else {
cout << "no hit" << endl;
}
//cout << wallHit << endl;
if (wallHit != 0)
return;
*/
int mobHit = -1; // Using 'hit' to pick which mob we shot now
int mobCount = mobs.size();
for (int i = 0; i < mobCount; i++) {
if (RaySphere(origin, direction,
*(mobs[i]->getLoc()), 1.0, &closest) == 1
)
mobHit = i;
}
if (mobHit >= 0) {
wallHit = 0;
mobNum = mobHit;
mobDist= closest;
float distMult = 15.0 / closest;
if (distMult < 0.0)
distMult = 0.0;
if (distMult > 1.0)
distMult = 1.0;
// !--- This cout can get removed after actual Mob damage works
int totDam = (int)((float)gundamage * distMult);
hits++;
mobs[mobHit]->damage(totDam, this);
if (totDam > 0)
hitAnim = 20;
}
if (wallHit == 1) {
Vec loc = origin + direction.Norm() * (closest - 0.01);
bulletHoles.push_back(BulletHole(loc,hitNormal));
while (bulletHoles.size() > MAX_BULLET_HOLES)
bulletHoles.pop_front();
}
b.origin = origin;
b.direction = direction;
b.end = b.origin + b.direction;
b.age = 30;
bullets.push_back(b);
}
}
void Game::renderGameOver(float xres, float yres, unsigned int Tex)
{
glBindTexture(GL_TEXTURE_2D, Tex);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(xres, yres);
glTexCoord2f(1.0f, 0.0f);
glVertex2f( xres, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex2f( 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f( 0.0f, yres);
glBindTexture(GL_TEXTURE_2D, 0);
glEnd();
}