-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombatState.pde
218 lines (196 loc) · 6.2 KB
/
CombatState.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
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
/*
* Copyright 2017 Billy Brown
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import java.util.LinkedList;
import java.util.List;
class CombatState extends GameState {
private Actor opponent = null;
private boolean turn; // Is it the player's turn?
private Button[] buttons = null;
private List<String> messages;
public void onEnter(final Game game) {
opponent = game.opponent;
game.opponent = null;
turn = true;
messages = new LinkedList<String>();
}
public void input(final Game game, final char key) {
if (opponent == null && key == ' ') {
nextState = ExploreState.class;
}
}
public void click(final Game game, final PVector position) {
if (!turn) {
return;
}
final Actor player = game.getPlayer();
for (final Button b : buttons) {
if (b.over(position)) {
switch (b.label) {
case "Attack":
attack(game, player, opponent);
break;
case "Flee":
actorFlees(player, opponent);
break;
default:
if (b.label.startsWith("Drink Potion")) {
// Drink a potion if one is available
final List<Item> potions = player.getPotions();
if (potions.size() > 0) {
player.takePotion(potions.get(0));
}
}
break;
}
}
}
}
private void addMessage(final String message) {
if (messages.size() >= 10) {
// Remove the last message
messages.remove(messages.size() - 1);
}
// Add the message to the beginning
messages.add(0, message);
}
private void actorFlees(final Actor actor, final Actor other) {
// The actor flees
actor.flee = true;
// The actor is invulnerable for a second
actor.invuln = millis() + 1000;
// Remove the actor's current path
actor.stop();
// Return to the explore state
nextState = ExploreState.class;
}
private void attack(final Game game, final Actor from, final Actor to) {
// 1d20 + attack vs defence
final int att = Dice.D20() + from.getAttack();
addMessage(from.name + " attacks: " + att);
if (att >= to.getDefence()) {
addMessage(from.name + " hits!");
// 1d8 + attack/2 damage
final int dmg = Dice.D8() + from.getAttack() / 2;
addMessage(from.name + " deals " + dmg + " damage.");
to.damage(dmg);
// If the target was killed
if (!to.alive()) {
addMessage(to.name + " died...");
if (to == game.getPlayer()) {
// The player dies
nextState = ExploreState.class;
return;
} else {
// The opponent died
game.getPlayer().gainXP(10 * opponent.getAttack() + opponent.getDefence());
opponent = null;
}
game.removeActor(to);
}
} else {
addMessage(from.name + " misses...");
}
// End the turn
turn = !turn;
}
public void update(final Game game) {
// Opponent's turn
if (!turn && opponent != null) {
// Opponent flees if it doesn't have much health and is losing
if (opponent.getHealth() <= 5 && game.getPlayer().getHealth() > 5) {
actorFlees(opponent, game.getPlayer());
// Get a bit of XP
game.getPlayer().gainXP(5 * opponent.getAttack());
opponent = null;
} else {
attack(game, opponent, game.getPlayer());
}
}
}
public void draw(final Game game) {
// Draw in white
fill(255);
final int padding = 50;
int offset = 30;
offset = drawTitle(offset);
drawMessages(offset);
drawOpponent(padding, offset);
offset = drawPlayer(game, padding, offset);
if (buttons == null) {
makeButtons(new PVector(padding, offset));
}
for (final Button b : buttons) {
if (b.label.startsWith("Drink Potion")) {
b.label = "Drink Potion (" + game.getPlayer().getPotions().size() + ")";
}
b.draw();
}
textAlign(CENTER, BOTTOM);
textSize(FONT_MEDIUM);
if (opponent == null) {
text("You win, press SPACE to continue.",
displayWidth / 2,
displayHeight - 20);
} else {
text("Turn: " + (turn ? game.getPlayer().name : opponent.name),
displayWidth / 2,
displayHeight - 20);
}
}
private int drawTitle(final int offset) {
textAlign(CENTER, TOP);
textSize(FONT_LARGE);
text("Combat", displayWidth / 2, offset);
return offset + FONT_LARGE * 2;
}
private void drawMessages(int offset) {
textAlign(CENTER, TOP);
textSize(FONT_MEDIUM);
for (final String message : messages) {
text(message, displayWidth / 2, offset);
offset += FONT_MEDIUM * 2;
}
}
private int drawPlayer(final Game game, final int padding, int offset) {
textAlign(LEFT, TOP);
textSize(FONT_MEDIUM);
final Actor player = game.getPlayer();
offset = drawActor(player, padding, offset);
// Draw the player's attack and defence values
text("Attack: +" + player.getAttack(), padding, offset);
offset += FONT_MEDIUM * 2;
text("Defence: " + player.getDefence(), padding, offset);
offset += FONT_MEDIUM * 2;
return offset;
}
private void drawOpponent(final int padding, int offset) {
if (opponent == null) {
return;
}
textAlign(RIGHT, TOP);
textSize(FONT_MEDIUM);
offset = drawActor(opponent, displayWidth - padding, offset);
}
private int drawActor(final Actor actor, final int padding, int offset) {
text("Name: " + actor.name, padding, offset);
offset += FONT_MEDIUM * 2;
text("Health: " + actor.getHealth() + "/" + actor.getMaxHealth(), padding, offset);
offset += FONT_MEDIUM * 2;
return offset;
}
public void resetTransition() {
nextState = CombatState.class;
}
private void makeButtons(final PVector position) {
buttons = new Button[3];
buttons[0] = new Button("Attack", position, FONT_MEDIUM);
position.y += FONT_MEDIUM * 2;
buttons[1] = new Button("Flee", position, FONT_MEDIUM);
position.y += FONT_MEDIUM * 2;
buttons[2] = new Button("Drink Potion", position, FONT_MEDIUM);
}
}