-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
236 lines (186 loc) · 4.42 KB
/
Controller.java
File metadata and controls
236 lines (186 loc) · 4.42 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
235
236
/*
* Levi Crider
* 2/9/22
* Legend of Zelda
*
*
*/
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
//This class is capable of handling ActionEvents such as when someone pushes a button
class Controller implements ActionListener, MouseListener, KeyListener{
//Member variables
View view;
Model model;
//Controls
//Quit
boolean esc;
boolean q;
//Save Load
boolean s;
boolean l;
//Toggle map
boolean mapEdit = false;
boolean potOrBrick = false; //false is for brick, true is for pots.
//Movement
boolean up;
boolean down;
boolean left;
boolean right;
//Boomerang
boolean ctrl;
Controller(Model m) {
model = m;
}
public void actionPerformed(ActionEvent e) { //method that handles the event
}
void setView(View v) { //Setter that sets the view
view = v;
}
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
//Quitting
case KeyEvent.VK_ESCAPE: esc = true; break;
case KeyEvent.VK_Q: q = true; break;
//Saving
case KeyEvent.VK_S:
model.Marshal();
System.out.println("Saved");
break;
//Loading
case KeyEvent.VK_L:
Json loadObject = Json.load("brickLocation.json");
model.Unmarshal(loadObject);
System.out.println("Loaded");
break;
//Toggling map edit
case KeyEvent.VK_E:
if(mapEdit){
mapEdit = false;
System.out.println("Map editing turned off!");
}
else{
mapEdit = true;
System.out.println("Map editing turned on!");
if (potOrBrick){
System.out.println("Editing Pots right now!");
}
else if (!potOrBrick){
System.out.println("Editing Bricks right now!");
}
}
break;
case KeyEvent.VK_P:
if (potOrBrick){
potOrBrick = false;
if (mapEdit && !potOrBrick){
System.out.println("Editing Bricks");
}
}
else{
potOrBrick = true;
if (mapEdit && potOrBrick){
System.out.println("Editing Pots");
}
}
break;
//Movement cases
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
}
}
public void keyReleased(KeyEvent e)
{
switch(e.getKeyCode())
{
//Movement
case KeyEvent.VK_RIGHT: right = false; break;
case KeyEvent.VK_LEFT: left = false; break;
case KeyEvent.VK_UP: up = false; break;
case KeyEvent.VK_DOWN: down = false; break;
//Boomerang
case KeyEvent.VK_CONTROL: model.addBoomerang(); break;
}
}
public void keyTyped(KeyEvent e)
{
}
void update() {
model.link.savePrev();
//Exit
if(esc) System.exit(0);
if (q) System.exit(0);
//Moving Link
if (right){
viewIncreaseX();
model.link.moveRight();
}
if (left){
//Moving the window when we goes through a left door
viewDecreaseX();
model.link.moveLeft();
}
if (up){
model.link.moveUp();
//Moving the window when we goes through an up door
viewDecreaseY();
}
if (down){
model.link.moveDown();
//Moving the window when we goes through an down door
viewIncraseY();
}
}
public void mousePressed(MouseEvent e)
{
//Gets the location of where user clicks
int locationx = e.getX();
int locationy = e.getY();
//Adding bricks or pots to the map
if(mapEdit && !potOrBrick){
model.addBrickToScreen(locationx + View.scrollPositonX, locationy + View.scrollPositonY); //Adds the brick to the screen
}
if (mapEdit && potOrBrick){
model.addPotToScreen(locationx + View.scrollPositonX, locationy + View.scrollPositonY);
}
}
//Making sure that the view stays in bounds
void viewIncreaseX(){
if (model.link.x == View.windowXSize){
View.scrollPositonX += View.windowXSize;
}
}
void viewDecreaseX(){
if (model.link.x == View.windowXSize){
View.scrollPositonX -= View.windowXSize;
}
}
void viewIncraseY(){
if (model.link.y == View.windowYSize){
View.scrollPositonY += View.windowYSize;
}
}
void viewDecreaseY(){
if (model.link.y == View.windowYSize){
View.scrollPositonY -= View.windowYSize;
}
}
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
}