/
MobSpawner.java
78 lines (63 loc) · 2.76 KB
/
MobSpawner.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
package com.codingame.game;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MobSpawner {
private Random random;
private SpawnLocation[] spawnLocations;
private double maxDirectionDelta;
private int spawnRate;
private int lastSpawn;
private double currentMaxHealth = Configuration.MOB_STARTING_MAX_ENERGY;
public MobSpawner(Random random, SpawnLocation[] spawnLocations, double maxDirectionDelta, int spawnRate) {
this.random = random;
this.spawnLocations = spawnLocations;
this.maxDirectionDelta = maxDirectionDelta;
this.spawnRate = spawnRate;
lastSpawn = -spawnRate;
}
List<Mob> update(int turn) {
// It's the end of the game, help the random generate a suitably epic final frame
boolean suddenDeath = turn >= 200;
if (turn - lastSpawn >= spawnRate) {
lastSpawn = turn;
return spawn(suddenDeath);
}
return Collections.emptyList();
}
private Vector opposite(Vector v) {
return new Vector(Configuration.MAP_WIDTH - v.getX(), Configuration.MAP_HEIGHT - v.getY());
}
private List<Mob> spawn(boolean suddenDeath) {
List<Mob> newMobs = new ArrayList<>();
for (SpawnLocation pairToUse : spawnLocations) {
Vector suddenDeathTarget = null;
if (suddenDeath) {
int tx = random.nextInt(Configuration.BASE_ATTRACTION_RADIUS);
int ty = random.nextInt(Configuration.BASE_ATTRACTION_RADIUS);
if (random.nextBoolean()) {
tx = Configuration.MAP_WIDTH - tx;
ty = Configuration.MAP_HEIGHT - ty;
}
suddenDeathTarget = new Vector(tx, ty);
}
double directionDelta = random.nextDouble() * maxDirectionDelta * 2 - maxDirectionDelta;
for (int i = 0; i < 2; ++i) {
Vector location = i == 0 ? pairToUse.position : pairToUse.symetry;
Vector direction = i == 0 ? pairToUse.direction : pairToUse.direction.symmetric();
Mob mob = new Mob(location, (int) currentMaxHealth);
if (suddenDeath) {
Vector v = new Vector(location, i == 0 ? suddenDeathTarget : opposite(suddenDeathTarget)).normalize()
.mult(Configuration.MOB_MOVE_SPEED).truncate();
mob.setSpeed(v);
} else {
mob.setSpeed(direction.rotate(directionDelta).normalize().mult(Configuration.MOB_MOVE_SPEED).truncate());
}
newMobs.add(mob);
}
}
currentMaxHealth += Configuration.MOB_GROWTH_MAX_ENERGY;
return newMobs;
}
}