-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathentity.cpp
More file actions
335 lines (295 loc) · 7.91 KB
/
Copy pathentity.cpp
File metadata and controls
335 lines (295 loc) · 7.91 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
/*-----------------------------------------------------------------------------
Entity.cpp
This tracks the active elements of the gameworld: pickups, projectiles,
robots, etc.
Good Robot
(c) 2014 Shamus Young
-----------------------------------------------------------------------------*/
#include "master.h"
#include "audio.h"
#include "entity.h"
#include "env.h"
#include "fx.h"
#include "player.h"
#include "projectile.h"
#include "render.h"
#include "robot.h"
#include "system.h"
#include "visible.h"
#include "world.h"
#define MAX_SHOVES 30
static vector<Robot> bot;
static vector<Robot> bot_queue;
static fxProjectile projectiles[MAX_PROJECTILES];
static int projectile_top;
static int active_robots;
static int dead_robots;
static vector<fx*> fx_list;
static vector<fxDevice*> device_list;
static bool is_calm; //True if we're on a peaceful screen and not in combat.
static bool in_update;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void insert_coin(GLvector2 position, int value)
{
fxPowerup* c;
c = new fxPowerup;
c->Init(position, POWERUP_COIN, value);
EntityFxAdd(c);
}
static void do_shoving ()
{
static int current_shover;
static int current_shovee;
unsigned max_shoves;
float allowed_distance;
float distance;
GLvector2 delta;
int shoves;
int bots_checked;
unsigned b1, b2;
bool new_shover;
if (bot.empty ())
return;
shoves = 0;
bots_checked = 0;
//Have the bots shove each other to keep them from stacking up into a deathball.
//All bots...
max_shoves = min ((int)bot.size (), MAX_SHOVES);
while (shoves < max_shoves) {
shoves++;
current_shovee++;
b1 = current_shover % bot.size ();
b2 = current_shovee % bot.size ();
new_shover = false;
if (bot[b1].Dead ())
new_shover = true;
if (b1 == b2)
new_shover = true;
if (new_shover) {
current_shover++;
current_shovee = current_shover;
continue;
}
if (bot[b2].Dead ())
continue;
allowed_distance = bot[b1].Size () + bot[b2].Size ();
delta = bot[b1].Position () - bot[b2].Position ();
if (fabs (delta.x) > allowed_distance || fabs (delta.y) > allowed_distance)
continue;
distance = delta.Length ();
if (distance < allowed_distance) {
float strength = 1.0f - distance / allowed_distance;
//delta.Normalize ();
delta *= strength;
bot[b2].Shove (delta * -1);
bot[b1].Shove (delta);
}
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
//TODO: Whhhaaat is this doing here?
bool WorldIsCalm()
{
return is_calm;
}
void EntityDeviceAdd (fxDevice* d)
{
device_list.push_back (d);
}
int EntityDeviceCount () { return device_list.size (); }
void EntityDeviceRender (bool occluded)
{
if (occluded) {
for (unsigned f = 0; f < device_list.size (); f++)
device_list[f]->RenderOccluded ();
} else {
for (unsigned f = 0; f < device_list.size (); f++)
device_list[f]->Render ();
}
RenderQuads ();
}
fxDevice* EntityDeviceFromId (int index)
{
return device_list[index];
}
void EntityFxAdd(fx* item)
{
fx_list.push_back(item);
}
//This adds a SINGLE projectile. Should be used by projectiles wanting to spawn children.
void EntityProjectileAdd (fxProjectile p)
{
projectiles[projectile_top] = p;
projectile_top = (projectile_top + 1) % MAX_PROJECTILES;
}
//This will fire a projectile according to the given properties. It will
//generate sounds and handle grouping. This should be used by the player and robots.
void EntityProjectileFire (fxOwner owner, const Projectile* p_info, GLvector2 origin, GLvector2 vector, int damage_level)
{
int count;
fxProjectile p;
count = clamp (p_info->_volley, 1, 100);
for (int i = 0; i < count; i++) {
p.Init (owner, p_info, origin, vector, damage_level);
p.BouncesSet (p_info->_bounces);
EntityProjectileAdd (p);
}
AudioPlay (p_info->_sound, origin);
}
fxProjectile* EntityProjectileList ()
{
return projectiles;
}
void EntityRobotAdd(Robot b)
{
//If we're in the middle of an update, we might be walking the list of bots.
//If so, queue the new bot and it'll be added next update.
if (in_update)
bot_queue.push_back(b);
else //Not in update mode. Safe to mess with the list.
bot.push_back(b);
}
int EntityRobotsActive () { return active_robots; }
int EntityRobotsDead () { return dead_robots; }
int EntityRobotCount() { return bot.size(); }
Robot* EntityRobot(int index)
{
if (index >= (int)bot.size())
return NULL;
return &bot[index];
}
fx* WorldFx(unsigned index)
{
if (index >= (int)fx_list.size())
return NULL;
return fx_list[index];
}
void EntityXpAdd(GLvector2 position, int change)
{
while (change >= 1000) {
insert_coin(position, 1000);
change -= 1000;
}
while (change >= 100) {
insert_coin(position, 100);
change -= 100;
}
while (change >= 10) {
insert_coin(position, 10);
change -= 10;
}
while (change >= 1) {
insert_coin(position, 1);
change -= 1;
}
}
void EntityClear()
{
bot.clear();
bot_queue.clear();
for (unsigned f = 0; f < fx_list.size(); f++)
delete fx_list[f];
for (unsigned f = 0; f < device_list.size (); f++)
delete device_list[f];
for (int i = 0; i < MAX_PROJECTILES; i++)
projectiles[i].Retire ();
fx_list.clear();
device_list.clear ();
}
Robot* EntityRobotFromId (int id)
{
for (unsigned b = 0; b < bot.size (); b++) {
if (bot[b].Id () == id)
return &bot[b];
}
return NULL;
}
void EntityUpdate()
{
in_update = true;
for (int i = 0; i < MAX_PROJECTILES; i++)
projectiles[i].Update ();
//Last update, we queued up any newly added robots to avoid adding to the
//list while we were iterating over it. Now put them in play.
for (unsigned i = 0; i < bot_queue.size(); i++)
bot.push_back(bot_queue[i]);
bot_queue.clear();
//process general fx
for (unsigned f = 0; f < fx_list.size(); f++) {
if (fx_list[f]->Active ()) {
fx_list[f]->Update ();
} else {
delete fx_list[f];
fx_list.erase(fx_list.begin() + f);
}
}
//Process doors.
for (unsigned f = 0; f < device_list.size (); f++) {
device_list[f]->Update ();
}
//Drop dead bots
for (unsigned b = 0; b < bot.size(); b++) {
if (bot[b].Retired ()) {
bot.erase (bot.begin () + b);
break; ///Only delete one per frame, to avoid hitching during mass deaths.
}
}
is_calm = true;
int active_counter = 0;
dead_robots = 0;
for (unsigned i = 0; i < bot.size(); i++) {
//Things are not calm if any bots are actively trying to kill the player.
if (bot[i].IsAlerted ()) {
is_calm = false;
}
if (bot[i].Dead ())
dead_robots++;
if (bot[i].IsAlerted () && !bot[i].Dead ())
active_counter++;
}
int begin = SystemTick ();
int end = begin + 3;
int update_count = 0;
static int update_bot;
while (SystemTick () < end && update_count < bot.size ()) {
update_bot %= bot.size ();
bot[update_bot].Update ();
update_bot++;
update_count++;
}
do_shoving();
active_robots = active_counter;
in_update = false;
}
void EntityRenderRobots (bool hidden)
{
if (hidden) {
for (unsigned i = 0; i < bot.size (); i++)
bot[i].RenderHidden ();
} else {
for (unsigned i = 0; i < bot.size (); i++)
bot[i].RenderBody ();
RenderQuads ();
RenderTriangles ();
for (unsigned i = 0; i < bot.size (); i++)
bot[i].RenderEye ();
RenderQuads ();
glDepthFunc (GL_EQUAL);
for (unsigned i = 0; i < bot.size (); i++)
bot[i].RenderIris ();
RenderQuads ();
for (unsigned i = 0; i < bot.size (); i++)
bot[i].RenderPain ();
glDepthFunc (GL_LEQUAL);
}
}
void EntityRenderFx()
{
glDepthMask(false);
for (unsigned f = 0; f < fx_list.size(); f++)
fx_list[f]->Render();
for (int i = 0; i < MAX_PROJECTILES; i++)
projectiles[i].Render ();
}