/
ChunkFactory.java
495 lines (440 loc) · 18.4 KB
/
ChunkFactory.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package leveldata;
import com.jme3.asset.AssetManager;
import com.jme3.light.Light;
import com.jme3.light.PointLight;
import com.jme3.light.SpotLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import spatial.MileStone;
import spatial.Plant;
import spatial.Platform;
import spatial.Platform.PType;
import spatial.PlayerInteractor;
import spatial.Torch;
import spatial.Wall;
import spatial.WindowFrame;
import spatial.hazard.BurstWizard;
import spatial.hazard.CalculatingWizard;
import spatial.hazard.LinearBat;
import spatial.hazard.SingleShotWizard;
import spatial.hazard.SpinningFireball;
import spatial.hazard.StationaryFireball;
import spatial.powerup.DoubleJumpPowerup;
import spatial.powerup.InvulnerabilityPowerup;
import spatial.powerup.SlowDownPowerup;
import spatial.powerup.SpeedPowerup;
import variables.P;
/**
* A class for generating chunks of the level. An instance of this class can
* take create a chunk with platforms, windows, enemies etc. Keeps some internal
* track of height and distance so that it doesn't place the enxt chunk out of
* reach from the previous one.
*
* This is the de facto level generator – the code for where each platform etc
* is placed can be found here.
*
* @author jonatankilhamn
*/
public class ChunkFactory {
private AssetManager assetManager;
private float height;
private float distanceOverFlow;
public ChunkFactory(AssetManager assetManager) {
this.assetManager = assetManager;
}
/**
* Generates a new chunk of the level. The generated content is delivered in
* a list of
* <code>Object</code>s. All such objects are either
* <code>Spatial</code>s or
* <code>Light</code>s. One of the spatials must have the name "background".
*
* @return A list of <code>Spatial</code>s and <code>Light</code>s.
*
*/
public List<Object> generateChunk(int level) {
if (level == 1) {
this.reset();
}
Random random = new Random();
LinkedList<Spatial> spatials = new LinkedList<Spatial>();
LinkedList<Light> lights = new LinkedList<Light>();
// Generate everything with a physical / game mechanical connection:
// standard length and distance
float totalLength = P.chunkLength;
float dist = P.platformDistance;
float d = distanceOverFlow;
distanceOverFlow = 0;
float windowHeight = Math.max(0, 5 * Math.round(height / 5));
int platformLayoutType;
int enemyType;
int powerupType;
int decorationType;
if (level < P.noOfStartingChunks) { //nothing special on the first few chunks
platformLayoutType = -1;
enemyType = -1;
powerupType = -1;
decorationType = 4;
} else {
/*results 0-7 are actual enemies; a 'roll' of 8 or higher
* will give nothing */
enemyType = random.nextInt(12);
/* results 0-3 are powerups; 4 or higher gives nothing */
powerupType = random.nextInt(8);
decorationType = random.nextInt(7);
// get back to normal height if we're too low or too high
if (height < -2) {
platformLayoutType = 2;
} else if (height > 5) {
platformLayoutType = 3;
} else {
platformLayoutType = random.nextInt(6);
}
}
PType pType;
float nDist;
switch (platformLayoutType) {
case (-1): // starting platform
while (d < totalLength) {
spatials.add(createPlatform(d, height, PType.LONG));
d += PType.LONG.length;
}
if (level == P.noOfStartingChunks-1) {
d += dist;
}
break;
case (0): // standard platforms
while (d < totalLength) {
spatials.add(createPlatform(d, height + random.nextFloat() * 3, PType.MEDIUM));
d += PType.MEDIUM.length + dist;
}
break;
case (1): // differentlength platforms
while (d < totalLength) {
if (random.nextBoolean()) {
pType = PType.SHORT;
} else {
pType = PType.MEDIUM;
}
nDist = dist * (1f + random.nextFloat());
spatials.add(createPlatform(d, height + random.nextFloat() * 3, pType));
d += pType.length + nDist;
}
break;
case (2): // climbing platforms
while (d < totalLength) {
if (random.nextBoolean()) {
pType = PType.SHORT;
} else {
pType = PType.MEDIUM;
}
nDist = dist * (0.6f + 0.5f * random.nextFloat());
height += 1 + 4 * random.nextFloat();
spatials.add(createPlatform(d, height + random.nextFloat() * 2, pType));
d += pType.length + nDist;
}
break;
case (3): // descending platforms
float descent;
while (d < totalLength) {
if (random.nextBoolean()) {
pType = PType.SHORT;
} else {
pType = PType.MEDIUM;
}
nDist = dist * (1f + random.nextFloat());
descent = 2 + 8 * random.nextFloat();
if (height - descent > P.deathTreshold + 2) {
height -= descent;
}
spatials.add(createPlatform(d, height + random.nextFloat() * 4, pType));
d += pType.length + nDist;
}
break;
case (4): // invulnerability only reachable with double-jump
height = -1;
spatials.add(createPlatform(d, height, PType.LONG));
d += PType.LONG.length + dist;
float nHeight = 5 + 3 * random.nextFloat();
spatials.add(createPlatform(d, height + nHeight, PType.SHORT));
spatials.add(createPlatform(d, height + nHeight - 13, PType.SHORT));
height += nHeight;
d += PType.SHORT.length + dist;
spatials.add(createInvulnerabilityPowerup(d + 5, height - 6));
height += 2 + 5 * random.nextFloat();
spatials.add(createPlatform(d, height, PType.SHORT));
d += PType.SHORT.length + 5;
height = -1;
if (d < totalLength) {
spatials.add(createPlatform(d, height, PType.MEDIUM));
d += PType.MEDIUM.length + dist;
}
powerupType = -1;
break;
case (5): // long platform with short platforms above
float d2 = d + PType.LONG.length/2; // start higher platforms a bit in
while (d < totalLength) {
spatials.add(createPlatform(d, height, PType.LONG));
d += PType.LONG.length;
}
d += dist;
height += 15;
windowHeight = height;
while (d2 < totalLength) {
spatials.add(createPlatform(d2, height,PType.SHORT));
spatials.add(createLinearBat(d2 + 40,height - 6));
d2 += PType.SHORT.length + 2*dist;
height += 1 + random.nextFloat();
}
height -= 20;
enemyType = -1;
default:
break;
}
// fill up with platforms if whatever was in the switch statement didn't already
while (d < totalLength) {
spatials.add(createPlatform(d, height, PType.SHORT));
height += random.nextInt(9) - 4;
d += PType.SHORT.length + dist;
}
// record this number so that the next chunk doesn't overlap
distanceOverFlow = d - totalLength;
// generate enemies:
SpotLight spotlight;
switch (enemyType) {
case (-1):
// no enemies;
break;
case (0):
case (1):
// single shot wizard
float wizardPosX = spatials.getLast().getLocalTranslation().getX()
+ 15;
float wizardPosY = spatials.getLast().getLocalTranslation().getY()
+ 18;
spotlight = new SpotLight();
if (P.useWizardLights) {
lights.add(spotlight);
}
spatials.add(createWizard(wizardPosX, wizardPosY, spotlight));
break;
case (2):
// burst wizard
wizardPosX = spatials.getLast().getLocalTranslation().getX()
+ 15;
wizardPosY = spatials.getLast().getLocalTranslation().getY()
+ 18;
spotlight = new SpotLight();
if (P.useWizardLights) {
lights.add(spotlight);
}
spatials.add(createBurstWizard(wizardPosX, wizardPosY, spotlight));
break;
case (3):
case (4):
// single bat
spatials.add(createLinearBat(d, height + 4 + random.nextFloat() * 2));
break;
case (5):
case (6):
// three bats
int temp1 = (random.nextInt(5) - 5) * 10; // 1st bat distance
spatials.add(createLinearBat(d + temp1, height + 7));
int temp2 = temp1;
while (temp2 == temp1) {
temp2 = (random.nextInt(5) - 5) * 10; // 2nd bat distance
}
spatials.add(createLinearBat(d + temp2, height + 13));
temp1 = temp2;
while (temp2 == temp1) {
temp1 = (random.nextInt(5) - 5) * 10; // 3rd bat distance
}
spatials.add(createLinearBat(d + temp1, height + 19));
break;
case (7):
// a wizard in the foreground shooting fireballs at where the player's going
spotlight = new SpotLight();
if (P.useWizardLights) {
lights.add(spotlight);
}
spatials.add(createCalculatingWizard(d, height, spotlight));
default:
// no enemies
break;
}
// generate powerups:
switch (powerupType) {
case (-1): // nothing
break;
case (0): // speed boost
spatials.add(createSpeedPowerup(d - 8, height + 5));
break;
case (1): // double jump
case (2):
spatials.add(createDoubleJumpPowerup(d - 3, height + 10));
break;
case (3):
spatials.add(createSlowDownPowerup(d - 15, height + 7));
break;
case (4):
spatials.add(createInvulnerabilityPowerup(d - 10, height + 6));
default:
break;
}
// Generate the background:
Node staticObjects = new Node("background");
// the wall:
Wall wall = new Wall(this.assetManager);
staticObjects.attachChild(wall);
// the decorations:
switch (decorationType) {
case (0): // window
case (1):
WindowFrame window = createWindowFrame(5f, windowHeight + 23f);
staticObjects.attachChild(window);
if (P.useWindowLights) {
lights.add(this.createWindowLight(5f, windowHeight + 23f));
}
break;
case (2): // plant
Plant plant = createPlant(20, windowHeight + 15);
staticObjects.attachChild(plant);
break;
case (3): // torch
case (4):
case (5):
Torch torch = createTorch(30, windowHeight + 15);
staticObjects.attachChild(torch);
if (P.useTorchLights) {
lights.add(this.createTorchLight(30f, windowHeight + 15f));
}
break;
default:
break;
}
if (level > 4 && level % 3 == 2) {
staticObjects.attachChild(createMileStone((int) (level - 4) / 3 + 1, 30f, windowHeight + 20f));
}
spatials.add(staticObjects);
LinkedList<Object> list = new LinkedList<Object>();
list.addAll(spatials);
list.addAll(lights);
return list;
}
public void reset() {
height = 0;
distanceOverFlow = 0;
}
/**
* Creates a platform at a given 2d position.
*/
private Platform createPlatform(float positionX, float positionY, PType type) {
Vector3f platformPos = new Vector3f(positionX, positionY, 0f);
return new Platform(this.assetManager, platformPos, type);
}
/* Creates a windowframe on the wall at a given position */
private WindowFrame createWindowFrame(float positionX, float positionY) {
Vector3f windowPos = new Vector3f(positionX, positionY, 0f);
WindowFrame window = new WindowFrame(this.assetManager, windowPos);
return window;
}
/* Creates a torch on the wall at a given psition */
private Torch createTorch(float positionX, float positionY) {
Vector3f windowPos = new Vector3f(positionX, positionY, 0f); //beware the Z is not used
Torch window = new Torch(this.assetManager, windowPos);
return window;
}
private Plant createPlant(float positionX, float positionY) {
Vector3f windowPos = new Vector3f(positionX, positionY, 0f); //beware the Z is not used
Plant window = new Plant(this.assetManager, windowPos);
return window;
}
/* Creates a spotlight shining through a window at a given position */
private Light createWindowLight(float positionX, float positionY) {
SpotLight windowLight = new SpotLight();
windowLight.setColor(new ColorRGBA(99 / 255f, 184 / 255f, 1f, 0f));
windowLight.setSpotOuterAngle(45f * FastMath.DEG_TO_RAD);
windowLight.setSpotInnerAngle(12f * FastMath.DEG_TO_RAD);
Vector3f windowPosition = new Vector3f(positionX, positionY + 8f, -P.platformWidth / 2 + 0.2f);
windowLight.setPosition(windowPosition);
windowLight.setDirection(P.windowLightDirection);
windowLight.setSpotRange(100f);
return windowLight;
}
/* Creates a pointlight, to use with a torch */
private Light createTorchLight(float positionX, float positionY) {
PointLight light = new PointLight();
light.setRadius(50);
light.setPosition(new Vector3f(positionX, positionY, -P.platformWidth / 2 + 0.2f));
light.setColor(new ColorRGBA(1f, 0.3f, 0f, 0f));
return light;
}
/* Creates a fireball hazard floating in the air.*/
private PlayerInteractor createHoveringFireball() {
PlayerInteractor hazard = new StationaryFireball(assetManager);
hazard.move(10f, 15f, 0f);
return hazard;
}
/* Creates a fireball hazard flying in a straight line.*/
private PlayerInteractor createLinearBat(float positionX, float positionY) {
PlayerInteractor hazard = new LinearBat(assetManager, new Vector3f(-15, 0, 0));
hazard.move(positionX + P.chunkLength, positionY, 0f);
return hazard;
}
/* Creates a fireball hazard flying in a counter-clockwise circle.*/
private PlayerInteractor createSpinningFireball() {
PlayerInteractor hazard = new SpinningFireball(assetManager);
hazard.move(5f, 6f, 0f);
return hazard;
}
/* Creates a wizard shooting fireballs at the player.*/
private PlayerInteractor createWizard(float positionX, float positionY, SpotLight spotlight) {
PlayerInteractor wizard = new SingleShotWizard(assetManager, spotlight);
wizard.move(positionX, positionY, 0f);
return wizard;
}
/* Creates a wizard shooting multiple fireballs at the player.*/
private PlayerInteractor createBurstWizard(float positionX, float positionY, SpotLight spotlight) {
PlayerInteractor wizard = new BurstWizard(assetManager, spotlight);
wizard.move(positionX, positionY, 0f);
return wizard;
}
/* Creates a wizard in the foreground, shooting fireballs ahead of the player.*/
private PlayerInteractor createCalculatingWizard(float positionX, float positionY, SpotLight spotlight) {
PlayerInteractor wizard = new CalculatingWizard(assetManager, spotlight);
wizard.move(positionX, positionY, 15f);
return wizard;
}
private PlayerInteractor createSpeedPowerup(float positionX, float positionY) {
SpeedPowerup speedPowerup = new SpeedPowerup(assetManager);
speedPowerup.move(positionX, positionY, 0f);
return speedPowerup;
}
private PlayerInteractor createDoubleJumpPowerup(float positionX, float positionY) {
DoubleJumpPowerup doubleJumpPowerup = new DoubleJumpPowerup(assetManager);
doubleJumpPowerup.move(positionX, positionY, 0f);
return doubleJumpPowerup;
}
private Spatial createSlowDownPowerup(float positionX, float positionY) {
SlowDownPowerup slowDownPowerup = new SlowDownPowerup(assetManager);
slowDownPowerup.move(positionX, positionY, 0f);
return slowDownPowerup;
}
private Spatial createInvulnerabilityPowerup(float positionX, float positionY) {
InvulnerabilityPowerup invulnerabilityPowerup =
new InvulnerabilityPowerup(assetManager);
invulnerabilityPowerup.move(positionX, positionY, 0f);
return invulnerabilityPowerup;
}
private Spatial createMileStone(int progress, float positionX, float positionY) {
MileStone mileStone = new MileStone(assetManager, progress);
mileStone.setLocalTranslation(positionX, positionY, 0f);
return mileStone;
}
}