-
Notifications
You must be signed in to change notification settings - Fork 5
/
scene_trace.cpp
389 lines (351 loc) · 10.1 KB
/
scene_trace.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
/*
Copyright 2013-2015 Rohit Nirmal
This file is part of Clonepoint.
Clonepoint is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clonepoint is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/
#include "scene.h"
#define GUNPOINT_MIN_DISTANCE_SQUARED 2000
float lerp(float x, float x0, float x1, float y0, float y1)
{
return ((x - x0) * (y1 - y0) / (x1 - x0)) + y0;
}
bool Scene::isTraceBlocked(Entity* source, vec2f a, vec2f b, vec2f* c, int* volIndex, bool* isDoor, float vertEpsilon, interpFunc func)
{
//assume trace is not blocked at beginning.
c->x = b.x;
c->y = b.y;
*volIndex = -1;
*isDoor = false;
bool leftToRight = (b.x >= a.x);
bool goingDown = (b.y >= a.y);
if (fabs(a.x - b.x) < vertEpsilon) //if this trace is pretty much vertical...
{
return traceInDirection(source, a, b, c, goingDown, TDVertical, volIndex, isDoor, func);
}
else
{
return traceInDirection(source, a, b, c, leftToRight, TDHorizontal, volIndex, isDoor, func);
}
}
bool Scene::traceInDirection(Entity* source, vec2f a, vec2f b, vec2f* end, bool downOrRight, TraceDirection direction, int* volIndex, bool* isDoor, interpFunc func)
{
int j;
int incr = downOrRight ? 1 : -1;
float endOppositeCheck;
float target;
int start;
vec2f interpolatedPoint, oldPoint;
oldPoint.x = a.x;
oldPoint.y = a.y;
bool stop = false;
if (direction == TDHorizontal)
{
start = a.x;
target = downOrRight ? b.x : fabs(b.x - 2 * a.x);
endOppositeCheck = b.x;
}
else
{
start = a.y;
target = downOrRight ? b.y : fabs(b.y - 2 * a.y);
endOppositeCheck = b.y;
}
if (func != &Scene::interpStandard && func != &Scene::traceGuardSight) //don't stop at b - go to map boundary.
{
endOppositeCheck = 0;
target = (direction == TDHorizontal) ? _currentMap->getMapWidth() : _currentMap->getMapHeight();
}
for (j = start; j < target; j += incr) //scan from left to right, or right to left.
{
if ((!downOrRight && j <= endOppositeCheck) || stop) //stupid hack to avoid copying this for loop.
{
break;
}
interpolatedPoint = (direction == TDHorizontal) ? vec2f(j, lerp(j, a.x, b.x, a.y, b.y)) : vec2f(lerp(j, a.y, b.y, a.x, b.x), j);
(this->*func)(source, interpolatedPoint, func == &Scene::interpStandard ? oldPoint : a, volIndex, isDoor, &stop, end);
if (stop)
{
return true;
}
oldPoint = interpolatedPoint;
}
return false;
}
void Scene::traceBullet(LivingEntity* entity, vec2f target, GunShotTraceType gstt, bool fired)
{
vec2f position = entity->getCollisionRectCenterPosition();
bool madeNoise = false;
if (fired)
{
if (gstt == Shot_FromEnemyVoluntary)
{
isTraceBlocked(NULL, position, target, &_unused_c, &_unused_index, &_unused_bool, 16.0f, &Scene::bulletFiredFromEnemy);
}
else
{
if (gstt == Shot_FromPlayer)
{
if (_numPlayerBullets == 0)
{
return;
}
_numPlayerBullets--;
madeNoise = true;
_playerShotFired = true;
isTraceBlocked(NULL, position, target, &_unused_c, &_unused_index, &_unused_bool, 16.0f, &Scene::bulletFiredFromPlayer);
}
else
{
isTraceBlocked(entity, position, target, &_unused_c, &_unused_index, &_unused_bool, 64.0f, &Scene::bulletFiredFromEnemyInvoluntary);
}
}
Locator::getAudio()->playSound("pistol");
}
else
{
isTraceBlocked(NULL, position, target, &_unused_c, &_unused_index, &_unused_bool, 16.0f, &Scene::traceLaserSight);
}
if (!_player->isAlive() && dynamic_cast<Enemy*>(entity))
{
_loadMenuVisible = true;
static_cast<Enemy*>(entity)->changeState(IDLE);
}
if (madeNoise) //make noise last, in case an enemy is killed and another may be alerted in its place
addNoise(position.x - _camera.x, position.y - _camera.y, 512, true, ALERT_RUN, NULL);
}
void Scene::bulletFiredFromEnemy(Entity* source, vec2f interpolatedPoint, vec2f start, int* index, bool* b1, bool* stop, vec2f* end)
{
breakOnGlass(interpolatedPoint, start, stop);
if (*stop)
{
return;
}
if (vec2InRect(interpolatedPoint, _player->getCollisionRect()) && _player->isAlive())
{
_player->die();
_stopTrajPoint = 0;
handlePlayerShotWhileAttached();
}
}
void Scene::bulletFiredFromPlayer(Entity* source, vec2f interpolatedPoint, vec2f start, int* index, bool* b1, bool* stop, vec2f* end)
{
breakOnGlass(interpolatedPoint, start, stop);
if (*stop)
{
return;
}
killEnemies(source, interpolatedPoint, stop);
}
//gun fired by guard involuntarily from activation can kill other guards or player.
void Scene::bulletFiredFromEnemyInvoluntary(Entity* source, vec2f interpolatedPoint, vec2f start, int* index, bool* b1, bool* stop, vec2f* end)
{
breakOnGlass(interpolatedPoint, start, stop);
if (*stop)
{
return;
}
killEnemies(source, interpolatedPoint, stop);
if (vec2InRect(interpolatedPoint, _player->getCollisionRect()) && _player->isAlive())
{
_player->die();
_stopTrajPoint = 0;
handlePlayerShotWhileAttached();
}
}
void Scene::traceLaserSight(Entity* source, vec2f interpolatedPoint, vec2f start, int* index, bool* b1, bool* stop, vec2f* end)
{
size_t k;
CollisionVolume* vol;
bool leftToRight = interpolatedPoint.x > start.x;
for (k = 0; k < _numCollideVols; k++)
{
vol = _currentMap->getCollideVolPointerAt(k);
if (vec2InRect(interpolatedPoint, vol->rect) && vol->active())
{
if (!vol->glass())
{
*stop = true;
break;
}
}
}
for (k = 0; k < _currentMap->getNumberOfEnemies(); k++)
{
Enemy* enemy = _currentMap->getEnemyAt(k);
if (enemy->isAlive())
{
if (vec2InRect(interpolatedPoint, enemy->getCollisionRect()))
{
bool hold = leftToRight xor (enemy->getDirection() == Right);
hold = hold && (start - interpolatedPoint).length_squared() >= GUNPOINT_MIN_DISTANCE_SQUARED;
enemy->setHeldAtGunpoint(hold);
*stop = true;
break;
}
else
{
if (enemy->isHeldAtGunpoint())
{
enemy->setHeldAtGunpoint(false);
}
}
}
}
_laserEnd = interpolatedPoint;
}
void Scene::traceGuardSight(Entity* source, vec2f interpolatedPoint, vec2f start, int* index, bool* b1, bool* stop, vec2f* end)
{
size_t k;
*stop = false;
vec2f oldPoint = start;
for (k = 0; k < _numCollideVols; k++)
{
//if the trace hits an active collision volume that is NOT glass, abandon check.
if (vec2InRect(interpolatedPoint, _currentMap->getCollideVolAt(k).rect) && _currentMap->getCollideVolAt(k).active() && !_currentMap->getCollideVolAt(k).glass())
{
*index = k;
if (_currentMap->getCollideVolAt(k).door())
{
*b1 = true;
}
handleTraceHit(&oldPoint, &interpolatedPoint, end, _currentMap->getCollideVolPointerAt(k));
*stop = true;
return;
}
}
if (handleTraceHitMapBounds(&interpolatedPoint, end))
{
*stop = true;
}
for (k = 0; k < _currentMap->getNumberOfEnemies(); k++)
{
Enemy* enemy = _currentMap->getEnemyAt(k);
if (enemy != source && enemy->isAlive())
{
if (vec2InRect(interpolatedPoint, enemy->getCollisionRect()))
{
*stop = true;
return;
}
}
}
}
void Scene::interpStandard(Entity* source, vec2f interpolatedPoint, vec2f start, int* index, bool* b1, bool* stop, vec2f* end)
{
size_t k;
*stop = false;
vec2f oldPoint = start;
for (k = 0; k < _numCollideVols; k++)
{
//if the trace hits an active collision volume that is NOT glass, abandon check.
if (vec2InRect(interpolatedPoint, _currentMap->getCollideVolAt(k).rect) && _currentMap->getCollideVolAt(k).active() && !_currentMap->getCollideVolAt(k).glass())
{
*index = k;
if (_currentMap->getCollideVolAt(k).door())
{
*b1 = true;
}
handleTraceHit(&oldPoint, &interpolatedPoint, end, _currentMap->getCollideVolPointerAt(k));
*stop = true;
return;
}
}
if (handleTraceHitMapBounds(&interpolatedPoint, end))
{
*stop = true;
}
}
void Scene::breakOnGlass(vec2f interpolatedPoint, vec2f start, bool* stop)
{
size_t k;
CollisionVolume* vol;
bool leftToRight = interpolatedPoint.x > start.x;
for (k = 0; k < _numCollideVols; k++)
{
vol = _currentMap->getCollideVolPointerAt(k);
if (vec2InRect(interpolatedPoint, vol->rect) && vol->active())
{
if (vol->glass())
{
//if the bullet hits glass, break it.
glassShatter(vol, vec2f(leftToRight ? 0.1f : -0.1f, 0));
}
else
{
*stop = true;
return;
}
}
}
}
void Scene::killEnemies(Entity* source, vec2f interpolatedPoint, bool* stop)
{
size_t k;
for (k = 0; k < _currentMap->getNumberOfEnemies(); k++)
{
Enemy* enemy = _currentMap->getEnemyAt(k);
if (enemy != source && enemy->isAlive())
{
if (vec2InRect(interpolatedPoint, enemy->getCollisionRect()))
{
enemy->setAlive(false);
enemy->changeState(KNOCKED_OUT);
*stop = true;
break;
}
}
}
}
bool Scene::isMouseCursorSeen(int mx, int my)
{
size_t i;
Enemy* enemy;
vec2f position;
vec2f v1, v2;
vec2f mousePos = vec2f(mx + _camera.x, my + _camera.y);
int firstRadius;
float dist, angle, testAngle;
GuardState state;
for (i = 0; i < _currentMap->getNumberOfEnemies(); i++)
{
enemy = _currentMap->getEnemyAt(i);
state = enemy->getState();
if (state == KNOCKED_OUT || state == FALLING || state == PINNED)
{
continue;
}
vec2f position = enemy->getCollisionRectCenterPosition();
vec2f v1 = vec2f(enemy->getDirection() == Right ? 1 : -1, 0);
dist = vec2f_distance(position, mousePos);
if (enemy->getType() == Enemy_Sniper)
{
firstRadius = ENEMY_FOV_RADIUS_SNIPER;
testAngle = ENEMY_FOV_HALFANGLE_SEEN;
}
else
{
firstRadius = ENEMY_FOV_RADIUS_LIT;
testAngle = enemy->canSeePlayer() ? ENEMY_FOV_HALFANGLE_SEEN : ENEMY_FOV_HALFANGLE;
}
if (dist < firstRadius)
{
v2 = vec2f_normalize(vec2f(mousePos.x - position.x, mousePos.y - position.y));
angle = acos(v1.dot(v2));
if (angle < ToRadian(testAngle) &&
!isTraceBlocked(enemy, position, mousePos, &_unused_c, &_unused_index, &_unused_bool, 16.0f, &Scene::traceGuardSight))
{
return true;
}
}
}
return false;
}