-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuState.pde
163 lines (146 loc) · 4.12 KB
/
MenuState.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
/*
* 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;
enum MenuInput {
SELECT, UP, DOWN
}
class MenuState extends GameState {
private final int fontSize = 48;
private int selected = 0;
private final StateTransitionButton[] options = new StateTransitionButton[] {
new StateTransitionButton("Play", fontSize, BuildState.class),
new StateTransitionButton("Help", fontSize, HelpState.class),
new StateTransitionButton("Credits", fontSize, CreditState.class)
};
public void click(final float x, final float y) {
for (final StateTransitionButton option : options) {
if (option.over(x, y)) {
nextAction = GameState.Action.ENTER;
nextState = option.next;
}
}
}
public void input(final char key) {
MenuInput action;
if (key == CODED) {
switch (keyCode) {
case UP:
action = MenuInput.UP;
codes.add(Konami.UP);
break;
case DOWN:
action = MenuInput.DOWN;
codes.add(Konami.DOWN);
break;
case LEFT:
codes.add(Konami.LEFT);
return;
case RIGHT:
codes.add(Konami.RIGHT);
return;
default:
return;
}
} else {
switch (key) {
case ENTER: case RETURN:
action = MenuInput.SELECT;
break;
case 'w':
action = MenuInput.UP;
break;
case 's':
action = MenuInput.DOWN;
break;
case 'a':
codes.add(Konami.A);
return;
case 'b':
codes.add(Konami.B);
return;
default:
return;
}
}
switch (action) {
case SELECT:
nextAction = GameState.Action.ENTER;
nextState = options[selected].next;
break;
case UP:
selected = (selected + options.length - 1) % options.length;
break;
case DOWN:
selected = (selected + 1) % options.length;
break;
}
}
public void draw() {
final int colour = 255;
drawBackground();
PVector offset = new PVector(width / 2, 50);
textAlign(CENTER, TOP);
fill(0); printLine("City Mayor", fontSize * 1.5, offset.get().add(new PVector(0, 2))); // shadow
fill(colour);
printLine("City Mayor", fontSize * 1.5, offset);
// Display the options
textAlign(LEFT, TOP);
textSize(fontSize);
offset.x -= 160;
offset.y = 300;
for (int i = 0; i < options.length; ++i) {
// Select the option that the mouse is over
if (options[i].over(offset.get(), mouseX, mouseY)) {
selected = i;
}
// The selected option has a different colour
fill(0); options[i].draw(offset.get().add(new PVector(0, 2))); // shadow
if (i == selected) {
fill(80, 220, 255);
} else {
fill(colour);
}
options[i].draw(offset.get());
offset.y += 64;
}
checkKonamiCode();
}
private PImage bg = null;
private float ratio = 0f;
private void drawBackground() {
if (bg == null) {
bg = images.getImage("background.jpg");
ratio = max(width / (float) bg.width, height / (float) bg.height);
bg.resize((int) (bg.width * ratio), (int) (bg.height * ratio));
}
image(bg, 0, 0, bg.width, bg.height);
}
public void resetTransition() {
nextAction = GameState.Action.NONE;
nextState = MenuState.class;
}
List<Konami> codes = new LinkedList<Konami>();
final Konami[] expected = new Konami[] {
Konami.UP, Konami.UP, Konami.DOWN, Konami.DOWN,
Konami.LEFT, Konami.RIGHT, Konami.LEFT, Konami.RIGHT,
Konami.B, Konami.A
};
private void checkKonamiCode() {
if (!konami && codes.size() >= expected.length) {
for (int i = 0; i < expected.length; ++i) {
if (codes.get(codes.size() - expected.length + i) != expected[i]) {
return;
}
}
konami = true;
}
}
}
enum Konami {
UP, DOWN, LEFT, RIGHT, B, A
}