-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewCharacterState.pde
261 lines (233 loc) · 7.02 KB
/
NewCharacterState.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
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
/*
* 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.Map;
import java.util.TreeMap;
static enum Typing {
START, STOP, CLEAR
}
class NewCharacterState extends GameState {
private static final String NEXT_LABEL = "Next";
private final String title = "New Character";
private final Map<String, Race> races = new TreeMap<String, Race>();
private final Map<String, Typing> typing = new TreeMap<String, Typing>();
// Race selection is done with buttons
private final CharacterOption[] options;
// This state creates the player
private Race race = null;
private String name = "";
private boolean isTyping = false;
public NewCharacterState() {
options = new CharacterOption[] {
makeRaceOption(),
makeNameOption(),
new CharacterOption("Done?", new String[] { NEXT_LABEL})
};
}
private CharacterOption makeRaceOption() {
// Select the races to be used
races.put("Human", Race.HUMAN);
races.put("Dwarf", Race.DWARF);
races.put("Elf", Race.ELF);
// Get the array of button labels
final String[] choices = races.keySet().toArray(new String[0]);
// Make them into a character option choice
return new CharacterOption("Pick a race:", choices);
}
private CharacterOption makeNameOption() {
// Select the typing options
typing.put("Start typing", Typing.START);
typing.put("Stop typing", Typing.STOP);
typing.put("Clear typing", Typing.CLEAR);
// Make the choices into character options
final String[] choices = typing.keySet().toArray(new String[0]);
return new CharacterOption("Name your character:", choices);
}
public void onEnter(final Game game) {
}
public void input(final Game game, final char key) {
if (isTyping) {
if (key == ENTER || key == RETURN) {
// Ignore certain keys
} else if (key == BACKSPACE && name.length() > 0) {
// Remove the last letter
name = name.substring(0, name.length() - 1);
} else {
// Just add the character to the name
name += key;
}
}
}
public void click(final Game game, final PVector position) {
for (final CharacterOption option : options) {
// Get the user's choice
final String choice = option.clicked(position);
// Was it for this option?
if (choice == null) {
continue;
}
// Are we dealing with a character race choice?
if (races.get(choice) != null) {
// Pick the race
race = races.get(choice);
break;
}
// Is it for the name?
if (typing.get(choice) != null) {
// Set the typing state
nameAction(typing.get(choice));
break;
}
// Is the character ready?
if (choice == NEXT_LABEL && isReady()) {
// Make the character
// Health = 3D8 + CON
final int health = Dice.D8() + Dice.D8() + Dice.D8() + race.base.CON();
final Actor player = new Actor(name, race, health);
game.setPlayer(player);
// Give it some starting items
final Item[] items = new Item[] {
getItem("Iron Sword"),
getItem("Chain Mail")
};
for (final Item item : items) {
player.inventory.addItem(item);
player.inventory.equipItem(item);
}
// Start exploring
nextState = ExploreState.class;
break;
}
}
}
private boolean isReady() {
return race != null && name.length() > 0;
}
private void nameAction(final Typing choice) {
switch (choice) {
case START:
isTyping = true;
break;
case STOP:
isTyping = false;
break;
case CLEAR:
name = "";
break;
}
}
public void update(final Game game) {
}
public void draw(final Game game) {
// Draw in white
fill(255);
// Use standard spacing
float off_x = 50;
float off_y = 50;
// Draw the page title
off_y = drawTitle(off_y);
// Display the character name
off_y = drawName(off_x, off_y);
// Display the current character stats
off_y = drawStats(off_x, off_y);
// Draw the available options
for (final CharacterOption option : options) {
option.draw(new PVector(off_x, off_y));
// Draw options in columns beside each other
off_x += option.optionWidth() + 50;
}
}
private float drawTitle(final float offset) {
textAlign(CENTER, TOP);
textSize(FONT_LARGE);
text(title, displayWidth / 2, offset);
return offset + FONT_LARGE * 2;
}
private float drawName(final float off_x, final float off_y) {
textAlign(LEFT, TOP);
textSize(FONT_MEDIUM);
text("Name: " + name, off_x, off_y);
return off_y + FONT_MEDIUM * 3;
}
private float drawStats(float off_x, float off_y) {
textAlign(TOP, LEFT);
textSize(FONT_MEDIUM);
text("STR: " + (race == null ? "" : race.base.STR()),
off_x, off_y);
off_x += 100;
text("DEX: " + (race == null ? "" : race.base.DEX()),
off_x, off_y);
off_x += 100;
text("CON: " + (race == null ? "" : race.base.CON()),
off_x, off_y);
off_x += 100;
text("Speed: " + (race == null ? "" : race.size.speed),
off_x, off_y);
return off_y + FONT_MEDIUM * 1.5;
}
public void resetTransition() {
nextState = NewCharacterState.class;
}
}
class CharacterOption {
final String label;
final String[] choices;
Button[] buttons;
public CharacterOption(final String label, final String[] choices) {
this.label = label;
this.choices = choices;
buttons = new Button[choices.length];
}
public float optionWidth() {
// Find the widest element
textSize(FONT_MEDIUM);
float max = textWidth(label);
for (final Button button : buttons) {
if (button != null) {
final float buttonWidth = textWidth(button.label);
max = Math.max(max, buttonWidth);
}
}
return max;
}
public void draw(final PVector pos) {
// Create the buttons the first time
if (buttons[0] == null) {
makeButtons(pos);
}
fill(255);
textAlign(LEFT, TOP);
textSize(FONT_MEDIUM);
// Draw the label
text(label, pos.x, pos.y);
// Draw the buttons
for (final Button button : buttons) {
button.draw();
}
}
private void makeButtons(final PVector pos) {
float offset = pos.y;
for (int i = 0; i < buttons.length; ++i) {
// Place the buttons in a column
offset += FONT_MEDIUM * 2;
final PVector position = new PVector(pos.x, offset);
buttons[i] = new Button(choices[i], position, FONT_MEDIUM);
}
}
public String clicked(final PVector pos) {
// If the buttons don't exist for some reason, do nothing
if (buttons[0] == null) {
return null;
}
// Check each button for being clicked
for (final Button button : buttons) {
if (button.over(pos)) {
return button.label;
}
}
return null;
}
}