/
EntitiesData.java
403 lines (348 loc) · 14.8 KB
/
EntitiesData.java
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
package com.fundynamic.d2tm.game.entities.entitiesdata;
import com.fundynamic.d2tm.game.entities.entitiesdata.ini.*;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.entities.EntityNotFoundException;
import com.fundynamic.d2tm.game.entities.EntityType;
import com.fundynamic.d2tm.game.types.SoundData;
import com.fundynamic.d2tm.utils.StringUtils;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import static com.fundynamic.d2tm.game.map.Cell.TILE_SIZE;
/**
* A collection of all the blue-prints of all the entities within the game. Usually read from a resource
* like rules.ini by the {@link EntitiesDataReader}
*/
public class EntitiesData {
public static final String UNKNOWN = "UNKNOWN";
public static final String DEATHHAND = "DEATHHAND";
// units
/**
* ID of TRIKE unit
*/
public static String TRIKE = "TRIKE";
/**
* ID of QUAD unit
*/
public static String QUAD = "QUAD";
/**
* ID of TANK unit
*/
public static String TANK = "TANK";
/**
* ID of HARVESTER
*/
public static final String HARVESTER = "HARVESTER";
/**
* ID of SOLDIER
*/
public static String SOLDIER = "SOLDIER";
/**
* ID of INFANTRY
*/
public static String INFANTRY = "INFANTRY";
// structures
public static String CONSTRUCTION_YARD = "CONSTYARD";
public static String REFINERY = "REFINERY";
public static String WINDTRAP = "WINDTRAP";
public static String LIGHT_FACTORY = "LIGHTFACTORY";
// projectiles
public static String BULLET = "RIFLE";
/**
* ID of large rocket
*/
public static final String LARGE_ROCKET = "LARGE_ROCKET";
// explosions
public static String EXPLOSION_SMALL_UNIT = "WHEELED";
private HashMap<String, EntityData> entitiesData;
private HashMap<String, SoundData> soundsData;
public EntitiesData() {
entitiesData = new HashMap<>();
soundsData = new HashMap<>();
}
public void addSound(String id, String path) throws SlickException {
SoundData soundData = new SoundData();
soundData.sound = loadSound(path);
soundsData.put(id, soundData);
}
public void addProjectile(String id, IniDataWeapon iniDataWeapon) throws SlickException {
EntityData entityData = createEntity(
id,
iniDataWeapon.image,
null,
iniDataWeapon.widthInPixels,
iniDataWeapon.heightInPixels,
EntityType.PROJECTILE,
-1,
iniDataWeapon.moveSpeed,
-1
);
entityData.damage = iniDataWeapon.damage;
entityData.explosionId = iniDataWeapon.explosionId;
entityData.soundData = getAndEnsureSoundId(iniDataWeapon.soundId, id); // for launching projectile?
entityData.maxAscensionHeight = iniDataWeapon.ascendTo;
entityData.maxAscensionAtFlightPercentage = iniDataWeapon.ascendAt;
entityData.startToDescendPercentage = iniDataWeapon.descendAt;
entityData.setFacingsAndCalculateChops(iniDataWeapon.facings);
}
public void addSuperPower(String id, IniDataSuperPower iniDataSuperPower) {
if (tryGetEntityData(EntityType.SUPERPOWER, id)) {
throw new IllegalArgumentException("Entity of type " + EntityType.SUPERPOWER + " already exists with id " + id + ". Known entities are:\n" + entitiesData);
}
try {
EntityData entityData = new EntityData();
entityData.key = EntityData.constructKey(EntityType.SUPERPOWER, id);
entityData.name = id;
entityData.type = EntityType.SUPERPOWER;
entityData.buildIcon = loadImage(iniDataSuperPower.buildIcon);
entityData.buildCost = iniDataSuperPower.buildCost;
entityData.buildTimeInSeconds = iniDataSuperPower.buildTimeInSeconds;
entityData.weaponId = getAndEnsureWeaponId(iniDataSuperPower.weaponId, id);
// TODO: Do a two step building of entityData
// 1) first read
// 2) then link (and check), because we get into circular references, ie Explosions vs Super powers and such
if (idProvided(iniDataSuperPower.explosionId)) {
if (!tryGetEntityData(EntityType.PARTICLE, iniDataSuperPower.explosionId)) {
throw new IllegalArgumentException("super power " + id + " [explosion] refers to non-existing [EXPLOSIONS/" + iniDataSuperPower.explosionId + "]");
}
entityData.explosionId = iniDataSuperPower.explosionId;
}
entitiesData.put(entityData.key, entityData);
} catch (SlickException e) {
throw new IllegalArgumentException("Unable to load image: ", e);
}
}
/**
* Create and add particle to this collection.
*
* @param id
* @param iniDataExplosion
* @throws SlickException
*/
public void addParticle(String id, IniDataExplosion iniDataExplosion) throws SlickException {
EntityData entityData = createEntity(
id,
iniDataExplosion.image,
null,
iniDataExplosion.widthInPixels,
iniDataExplosion.heightInPixels,
EntityType.PARTICLE,
-1,
-1,
-1
);
entityData.soundData = getAndEnsureSoundId(iniDataExplosion.soundId, id);
entityData.animationSpeed = iniDataExplosion.framesPerSecond;
entityData.recolor = iniDataExplosion.recolor;
}
private SoundData getAndEnsureSoundId(String soundId, String id) {
if (!idProvided(soundId)) {
return null;
}
if (!soundsData.containsKey(soundId)) {
throw new IllegalArgumentException("entity " + id + " property [Sound] refers to non-existing [SOUNDS/" + soundId + "]");
}
return soundsData.get(soundId);
}
/**
* Create and add a Structure EntityData to this collection.
*
* @throws SlickException
*/
public EntityData addStructure(String id, IniDataStructure iniDataStructure) throws SlickException {
EntityData entityData = createEntity(
id,
iniDataStructure.image,
null,
iniDataStructure.width,
iniDataStructure.height,
EntityType.STRUCTURE,
iniDataStructure.sight,
0F,
iniDataStructure.hitpoints
);
// The buildRange is (for now) determined by the size of the structure.
// Because the range is calculated from the center of the structure.
// In order to make it 'fair' for larger structures (if any would appear),
// we add 'half' of the structure to the range.
//
// For a constyard it is a square, so 64x64 pixels = 1 'half', meaning:
//
// (64/64)/2 = 0,5 * 32 (tile width) = 16 pixels
//
// A larger structure would be (width/height), ie a heavy factory thing would be:
// (96/64)/2 = ,75 * 32 = 24 pixels.
//
// Although on every squared structure this would even out fine, but then the
// 'buildRange' should have one tile extra because (again) it is calculated
// from the center
//
// this is all weird , then again, fixing this would require to check for every cell
// on the structure which basically makes calculating the 'can I place it in this distance' logic
// Width*height times more consuming.
//
// Unless....
//
// We do the 'computed map' thing (where we have all entity data on a map attached), so we don't
// need to do an expensive lookup in the EntityRepository
//TODO: Do something about the above, for now accept its quirks
float someRatio = (float)entityData.getWidth() / (float)entityData.getHeight();
int extraFromCenter = (int)((someRatio / 2) * TILE_SIZE); // this is seriously flawed :/ (too tired to fix now)
// add additional '1' to get 'past the center and occupy one cell'.
entityData.buildRange = extraFromCenter + ((1 + iniDataStructure.buildRangeInTiles) * TILE_SIZE);
entityData.entityBuilderType = iniDataStructure.getEntityBuilderType();
entityData.buildTimeInSeconds = iniDataStructure.buildTimeInSeconds;
entityData.buildList = iniDataStructure.buildList;
entityData.buildCost = iniDataStructure.buildCost;
if (idProvided(iniDataStructure.explosion)) {
if (!tryGetEntityData(EntityType.PARTICLE, iniDataStructure.explosion)) {
throw new IllegalArgumentException("structure " + id + " [explosion] refers to non-existing [EXPLOSIONS/" + iniDataStructure.explosion + "]");
}
entityData.explosionId = iniDataStructure.explosion;
}
entityData.buildIcon = loadImage(iniDataStructure.buildIcon);
return entityData;
}
public EntityData addUnit(String id, IniDataUnit iniDataUnit) throws SlickException {
EntityData entityData = createEntity(
id,
iniDataUnit.pathToImage,
iniDataUnit.pathToBarrelImage,
iniDataUnit.width,
iniDataUnit.height,
EntityType.UNIT,
iniDataUnit.sight,
iniDataUnit.moveSpeed,
iniDataUnit.hitPoints
);
entityData.attackRate = iniDataUnit.attackRate;
entityData.attackRange = iniDataUnit.attackRange;
entityData.turnSpeed = iniDataUnit.turnSpeed;
entityData.turnSpeedCannon = iniDataUnit.turnSpeedCannon;
entityData.animationSpeed = iniDataUnit.animationSpeed;
entityData.buildTimeInSeconds = iniDataUnit.buildTimeInSeconds;
entityData.buildIcon = loadImage(iniDataUnit.buildIcon);
entityData.buildCost = iniDataUnit.buildCost;
entityData.name = id;
// NOOOOOO
// TODO: Add 'harvester' property to unit in INI file (https://github.com/Fundynamic/dune2themaker4j/issues/156)
if (id.equals(EntitiesData.HARVESTER)) {
entityData.isHarvester = true;
}
entityData.weaponId = getAndEnsureWeaponId(iniDataUnit.weaponId, id);
String explosionId = iniDataUnit.explosionId;
if (idProvided(explosionId)) {
if (!tryGetEntityData(EntityType.PARTICLE, explosionId)) {
throw new IllegalArgumentException("unit " + id + " [explosion] refers to non-existing [EXPLOSIONS/" + explosionId + "]");
}
entityData.explosionId = explosionId;
}
return entityData;
}
public String getAndEnsureWeaponId(String weaponId, String id) {
if (idProvided(weaponId)) {
if (!tryGetEntityData(EntityType.PROJECTILE, weaponId)) {
throw new IllegalArgumentException("entity " + id + " property [Weapon] refers to non-existing [WEAPONS/" + weaponId + "]");
}
}
return weaponId;
}
public boolean idProvided(String id) {
return !UNKNOWN.equals(id);
}
private EntityData createEntity(String id, String pathToImage, String pathToBarrelImage, int widthInPixels, int heightInPixels, EntityType entityType, int sight, float moveSpeed, int hitPoints) throws SlickException {
if (tryGetEntityData(entityType, id)) {
throw new IllegalArgumentException("Entity of type " + entityType + " already exists with id " + id + ". Known entities are:\n" + entitiesData);
}
EntityData entityData = new EntityData();
entityData.image = loadImage(pathToImage);
entityData.barrelImage = loadImage(pathToBarrelImage);
entityData.setWidth(widthInPixels);
entityData.setHeight(heightInPixels);
entityData.type = entityType;
entityData.sight = sight;
entityData.moveSpeed = moveSpeed;
entityData.hitPoints = hitPoints;
entityData.key = EntityData.constructKey(entityType, id);
entityData.name = id;
entitiesData.put(entityData.key, entityData);
return entityData;
}
/**
* Loads image from path and returns a Slick image object. Returns null when path to image is null or empty string.
* @param pathToImage
* @return
* @throws SlickException
*/
protected Image loadImage(String pathToImage) throws SlickException {
if (StringUtils.isEmpty(pathToImage)) {
return null;
}
return createSlickImage(pathToImage);
}
protected Sound loadSound(String path) throws SlickException {
if (StringUtils.isEmpty(path)) {
return null;
}
return new Sound(path);
}
/**
* Creates a Slick Image.
*
* TODO: Use imageRepository, as we can use this for all base images. When an entity is created by the
* entity repository then there a copy should be made and recolored, there we can also cache those things
* based on image-name + color.
* @param pathToImage
* @return
* @throws SlickException
*/
protected Image createSlickImage(String pathToImage) throws SlickException {
System.err.println("Reading " + pathToImage);
return new Image(pathToImage);
}
public EntityData getParticle(String id) {
return getEntityData(EntityType.PARTICLE, id);
}
public EntityData getEntityData(EntityType entityType, String id) {
return getEntityData(EntityData.constructKey(entityType, id));
}
public EntityData getEntityData(String key) {
EntityData entityData = entitiesData.get(key);
if (entityData == null) throw new EntityNotFoundException("Entity not found for key " + key + ". Known entities are:\n" + toString());
return entityData;
}
private boolean tryGetEntityData(EntityType entityType, String id) {
try {
getEntityData(entityType, id);
return true;
} catch (EntityNotFoundException e) {
// System.err.println(e);
return false;
}
}
public void clear() {
this.entitiesData.clear();
}
public boolean isEmpty() {
return this.entitiesData.isEmpty();
}
@Override
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.
append("--- EntitiesData ---\n");
for (String key : entitiesData.keySet()) {
stringBuffer.append(key + "=" + entitiesData.get(key) + "\n");
}
stringBuffer.append("--- EntitiesData ---\n");
return stringBuffer.toString();
}
public List<SoundData> getSounds() {
Collection<SoundData> values = soundsData.values();
return new ArrayList<>(values);
}
}