-
Notifications
You must be signed in to change notification settings - Fork 0
/
TowerDefenseController.java
190 lines (157 loc) · 4.45 KB
/
TowerDefenseController.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
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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.net.ssl.SSLContext;
import javax.swing.JRadioButton;
public class TowerDefenseController implements StateObserver, MouseListener, ActionListener, KeyListener {
private TowerDefenseModel model;
private TowerDefenseView view;
private JRadioButton selectedButton;
private SquareCoordinate selectedSquare = new SquareCoordinate(4, 3);
public TowerDefenseController(TowerDefenseModel model, TowerDefenseView view) {
this.model = model;
this.view = view;
model.registerStateObserver(this);
view.addMouseListener(this);
view.addKeyListener(this);
view.getStoreButtons().get(0).setSelected(true);
selectedButton = view.getStoreButtons().get(0);
for (JRadioButton button : view.getStoreButtons()) {
button.addActionListener(this);
}
view.getNameTextField().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
nameTyped(arg0);
}
});
}
public void nameTyped(ActionEvent e) {
model.placeScore(new Highscore(e.getActionCommand(), model.getScore()));
}
@Override
public void stateChanged() {
view.setState(model);
view.setSelectedSquare(selectedSquare);
view.repaint();
}
@Override
public void mousePressed(MouseEvent arg0) {
if (selectedButton == null)
return;
if (model.isGameOver())
return;
int x = arg0.getX();
int y = arg0.getY();
// account for 10px border
x -= 10;
y -= 10;
// each square is 100px side length
x /= 100;
y /= 100;
if (x > Map.COLUMNS)
return;
if (y > Map.ROWS)
return;
SquareCoordinate position = new SquareCoordinate(x, y);
attemptPurchase(position);
view.requestFocus();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent arg0) {
selectedButton = ((JRadioButton) arg0.getSource());
view.requestFocus();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
private Purchasable getSelectedItem(SquareCoordinate position) {
if (selectedButton == null)
return null;
switch (selectedButton.getText()) {
case "[10 points] Basic Turret":
return (new BasicTurret(position));
case "[20 points] Circle Turret":
return (new CircleTurret(position));
case "[30 points] Aim Turret":
return (new AimTurret(position));
case "[50 points] Homing Turret":
return (new HomingTurret(position));
case "[40 points] Reduce cooldown":
return (new CooldownUpgrade(position));
case "[60 points] Damage upgrade":
return (new DamageUpgrade(position));
default:
return null;
}
}
private void attemptPurchase(SquareCoordinate position) {
try {
if (getSelectedItem(position) != null)
model.purchase(getSelectedItem(position));
else
model.sell(position);
view.clearPurchaseErrorMessage();
} catch (PurchaseException e) {
view.setPurchaseErrorMessage(e.getMessage());
}
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (e.getKeyChar() == 'm')
model.cheat();
if (e.getKeyChar() == ' ')
model.togglePause();
if (e.getKeyChar() == 'r')
model.restart();
if (e.getKeyChar() == 'a')
model.advanceLevel();
try {
int n = Integer.parseInt(Character.toString(e.getKeyChar()));
if (n > 0 && n <= view.getStoreButtons().size()) {
view.getStoreButtons().get(n - 1).setSelected(true);
selectedButton = view.getStoreButtons().get(n - 1);
}
} catch (NumberFormatException ex) {
}
if (model.isGameOver())
return;
// up = 38, down = 40, left = 37, right = 39
if (code == 38)
selectedSquare = selectedSquare.decY();
if (code == 40)
selectedSquare = selectedSquare.incY();
if (code == 37)
selectedSquare = selectedSquare.decX();
if (code == 39)
selectedSquare = selectedSquare.incX();
if (code == 10)
attemptPurchase(selectedSquare);
}
}