-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathProjectile.java
More file actions
420 lines (365 loc) · 14.3 KB
/
Projectile.java
File metadata and controls
420 lines (365 loc) · 14.3 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
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
/*
* Copyright 2018 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.destinationsol.game.projectile;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Fixture;
import org.destinationsol.common.SolColor;
import org.destinationsol.common.SolMath;
import org.destinationsol.common.SolRandom;
import org.destinationsol.game.DmgType;
import org.destinationsol.game.FactionManager;
import org.destinationsol.game.FarObject;
import org.destinationsol.game.GameDrawer;
import org.destinationsol.game.SolGame;
import org.destinationsol.game.SolObject;
import org.destinationsol.game.SolObjectEntityWrapper;
import org.destinationsol.game.drawables.Drawable;
import org.destinationsol.game.drawables.DrawableLevel;
import org.destinationsol.game.drawables.SpriteManager;
import org.destinationsol.game.faction.Faction;
import org.destinationsol.game.item.Shield;
import org.destinationsol.game.particle.DSParticleEmitter;
import org.destinationsol.game.particle.EffectConfig;
import org.destinationsol.game.particle.LightSource;
import org.destinationsol.game.ship.SolShip;
import org.destinationsol.health.events.DamageEvent;
import org.destinationsol.location.components.Position;
import org.terasology.gestalt.entitysystem.entity.EntityIterator;
import org.terasology.gestalt.entitysystem.entity.EntityRef;
import java.util.ArrayList;
import java.util.List;
public class Projectile implements SolObject {
private static final float MIN_ANGLE_TO_GUIDE = 2f;
private final ArrayList<Drawable> drawables;
private final ProjectileBody body;
private final Faction faction;
private final DSParticleEmitter bodyEffect;
private final DSParticleEmitter trailEffect;
private final LightSource lightSource;
private final ProjectileConfig config;
private boolean shouldBeRemoved;
private SolObject obstacle;
private SolShip ship;
private boolean wasDamageDealt;
public Projectile(SolGame game, float angle, Vector2 muzzlePos, Vector2 gunVelocity, Faction faction,
ProjectileConfig config, boolean varySpeed, SolShip ship) {
drawables = new ArrayList<>();
this.config = config;
this.ship = ship;
Drawable drawable;
if (config.stretch) {
drawable = new ProjectileDrawable(this, config.sprite.frames, config.texSz);
} else {
drawable = SpriteManager.createSprite(config.sprite.displayName, config.texSz, config.origin.x, config.origin.y, new Vector2(), DrawableLevel.PROJECTILES, 0, 0, SolColor.WHITE, false);
}
drawables.add(drawable);
float speed = config.speed;
if (varySpeed) {
speed *= SolRandom.randomFloat(.9f, 1.1f);
}
if (config.physSize > 0) {
body = new BallProjectileBody(game, muzzlePos, angle, this, gunVelocity, speed, config);
} else {
body = new PointProjectileBody(angle, muzzlePos, gunVelocity, speed, this, game, config.acc);
}
this.faction = faction;
bodyEffect = buildEffect(game, config.bodyEffect, DrawableLevel.PART_BG_0, null, true);
trailEffect = buildEffect(game, config.trailEffect, DrawableLevel.PART_BG_0, null, false);
if (config.lightSz > 0) {
Color col = SolColor.WHITE;
if (bodyEffect != null) {
col = config.bodyEffect.tint;
}
lightSource = new LightSource(config.lightSz, true, 1f, new Vector2(), col);
lightSource.collectDrawables(drawables);
} else {
lightSource = null;
}
}
private DSParticleEmitter buildEffect(SolGame game, EffectConfig ec, DrawableLevel drawableLevel, Vector2 position, boolean inheritsVelocity) {
if (ec == null) {
return null;
}
DSParticleEmitter res = new DSParticleEmitter(ec, -1, drawableLevel, new Vector2(), inheritsVelocity, game, position, body.getVelocity(), 0);
if (res.isContinuous()) {
res.setWorking(true);
drawables.addAll(res.getDrawables());
} else {
game.getPartMan().finish(game, res, position);
}
return res;
}
@Override
public void update(SolGame game) {
body.update(game);
if (obstacle != null) {
//This handles collision of a Projectile with an entity.
if (obstacle instanceof SolObjectEntityWrapper) {
EntityRef entity = ((SolObjectEntityWrapper) obstacle).getEntity();
if (!wasDamageDealt) {
if (config.aoeRadius >= 0) { //This checks if the projectile does Area-Of-Effect damage. If it does not, the value is usually -1
EntityIterator iterator = game.getEntitySystemManager().getEntityManager().iterate(new Position());
while (iterator.next()) {
Vector2 entityPosition = iterator.getEntity().getComponent(Position.class).get().position;
if (getPosition().dst2(entityPosition) <= config.aoeRadius) {
game.getEntitySystemManager().sendEvent(new DamageEvent(config.dmg), entity);
}
}
} else {
game.getEntitySystemManager().sendEvent(new DamageEvent(config.dmg), entity);
}
}
if (config.density > 0) {
obstacle = null;
wasDamageDealt = true;
} else {
collided(game);
//TODO Once SolShip is an entity, this should be refactored to work with it
// if (config.emTime > 0 && obstacle instanceof SolShip) {
// ((SolShip) obstacle).disableControls(config.emTime, game);
// }
return;
}
} else {
//TODO this else block is legacy code for handling contact between a SolObject and an entity. Once every
// SolObject has been converted to an entity, this can be removed.
if (!wasDamageDealt) {
if (config.aoeRadius >= 0) { //If AoE is enabled for this Projectile, damage all within the radius.
game.getObjectManager().doToAllCloserThan(config.aoeRadius, this, (SolObject obj) ->
obj.receiveDmg(config.dmg, game, body.getPosition(), config.dmgType, ship)
);
} else {
obstacle.receiveDmg(config.dmg, game, body.getPosition(), config.dmgType, ship);
}
}
if (config.density > 0) {
obstacle = null;
wasDamageDealt = true;
} else {
collided(game);
if (config.emTime > 0 && obstacle instanceof SolShip) {
((SolShip) obstacle).disableControls(config.emTime, game);
}
return;
}
}
}
if (lightSource != null) {
lightSource.update(true, body.getAngle(), game);
}
maybeGuide(game);
game.getSoundManager().play(game, config.workSound, null, this);
}
private void maybeGuide(SolGame game) {
if (config.guideRotationSpeed == 0) {
return;
}
float ts = game.getTimeStep();
SolShip ne = game.getFactionMan().getNearestEnemy(game, this);
if (ne == null) {
return;
}
float desiredAngle = body.getDesiredAngle(ne);
float angle = getAngle();
float diffAngle = SolMath.norm(desiredAngle - angle);
if (SolMath.abs(diffAngle) < MIN_ANGLE_TO_GUIDE) {
return;
}
float rot = ts * config.guideRotationSpeed;
float min = -rot;
diffAngle = MathUtils.clamp(diffAngle, min, rot);
body.changeAngle(diffAngle);
}
private void collided(SolGame game) {
shouldBeRemoved = true;
Vector2 position = body.getPosition();
buildEffect(game, config.collisionEffect, DrawableLevel.PART_FG_1, position, false);
buildEffect(game, config.collisionEffectBackground, DrawableLevel.PART_FG_0, position, false);
if (config.collisionEffectBackground != null) {
game.getPartMan().blinks(position, game, config.collisionEffectBackground.size);
}
game.getSoundManager().play(game, config.collisionSound, null, this);
}
@Override
public boolean shouldBeRemoved(SolGame game) {
return shouldBeRemoved;
}
@Override
public void onRemove(SolGame game) {
Vector2 position = body.getPosition();
if (bodyEffect != null) {
game.getPartMan().finish(game, bodyEffect, position);
}
if (trailEffect != null) {
game.getPartMan().finish(game, trailEffect, position);
}
body.onRemove(game);
}
@Override
public void receiveDmg(float dmg, SolGame game, Vector2 position, DmgType dmgType, SolObject instigator) {
if (config.density > 0) {
return;
}
collided(game);
}
@Override
public boolean receivesGravity() {
return !config.massless;
}
@Override
public void receiveForce(Vector2 force, SolGame game, boolean acc) {
body.receiveForce(force, game, acc);
}
@Override
public Vector2 getPosition() {
return body.getPosition();
}
@Override
public FarObject toFarObject() {
return null;
}
@Override
public List<Drawable> getDrawables() {
return drawables;
}
@Override
public float getAngle() {
return body.getAngle();
}
@Override
public Vector2 getVelocity() {
return body.getVelocity();
}
@Override
public void handleContact(SolObject other, float absImpulse,
SolGame game, Vector2 collPos) {
}
@Override
public String toDebugString() {
return null;
}
@Override
public Boolean isMetal() {
return null;
}
@Override
public boolean hasBody() {
return true;
}
public Faction getFaction() {
return faction;
}
public boolean shouldCollide(SolObject object, Fixture fixture, FactionManager factionManager) {
if (object instanceof SolShip) {
SolShip ship = (SolShip) object;
if (this.ship == ship) {
return false;
}
if (ship.getHull().getShieldFixture() == fixture) {
if (config.density > 0) {
return false;
}
Shield shield = ship.getShield();
return shield != null && shield.canAbsorb(config.dmgType);
}
return true;
} else if (object instanceof Projectile) {
// Projectiles from the same sources are allowed to overlap
Projectile projectile = (Projectile) object;
return projectile.ship != this.ship;
}
return true;
}
public void setObstacle(SolObject object, SolGame game) {
if (!shouldCollide(object, null, game.getFactionMan())) {
return; // happens for some reason when projectile is just created
}
obstacle = object;
}
public boolean isMassless() {
return config.massless;
}
public ProjectileConfig getConfig() {
return config;
}
private static class ProjectileDrawable implements Drawable {
private final Projectile projectile;
private final Animation<TextureAtlas.AtlasRegion> animation;
private final float width;
private TextureAtlas.AtlasRegion texture;
private float animationTime;
ProjectileDrawable(Projectile projectile, Animation<TextureAtlas.AtlasRegion> animation, float width) {
this.projectile = projectile;
this.animation = animation;
texture = animation.getKeyFrame(animationTime, true);
this.width = width;
}
@Override
public TextureAtlas.AtlasRegion getTexture() {
return texture;
}
@Override
public DrawableLevel getLevel() {
return DrawableLevel.PROJECTILES;
}
@Override
public void update(SolGame game, SolObject o) {
animationTime += game.getTimeStep();
texture = animation.getKeyFrame(animationTime, true);
}
@Override
public void prepare(SolObject o) {
}
@Override
public Vector2 getPosition() {
return projectile.getPosition();
}
@Override
public Vector2 getRelativePosition() {
return Vector2.Zero;
}
@Override
public float getRadius() {
return projectile.config.texSz / 2;
}
@Override
public void draw(GameDrawer drawer, SolGame game) {
float h = width;
float minH = game.getCam().getRealLineWidth() * 3;
if (h < minH) {
h = minH;
}
Vector2 position = projectile.getPosition();
float w = projectile.getVelocity().len() * game.getTimeStep();
if (w < 4 * h) {
w = 4 * h;
}
drawer.draw(texture, w, h, w, h / 2, position.x, position.y, SolMath.angle(projectile.getVelocity()), SolColor.LG);
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public boolean okToRemove() {
return false;
}
}
}