This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.pde
110 lines (86 loc) · 2.13 KB
/
Player.pde
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
class Player {
private static final int INITIAL_HP = 20;
public static final int TOP_TEXT_MARGIN = 23;
private Game game;
private int hp = INITIAL_HP;
private int gold;
private TowerGroups towers;
private Group<Guard> guards;
Player(int initialGold, Game game) {
this.gold = initialGold;
this.game = game;
this.towers = new TowerGroups(game);
this.guards = new Group(game);
}
public TowerGroups getTowers() {
return towers;
}
public Group<Guard> getGuards() {
return guards;
}
public void drawStats() {
fill(Utils.FADED_RED);
_topText(hp + "HP", 10);
fill(Utils.BANANA);
_topText(gold + "G", 60);
}
public void spendGold(int cost) throws InsufficientGoldException {
if (gold - cost < 0) {
throw new InsufficientGoldException();
}
gold -= cost;
}
public void buyTower(int type, Node node) {
try {
node.assertVacancy();
spendGold(TowerCost.calc(type));
} catch (InsufficientGoldException e) {
println("Can't buy that.");
return;
} catch (NodeIsOccupiedException e) {
println("Node is occupied.");
return;
}
switch (type) {
case Tower.ARROW:
new ArrowTower(type, node, this, game);
break;
case Tower.ICE:
new IceTower(type, node, this, game);
break;
default:
println("Type number " + type + " does not map to a valid tower type.");
}
}
public void receiveDmg() {
hp = hp - 1 > 0
? hp - 1
: 0;
}
public boolean isAlive() {
return hp > 0;
}
public int getHp() {
return hp;
}
public void gainGold(int amount) {
gold += amount;
}
private void _topText(String text, int x) {
textSize(32);
text(text, x, TOP_TEXT_MARGIN);
}
public void addToGuards(Guard guard) {
guards.add(guard);
}
public void addToTowers(GuardTower tower) {
towers.add(tower);
}
public void addToTowers(IceTower tower) {
towers.add(tower);
}
public void addToTowers(ArrowTower tower) {
towers.add(tower);
}
}
class InsufficientGoldException extends Exception {}