-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathentity.cpp
More file actions
332 lines (282 loc) · 8.31 KB
/
entity.cpp
File metadata and controls
332 lines (282 loc) · 8.31 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
#include "defines.hpp"
#include "entity.hpp"
#include "enemies.hpp"
#include "entityPlayer1.hpp"
#include "entityPlayer2.hpp"
#include "entityblackhole.hpp"
#include "entitygrunt.hpp"
#include "entityline.hpp"
#include "entitymayfly.hpp"
#include "entityplayermissile.hpp"
#include "entityproton.hpp"
#include "entityrepulsor.hpp"
#include "entitysnake.hpp"
#include "entityspinner.hpp"
#include "entitytinyspinner.hpp"
#include "entitywanderer.hpp"
#include "entityweaver.hpp"
#include "game.hpp"
#include "grid.hpp"
#include "particle.hpp"
#include "players.hpp"
#include <SDL3/SDL_opengl.h>
entity::entity()
: mType(ENTITY_TYPE_UNDEF)
{
setState(ENTITY_STATE_INACTIVE);
mSpawnTime = 40;
mDestroyTime = 3;
mIndicateTime = 75;
mStateTimer = 0;
mAggression = 1;
mEdgeBounce = false;
mGridBound = true;
mGenId = 0;
}
// Static class factory
entity* entity::createEntity(EntityType _entity, const game& gameRef)
{
switch (_entity) {
case ENTITY_TYPE_PLAYER1:
return new entityPlayer1(gameRef);
case ENTITY_TYPE_PLAYER2:
return new entityPlayer2();
case ENTITY_TYPE_PLAYER_MISSILE:
return new entityPlayerMissile();
case ENTITY_TYPE_GRUNT:
return new entityGrunt(gameRef);
case ENTITY_TYPE_WANDERER:
return new entityWanderer();
case ENTITY_TYPE_WEAVER:
return new entityWeaver();
case ENTITY_TYPE_SPINNER:
return new entitySpinner(gameRef);
case ENTITY_TYPE_TINYSPINNER:
return new entityTinySpinner(gameRef);
case ENTITY_TYPE_MAYFLY:
return new entityMayfly();
case ENTITY_TYPE_SNAKE:
return new entitySnake();
case ENTITY_TYPE_BLACKHOLE:
return new entityBlackHole(gameRef);
case ENTITY_TYPE_REPULSOR:
return new entityRepulsor(gameRef);
case ENTITY_TYPE_PROTON:
return new entityProton();
case ENTITY_TYPE_LINE:
return new entityLine();
default:
return new entity();
}
return 0;
}
void entity::draw()
{
mModel.Identity();
mModel.Scale(mScale);
mModel.Rotate(mAngle);
mModel.Translate(mPos);
if (this->getState() == entity::ENTITY_STATE_INDICATING) {
if (((int)(mStateTimer / 5)) & 1) {
vector::pen pen = mPen;
mModel.draw(pen);
}
} else if (this->getEnabled() && (this->getState() != entity::ENTITY_STATE_SPAWN_TRANSITION)) {
vector::pen pen = mPen;
if (getState() == ENTITY_STATE_SPAWNING) {
Point3d scale = mScale;
Point3d trans = mPos;
float inc = 1.0f / mSpawnTime;
float progress = mStateTimer * inc;
// *********************************************
glLineWidth(pen.lineRadius * .3);
glBegin(GL_LINES);
progress = 1 - progress;
float a = progress;
if (a < 0)
a = 0;
if (a > 1)
a = 1;
pen.a = a;
mModel.Identity();
mModel.Scale(scale * progress * 1);
mModel.Rotate(mAngle);
mModel.Translate(trans);
mModel.emit(pen);
// *********************************************
progress = progress + .25;
a = 1 - progress;
if (a < 0)
a = 0;
if (a > 1)
a = 1;
pen.a = a;
mModel.Identity();
mModel.Scale(scale * progress * 4);
mModel.Rotate(mAngle);
mModel.Translate(trans);
mModel.emit(pen);
// *********************************************
progress = progress + .25;
a = 1 - progress;
if (a < 0)
a = 0;
if (a > 1)
a = 1;
pen.a = a;
mModel.Identity();
mModel.Scale(scale * progress * 7);
mModel.Rotate(mAngle);
mModel.Translate(trans);
mModel.emit(pen);
// *********************************************
// Restore stuff
mModel.Identity();
mModel.Rotate(mAngle);
mModel.Scale(scale);
mModel.Translate(trans);
glEnd();
}
mModel.draw(pen);
}
}
void entity::runTransition()
{
setState(ENTITY_STATE_RUNNING);
}
void entity::run()
{
mAggression += .0002;
mPos += mSpeed;
mPos += mDrift;
mAngle += mRotationRate;
mAngle = fmodf(mAngle, 2.0f * PI);
// Keep it on the grid
if (mGridBound) {
Point3d hitPoint;
Point3d speed = mSpeed;
if (theGame->mGrid->hitTest(mPos, mRadius, &hitPoint, &speed)) {
mPos = hitPoint;
if (mEdgeBounce) {
mSpeed = speed;
}
}
}
// Update the model's matrix
mModel.Identity();
mModel.Scale(mScale);
mModel.Rotate(mAngle);
mModel.Translate(mPos);
mDrift *= .95;
}
void entity::spawnTransition()
{
setState(ENTITY_STATE_SPAWNING);
mStateTimer = mSpawnTime;
mSpeed = Point3d(0, 0, 0);
mDrift = Point3d(0, 0, 0);
mAngle = 0;
mRotationRate = 0;
mAggression = 1;
// Update the model's matrix
mModel.Identity();
mModel.Scale(mScale);
mModel.Rotate(mAngle);
mModel.Translate(mPos);
spawn();
}
void entity::spawn()
{
if (--mStateTimer <= 0) {
setState(ENTITY_STATE_RUNNING);
}
}
void entity::destroyTransition()
{
setState(ENTITY_STATE_DESTROYED);
mStateTimer = mDestroyTime;
++mGenId;
// Explode the object into line entities
theGame->mEnemies->explodeEntity(*this);
}
void entity::destroy()
{
if (--mStateTimer <= 0) {
setState(ENTITY_STATE_INACTIVE);
return;
}
// Throw out some particles
Point3d pos(this->mPos);
Point3d angle(0, 0, 0);
float speed = 2.0;
float spread = 2 * PI;
int num = 20;
int timeToLive = 200;
vector::pen pen = mPen;
pen.r *= 1.2;
pen.g *= 1.2;
pen.b *= 1.2;
pen.a = 200;
pen.lineRadius = 5;
theGame->mParticles->emitter(&pos, &angle, speed, spread, num, &pen, timeToLive, true, true, .97, true);
}
void entity::indicateTransition()
{
++mGenId;
mStateTimer = mIndicateTime;
setState(ENTITY_STATE_INDICATING);
}
void entity::indicating()
{
if (--mStateTimer <= 0) {
setState(ENTITY_STATE_INACTIVE);
}
}
void entity::hit(entity* aEntity)
{
if (aEntity) {
entityPlayerMissile* missile = dynamic_cast<entityPlayerMissile*>(aEntity);
if (missile) {
if (mScoreValue) {
// Add points and display them at the destruction point
if (missile->mPlayerSource == 0)
theGame->getPlayer1()->addKillAtLocation(mScoreValue, getPos());
else if (missile->mPlayerSource == 1)
theGame->getPlayer2()->addKillAtLocation(mScoreValue, getPos());
else if (missile->mPlayerSource == 2)
theGame->getPlayer3()->addKillAtLocation(mScoreValue, getPos());
else if (missile->mPlayerSource == 3)
theGame->getPlayer4()->addKillAtLocation(mScoreValue, getPos());
}
theGame->mSound->playTrack(SOUNDID_ENEMYHIT);
} else if (aEntity && aEntity->getType() == entity::ENTITY_TYPE_BLACKHOLE) {
theGame->mSound->playTrack(SOUNDID_ENEMYHIT);
} else if ((aEntity && (aEntity->getType() == entity::ENTITY_TYPE_PLAYER1)) || (aEntity->getType() == entity::ENTITY_TYPE_PLAYER2)) {
theGame->mSound->playTrack(SOUNDID_ENEMYHIT);
}
} else {
// It must be either a bomb or the player getting destroyed
}
setState(ENTITY_STATE_DESTROY_TRANSITION);
}
entity* entity::hitTest(const Point3d& pos, float radius)
{
Point3d ourPos(0.0f, 0.0f, 0.0f);
// this->getModel()->mMatrix.TransformVertex(ourPos, &ourPos);
getModel()->mMatrix.TranslateVertex(ourPos);
#if 0
float distance = mathutils::calculate2dDistance(pos, ourPos);
float resultRadius = radius + getRadius();
if (distance < resultRadius)
{
return this;
}
#else
float distance = mathutils::calculate2dDistanceSquared(pos, ourPos);
float resultRadius = radius + getRadius();
if (distance < resultRadius * resultRadius) {
return this;
}
#endif
return nullptr;
}