-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
182 lines (164 loc) · 3.89 KB
/
Main.java
File metadata and controls
182 lines (164 loc) · 3.89 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
import processing.core.PApplet;
import java.util.*;
public class Main extends PApplet{
private Character character;
private Maze maze;
private List<Character> movables;
private List<Renderable> renderables;
private boolean isGodMode;
private Screen screen;
private EndScreen endScreen;
/**
* Singleton design pattern so we don't need to keep passing reference to this class around. We need
* the draw methods inside PApplet (like ellipse(float, float, float, float))
*/
private static Main instance;
private TitleScreen titleScreen;
public static void main(String[] args){
PApplet.main("Main");
}
/**
* Access the singleton.
* Use this to access draw methods (the ellipse(), line() and more).
* @return Get the single instance of this class.
*/
public static Main getInstance(){
return instance;
}
@Override
public void settings(){
screen = Screen.TITLE_SCREEN;
instance = this;
size(1000, 1000);
titleScreen = new TitleScreen();
endScreen = new EndScreen();
renderables = new ArrayList<>();
movables = new ArrayList<>();
}
@Override
public void draw(){
switch(screen){
case TITLE_SCREEN:
background(255);
titleScreen.render();
break;
case MINIMAP:
background(isGodMode ? 255 : 0);
maze.renderMinimap();
if(isGodMode){
maze.renderMinimapGod();
}
break;
case PLAYING:
background(isGodMode ? 255 : 0);
renderables.forEach(Renderable::render);
movables.forEach(Character::move);
break;
case END:
background(255);
endScreen.render();
break;
}
}
@Override
public void keyPressed(){
// title screen commands
if(screen == Screen.TITLE_SCREEN){
titleScreen.feedCharacter(key);
}
// playing only keys
if(screen == Screen.PLAYING){
switch(key){
case 'H':
case 'h':
maze.hint(character.getPos(),10);
break;
case 'G':
case 'g':
maze.hint(character.getPos(),maze.getLength()*maze.getWidth());
break;
default:
character.setVelocity(keyCode,true);
break;
}
}
// minimap and playing commands
if(screen == Screen.MINIMAP || screen == Screen.PLAYING){
switch(key){
case 'M':
case 'm':
screen = screen == Screen.MINIMAP ? Screen.PLAYING : Screen.MINIMAP;
break;
case 'P':
case 'p':
isGodMode = !isGodMode;
break;
case 'R':
case 'r':
bringToTitleScreen();
break;
}
}
}
/**
* God mode means you can see the whole map (ray casting off).
* @return
*/
public boolean isGodMode(){
return isGodMode;
}
@Override
public void mouseClicked() {
if(screen == Screen.TITLE_SCREEN){
titleScreen.click();
}else if(screen == Screen.END){
endScreen.click();
}
}
@Override
public void keyReleased(){
if(screen == Screen.PLAYING){
character.setVelocity(keyCode,false);
}
}
public void startGame(int rows, int columns, int amountMonsters){
// for when we restart the game
renderables.clear();
movables.clear();
maze = new Maze(rows, columns, this);
character = new Character(maze);
movables.add(character);
renderables.add(character);
for(int i = 0; i<amountMonsters; i++){
int randomX = (int)(Math.random()*maze.getWidth());
int randomY = (int)(Math.random()*maze.getLength());
while(randomX<5 && randomY<5){
randomX = (int)(Math.random()*maze.getWidth());
randomY = (int)(Math.random()*maze.getLength());
}
Monster monster = new Monster(randomX,randomY,maze,character);
movables.add(monster);
renderables.add(monster);
}
renderables.add(maze);
screen = Screen.PLAYING;
}
public List<Character> getMovables(){
return movables;
}
public void endGame(EndScreen.EndType endType){
screen = Screen.END;
endScreen.show(endType);
}
public void bringToTitleScreen(){
titleScreen = new TitleScreen();
endScreen = new EndScreen();
this.screen = Screen.TITLE_SCREEN;
}
private enum Screen{
PLAYING,
MINIMAP,
TITLE_SCREEN,
END
}
}