-
Notifications
You must be signed in to change notification settings - Fork 0
/
Turret.java
86 lines (64 loc) · 1.88 KB
/
Turret.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
import java.util.ArrayList;
import java.util.List;
public abstract class Turret implements Purchasable {
private SquareCoordinate position;
private int cooldown;
private List<Upgrade> upgrades = new ArrayList<Upgrade>();
public Turret(SquareCoordinate position) {
this(position, 1);
}
public Turret(SquareCoordinate position, int cooldown) {
this.position = position;
this.cooldown = cooldown;
}
public Projectile fire(List<Enemy> enemies, long tick) {
if (tick % getCooldown() == 0)
return getProjectile(enemies);
return null;
}
public abstract Projectile getProjectile(List<Enemy> enemies);
public int getX() {
return position.getX();
}
public int getY() {
return position.getY();
}
public int getDamage() {
return 1 + numberOfDamageUpgrades();
}
public int getCooldown() {
return Math.max(cooldown - numberOfCooldownUpgrades() * 10, 1);
}
private int numberOfTypeUpgrade(Class<? extends Upgrade> clazz) {
int count = 0;
for (Upgrade upgrade : upgrades)
if (upgrade.getClass() == clazz)
count++;
return count;
}
public int numberOfDamageUpgrades() {
return numberOfTypeUpgrade(DamageUpgrade.class);
}
public int numberOfCooldownUpgrades() {
return numberOfTypeUpgrade(CooldownUpgrade.class);
}
public SquareCoordinate getPosition() {
return position;
}
public int getValue() {
return getBasePrice() + getUpgradesValue();
}
public boolean addUpgrade(Upgrade upgrade) throws PurchaseException {
if (upgrade instanceof CooldownUpgrade && numberOfCooldownUpgrades() >= 4)
throw new PurchaseException("Max cooldown");
if (upgrade instanceof DamageUpgrade && numberOfDamageUpgrades() >= 4)
throw new PurchaseException("Max damage");
return upgrades.add(upgrade);
}
private int getUpgradesValue() {
int value = 0;
for (Upgrade upgrade : upgrades)
value += upgrade.getValue();
return value;
}
}