-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathProjectile.java
More file actions
187 lines (153 loc) · 6.37 KB
/
Projectile.java
File metadata and controls
187 lines (153 loc) · 6.37 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
package com.fundynamic.d2tm.game.entities.projectiles;
import com.fundynamic.d2tm.game.behaviors.Destructible;
import com.fundynamic.d2tm.game.behaviors.Moveable;
import com.fundynamic.d2tm.game.entities.*;
import com.fundynamic.d2tm.game.entities.units.UnitFacings;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.math.Coordinate;
import com.fundynamic.d2tm.math.Vector2D;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
public class Projectile extends Entity implements Moveable, Destructible {
// state
private Coordinate target;
private boolean destroyed;
private float height = 0F; // supposed 'height' of projectile
private float distanceCalculatedALaunch;
public Projectile(Coordinate mapCoordinates, SpriteSheet spriteSheet, Player player,
EntityData entityData, EntityRepository entityRepository) {
super(mapCoordinates, spriteSheet, entityData, player, entityRepository);
target = mapCoordinates;
}
@Override
public EntityType getEntityType() {
return EntityType.PROJECTILE;
}
@Override
public void render(Graphics graphics, int x, int y) {
if (graphics == null) throw new IllegalArgumentException("Graphics must be not-null");
Image sprite = getSprite();
if (height > 0) {
int shadowX = x + Math.round(height / 8);
float shadowTransparancy = 0.5f;
shadowTransparancy -= (0.4f * (height/entityData.maxAscensionHeight));
shadowTransparancy = Math.max(shadowTransparancy, 0f);
sprite.setImageColor(0, 0, 0, shadowTransparancy); // set color of image to black, transparent
float shadowScale = 1.0f + (0.25f * (height/entityData.maxAscensionHeight));
float scale = 1.0f + (0.5f * (height/entityData.maxAscensionHeight));
graphics.setAntiAlias(true);
sprite.draw(shadowX, y, shadowScale);
graphics.setAntiAlias(false);
sprite.setImageColor(1, 1, 1, 1); // restore drawing to opaque
sprite.draw(x, (y - height), scale);
} else {
graphics.drawImage(sprite, x, (y - height));
}
}
public Image getSprite() {
return spritesheet.getSprite(getFacing(coordinate, target), 0);
}
public int getFacing(Vector2D from, Vector2D to) {
int facing = 0;
if (entityData.hasFacings() && !from.equals(to)) {
facing = UnitFacings.calculateFacingSpriteIndex(from, to, entityData.getFacings(), entityData.getChop());
}
return facing;
}
@Override
public void update(float deltaInSeconds) {
if (target != coordinate) {
float timeCorrectedSpeed = entityData.getRelativeMoveSpeed(deltaInSeconds);
Vector2D direction = target.min(coordinate);
Vector2D normalised = direction.normalise();
// make sure we don't overshoot
float distance = coordinate.distance(target);
if (distance < timeCorrectedSpeed) timeCorrectedSpeed = distance;
Vector2D delta = normalised.scale(timeCorrectedSpeed);
coordinate = coordinate.add(delta);
if (canAscendAndDescend()) {
if (shouldAscend()) {
ascend();
} else if (shouldDescend()) {
descend();
}
}
}
if (getCurrentDistanceToTarget() < 0.1F) {
if (entityData.hasExplosionId()) {
entityRepository.explodeAt(getCenteredCoordinate(), entityData, player);
}
// do damage on cell / range of cells
EntitiesSet entities = entityRepository.findAliveEntitiesOfTypeAtVector(coordinate, EntityType.UNIT, EntityType.STRUCTURE);
entities.each(new EntityHandler() {
@Override
public void handle(Entity entity) {
if (entity.isDestructible()) {
Destructible destructibleEntity = (Destructible) entity;
destructibleEntity.takeDamage(entityData.damage, origin);
}
}
});
destroyed = true;
}
}
public void descend() {
if (height > 0) {
float maxDescensionAtFlightPercentage = 1f - entityData.startToDescendPercentage;
float descendProgress = 1.0f - getFlightProgress();
float progress = descendProgress * (1 / maxDescensionAtFlightPercentage);
height = Math.min(entityData.maxAscensionHeight, progress * entityData.maxAscensionHeight);
if (height < 0) height = 0;
}
}
public boolean shouldDescend() {
return getFlightProgress() > entityData.startToDescendPercentage;
}
public void ascend() {
if (height < entityData.maxAscensionHeight) {
height = (getFlightProgress() * entityData.maxAscensionHeight) * (1 / entityData.maxAscensionAtFlightPercentage);
if (height >= entityData.maxAscensionHeight) height = entityData.maxAscensionHeight;
}
}
public boolean shouldAscend() {
return getFlightProgress() < entityData.maxAscensionAtFlightPercentage;
}
public boolean canAscendAndDescend() {
return entityData.maxAscensionHeight > 0;
}
public float getFlightProgress() {
return 1.0f - (getCurrentDistanceToTarget() / distanceCalculatedALaunch);
}
public float getCurrentDistanceToTarget() {
return target.distance(coordinate);
}
@Override
public void moveTo(Vector2D moveToTarget) {
Coordinate newTarget = new Coordinate(moveToTarget);
distanceCalculatedALaunch = target.distance(newTarget);
this.target = newTarget;
}
@Override
public void takeDamage(int hitPoints, Entity origin) {
// a projectile cannot take damage (yet?, we could somehow make it possible that projectiles can get intercepted?)
}
@Override
public boolean isDestroyed() {
return destroyed;
}
@Override
public int getHitPoints() {
return 0;
}
@Override
public String toString() {
return "Projectile{" +
"target=" + target +
", destroyed=" + destroyed +
'}';
}
public Coordinate getTarget() {
return target;
}
}