-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.java
148 lines (126 loc) · 3.04 KB
/
Controller.java
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
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;
class Controller implements ActionListener, MouseListener, KeyListener {
View view;
Model model;
// Keys used as inputs
boolean keyLeft;
boolean keyRight;
boolean keyUp;
boolean keyDown;
boolean keyS; // Save JSON value
boolean keyL; // Load JSON value
boolean keyC;
boolean keySpace;
//////////////////////////////////////
/////////// Utility Methods //////////
//////////////////////////////////////
Controller(Model m) {
model = m;
}
void setView(View v) {
view = v;
}
//////////////////////////////////////
//////// Mouse Action Methods ////////
//////////////////////////////////////
public void actionPerformed(ActionEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
model.setDestination1(e.getX(), e.getY());
model.setDestination2(e.getX(), e.getY());
model.addCoinBlock();
}
}
public void mousePressed(MouseEvent e) {
model.setDestination1(e.getX(), e.getY());
}
public void mouseReleased(MouseEvent e) {
model.setDestination2(e.getX(), e.getY());
model.addBrick();
}
//////////////////////////////////////
////////// Key Action Methods ////////
//////////////////////////////////////
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
keyRight = true;
break;
case KeyEvent.VK_LEFT:
keyLeft = true;
break;
case KeyEvent.VK_S:
keyS = true;
break;
case KeyEvent.VK_L:
keyL = true;
break;
case KeyEvent.VK_SPACE:
keySpace = true;
break;
}
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
keyRight = false;
break;
case KeyEvent.VK_LEFT:
keyLeft = false;
break;
case KeyEvent.VK_S:
keyS = false;
break;
case KeyEvent.VK_L:
keyL = false;
break;
case KeyEvent.VK_SPACE:
keySpace = false;
break;
}
}
public void keyTyped(KeyEvent e) {
}
//////////////////////////////////////
//////////////// Update //////////////
//////////////////////////////////////
void updateNew() {
// Evaluate each possible action
model.mario.setPrevious();
double score_run = model.evaluateAction(Action.RunRight, 0);
double score_jump = model.evaluateAction(Action.Jump, 0);
double score_jump_and_run = model.evaluateAction(Action.RunAndJump, 0);
if (score_jump_and_run > score_jump && score_jump_and_run > score_run)
model.doAction(Action.RunAndJump);
else if (score_jump > score_run)
model.doAction(Action.Jump);
else
model.doAction(Action.RunRight);
}
void update() {
model.mario.setPrevious();
if (keyRight) {
model.mario.runRight();
}
if (keyLeft) {
model.mario.runLeft();
}
if (keySpace) {
model.mario.jump();
}
if (keyS)
model.marshal();
if (keyL)
model.unmarshal();
}
}