-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTitleScreen.java
More file actions
234 lines (207 loc) · 8.19 KB
/
TitleScreen.java
File metadata and controls
234 lines (207 loc) · 8.19 KB
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
import processing.core.PImage;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class TitleScreen implements Renderable{
private StringBuilder stringBuilder = new StringBuilder();
private final Main main = Main.getInstance();
private Mode mode;
private final float centerX = main.width / 2;
// title screen variables
private final PImage titleImage, whatIsThisImage, playImage;
private float titleImageY = .2F * main.height;
private float whatIsThisButtonY = .40F * main.height;
private float playButtonY = .60F * main.height;
private final float widthTitleButton = .5F * main.width; // 50% of width
private final float heightTitleButton = .1F * main.height; // 10% of width
// play screen variables
private final PImage widthImage, heightImage, monsterImage;
private PlayField playField;
private float leftX = .2F * main.width;
private float widthY = .1F * main.height;
private float heightY = .3F * main.height;
private float monstersY = .5F * main.height;
private float playY = .7F * main.height;
private float inputAreaX = .7F * main.width;
private float inputAreaWidth = .5F * main.width;
private float inputAreaHeight = .1F * main.height;
private int height = 15;
private int width = 15;
private int amountMonsters = 2;
public TitleScreen(){
mode = Mode.TITLE;
titleImage = main.loadImage("maze_runner.png");
titleImage.resize(800, 0);
whatIsThisImage = main.loadImage("what_is_this.png");
whatIsThisImage.resize(400, 0);
playImage = main.loadImage("play.png");
playImage.resize(200, 0);
widthImage = main.loadImage("width.png");
widthImage.resize(400, 0);
heightImage = main.loadImage("height.png");
heightImage.resize(400, 0);
monsterImage = main.loadImage("monsters.png");
monsterImage.resize(400, 0);
}
@Override
public void render(){
switch(mode){
case TITLE:
renderTitleMode();
break;
case PLAY:
renderPlayMode();
break;
}
}
private void renderTitleMode(){
main.imageMode(main.CENTER);
main.image(titleImage, centerX, titleImageY);
main.rectMode(main.CENTER);
main.fill(0, 105, 217);
main.rect(centerX, whatIsThisButtonY, widthTitleButton, heightTitleButton);
main.image(whatIsThisImage, centerX, whatIsThisButtonY);
main.fill(33, 136, 56);
main.rect(centerX, playButtonY, widthTitleButton, heightTitleButton);
main.image(playImage, centerX, playButtonY);
}
private void renderPlayMode(){
main.imageMode(main.CENTER);
main.rectMode(main.CENTER);
main.textSize(widthY * .7F);
main.stroke(255);
// width input
main.fill(255);
main.rect(inputAreaX, widthY, inputAreaWidth, inputAreaHeight);
main.fill(0);
main.text(width, inputAreaX - inputAreaX * .3F, widthY + widthY * .3F);
main.image(widthImage, leftX, widthY);
// height input
main.fill(255);
main.rect(inputAreaX, heightY, inputAreaWidth, inputAreaHeight);
main.fill(0);
main.text(height, inputAreaX - inputAreaX * .3F, heightY + widthY * .3F);
main.image(heightImage, leftX, heightY);
// monster amount input
main.fill(255);
main.rect(inputAreaX, monstersY, inputAreaWidth, inputAreaHeight);
main.fill(0);
main.text(amountMonsters, inputAreaX - inputAreaX * .3F, monstersY + widthY * .3F);
main.image(monsterImage, leftX, monstersY);
// play button
main.fill(33, 136, 56);
main.rect(centerX, playY, widthTitleButton, heightTitleButton);
main.image(playImage, centerX, playY);
}
public void feedCharacter(char key){
if(mode == Mode.PLAY){
if(key == '\b' && stringBuilder.length() >= 1){
// backspace
stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), "");
}
// make sure input is a number
try{
int value = Integer.parseInt(key + "");
stringBuilder.append(value);
}catch (NumberFormatException e){
// cannot accept non number
}
if(playField != null){
parseInput();
}
}
}
public void click(){
switch(mode){
case TITLE:
// what is this section clicked
if(main.mouseX >= centerX - widthTitleButton / 2 && main.mouseX <= centerX + widthTitleButton / 2 &&
main.mouseY >= whatIsThisButtonY - heightTitleButton / 2 && main.mouseY <= whatIsThisButtonY + heightTitleButton / 2){
Desktop desktop = Desktop.getDesktop();
try{
desktop.browse(new URI("https://github.com/WilliamC07/MazeRunner/blob/master/README.md"));
}catch (URISyntaxException | IOException e){
e.printStackTrace();
}
}
// play section clicked
if(main.mouseX >= centerX - widthTitleButton / 2 && main.mouseX <= centerX + widthTitleButton / 2 &&
main.mouseY >= playButtonY - heightTitleButton / 2 && main.mouseY <= playButtonY + heightTitleButton / 2){
mode = Mode.PLAY;
}
break;
case PLAY:
// parse input when clicked
stringBuilder = new StringBuilder();
fixBounds();
// width section clicked
if(main.mouseX >= inputAreaX - inputAreaWidth / 2 && main.mouseX <= inputAreaX + inputAreaWidth / 2 &&
main.mouseY >= widthY - inputAreaHeight / 2 && main.mouseY <= widthY + inputAreaHeight / 2){
playField = PlayField.WIDTH;
}
// height section clicked
if(main.mouseX >= inputAreaX - inputAreaWidth / 2 && main.mouseX <= inputAreaX + inputAreaWidth / 2 &&
main.mouseY >= heightY - inputAreaHeight / 2 && main.mouseY <= heightY + inputAreaHeight / 2){
playField = PlayField.HEIGHT;
}
// monster section clicked
if(main.mouseX >= inputAreaX - inputAreaWidth / 2 && main.mouseX <= inputAreaX + inputAreaWidth / 2 &&
main.mouseY >= monstersY - inputAreaHeight / 2 && main.mouseY <= monstersY + inputAreaHeight / 2){
playField = PlayField.MONSTER;
}
// play button clicked
if(main.mouseX >= centerX - widthTitleButton / 2 && main.mouseX <= centerX + widthTitleButton / 2 &&
main.mouseY >= playY - heightTitleButton / 2 && main.mouseY <= playY + heightTitleButton / 2){
main.startGame(height, width, amountMonsters);
}
break;
}
}
private void parseInput(){
int value = 0;
try{
value = Integer.parseInt(stringBuilder.toString());
}catch(NumberFormatException e){
// Cannot get the value, so just use the default one
}
switch (playField) {
case HEIGHT:
height = value;
break;
case WIDTH:
width = value;
break;
case MONSTER:
amountMonsters = value;
break;
}
}
private void fixBounds(){
if(height < 10){
height = 10;
}else if(height > 50){
height = 50;
}
if(width < 10){
width = 10;
}else if(width > 50){
width = 50;
}
if(amountMonsters < 0){
amountMonsters = 0;
}else if(amountMonsters > 10){
amountMonsters = 10;
}
}
private enum Mode{
TITLE,
WHAT_IS_THIS,
PLAY
}
private enum PlayField{
HEIGHT,
WIDTH,
MONSTER
}
}