-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamePanel.java
executable file
·438 lines (401 loc) · 12.7 KB
/
GamePanel.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* GamePanel class. Creates the main panel for the TowerDefenseGame. This
* contains all the crucial data of the game
*
* @author haolidu
* @version 1.00
*/
public class GamePanel extends JPanel {
private static final long serialVersionUID = -5083465984960174956L;
private Timer timer;
private int life; //
private int money;
private List<Enemy> enemies;
private List<Tower> towers;
private List<PathFragment> paths;
private static final int WIDTH = TowerDefenseGame.WINDOW_WIDTH - 150;
private static final int HEIGHT = TowerDefenseGame.WINDOW_HEIGHT;
/**
* Constructor for the GamePanel.
*/
public GamePanel() {
super();
setBackground(Color.white);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
// Initialize variables
paths = new ArrayList<PathFragment>(40);
enemies = new LinkedList<Enemy>();
towers = new ArrayList<Tower>();
life = 100; // Game is over when it reaches 0.
money = 500; // can be used to buy towers
// Configure Path for the game. 1.03 makes the target length
// (horizontal) of the path a bit longer to cover the width of the
// panel.
randomPath((int) (WIDTH * 1.03), null);
addMouseListener(new ClickListener());
timer = new Timer(80, new TimerListener());
timer.start();
}
/**
* Gets the list of enemies
*
* @return List<Enemy> a list that contains all the enemies
*/
public List<Enemy> getEnemies() {
return enemies;
}
/**
* Gets the life of the game
*
* @return int
*/
public int getLife() {
return life;
}
/**
* Gets the money of the game
*
* @return int
*/
public int getMoney() {
return money;
}
/**
* Gets the list of paths for the enemies to follow.
*
* @return List<PathFragment> a list that contains all the pathFragments of
* this game
*/
public List<PathFragment> getPath() {
return paths;
}
/**
* Gets the timer of the game
*
* @return Timer
*/
public Timer getTimer() {
return timer;
}
/**
* Gets the towers of the game
*
* @return List<Towers> a list of all the towers
*/
public List<Tower> getTowers() {
return towers;
}
/**
* Paints all the components on the game map. Towers are painted first, then
* the path, then all the enemies.
*
* @return none.
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draws all the towers
for (Tower tower : towers) {
tower.draw(g);
}
// Draws the path
for (PathFragment fragment : paths) {
fragment.draw(g);
}
// Draws all the enemies
for (Enemy enermy : enemies) {
enermy.draw(g);
}
}
/**
* Creates a new path for the game. Packages repaint() at the end
*
* @return none
*/
protected void newPath() {
paths = new ArrayList<PathFragment>(40);
randomPath((int) (WIDTH * 1.03), null);
repaint();
}
/**
* Attacks all the enemies.
*
* @return none
*/
private void attackEnemy() {
for (Enemy enemy : enemies) {
enemy.beAttacked();
}
}
/**
* Checks the status, out of bound, dead of all the enemies. Also checks if
* the game is over.
*
* @return none.
*/
private void checkStatus() {
for (Enemy enemy : enemies) {
if (enemy.isOutOfBound()) {
life -= enemy.decreaseLife;
if (life < 0) {
life = 0;
}
// Refreshes the control panel to reflect changes in numbers
TowerDefenseGame.getControlPanel().refresh();
} else if (enemy.isDead()) {
money += enemy.increaseMoney;
TowerDefenseGame.getControlPanel().refresh();
}
if (life <= 0) {
timer.stop();
JOptionPane.showMessageDialog(this, "Game Over",
"Gave Over", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
}
/**
* Helper method for path generation. Checks if the particular path fragment
* is in bound.
*
* @param pt
* the end point of the path fragment.
* @return true for in bound, false otherwise.
*/
private boolean isInBound(double[] pt) {
return (pt[0] >= 0 && pt[1] >= 0 && pt[1] <= HEIGHT);
}
/**
* Moves all the enemies.
*
* @return none.
*/
private void moveAll() {
for (Enemy enermy : enemies) {
enermy.move();
}
}
/**
* Helper method for generating random numbers. Generates a random number in
* int between 0 and the limit given
*
* @param limit
* @return int
*/
private int random(int limit) {
return (int) (Math.random() * limit);
}
/**
* Helper method for generating the path for the game Generates a random
* direction in int that are multiples of 45 degrees.
*
* @return int
*/
private int randomDirection() {
/*
* Random directions between 270-360 and 0-90. This creates a path
* that's visually appalling.
*
* if (Math.random() < 0.5) { return 270 + random(90); } else { return
* random(120); }
*/
// Generates angles that are multiples of 45 degrees.
double random = Math.random();
if (random < 0.125) {
return 0;
} else if (random < 0.25 && random >= 0.125) {
return 45;
} else if (random < 0.375 && random >= 0.25) {
return 90;
} else if (random < 0.5 && random >= 0.375) {
return 135;
} else if (random < 0.75 && random >= 0.625) {
return 225;
} else if (random < 0.825 && random >= 0.7) {
return 270;
} else {
return 315;
}
}
/**
* Initialize the list of path fragments. It is able to randomly generate a
* game path. It recursively calls itself to add new path fragment to the
* game path until it is long enough to cover the width of the panel.
*
* @param int targetLength: the horizontal distance for the path to cover.
* PathFragment previousFragment, null if is the first path fragment
* @return none.
*/
private void randomPath(int targetLength, PathFragment previousFragment) {
int width = 80 + (int) (Math.random() * 100); // A random width for each
// path fragment
if (previousFragment == null) { // First path fragment
// Randomly choose a starting point on the left side of the panel
// and direction
int y = random(TowerDefenseGame.WINDOW_HEIGHT);
int direction = random(70);
PathFragment newFragment = new PathFragment(0, y, width, direction);
// Check if the new path is in bound
while (!isInBound(newFragment.getPathEnd())) {
direction = random(70);
newFragment = new PathFragment(0, y, width, direction);
}
paths.add(newFragment);
randomPath(targetLength - (int)
(Math.cos(Math.toRadians(direction)) * width), newFragment);
} else if (targetLength > 0) {
int direction = randomDirection();
PathFragment newFragment;
double[] pt = previousFragment.getPathEnd();
newFragment = new PathFragment((int) pt[0],
(int) pt[1], width, direction);
while (!isInBound(newFragment.getPathEnd())
|| direction == previousFragment.getDirection()
|| direction == previousFragment.getDirection() - 180
|| direction == previousFragment.getDirection() + 180) {
direction = randomDirection();
newFragment = new PathFragment((int)
pt[0], (int) pt[1], width, direction);
}
paths.add(newFragment);
randomPath(targetLength - (int)
(Math.cos(Math.toRadians(direction)) * width), newFragment);
}
}
/**
* Remove enemies that are either out of bound or dead. Also checks if this
* wave is defeated. If it is, starts a new wave and adds 25 to the money
*
* @return none
*/
private void remove() {
Iterator<Enemy> iterator = enemies.iterator();
while (iterator.hasNext()) {
Enemy enemy = iterator.next();
if (enemy.isDead() || enemy.isOutOfBound()) {
iterator.remove();
}
if (enemies.isEmpty()) {
money += 25;
TowerDefenseGame.getControlPanel().newWave();
}
}
}
/**
* Resets number of enemies that the tower has attacked after each
* iteration.
*
* @return none.
*/
private void resetTower() {
for (Tower tower : towers) {
tower.resetAttacked();
}
}
/**
* Click listener. Responds to clicking on the game panel
*
* @author haolidu
* @version 1.00
*/
private class ClickListener extends MouseAdapter {
/**
* Instantiating a Tower given a String of the class name and the point
* where it should be place.
*
* @param Point
* p is where the tower will be placed.
* @param String
* className for the tower
* @return Tower
*/
public Tower instantiateTower(String className, Point p) {
try {
Class<?> cl = Class.forName(className);
return (Tower) (cl.getDeclaredConstructor(int.class,
int.class).newInstance(p.x, p.y));
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchMethodException e) {
e.printStackTrace();
System.exit(1);
} catch (InstantiationException e) {
e.printStackTrace();
System.exit(1);
} catch (IllegalAccessException e) {
e.printStackTrace();
System.exit(1);
} catch (InvocationTargetException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
/**
* Responds to mouse clicking on the panel
*
* @return none
*/
public void mousePressed(MouseEvent e) {
// Get the Tower properties
String towerType = TowerDefenseGame
.getControlPanel().getTowerType();
Point p = e.getPoint();
// Create the corresponding tower if there's enough money.
Tower tower = instantiateTower(towerType, p);
if (money >= tower.cost) {
money -= tower.cost;
towers.add(tower);
TowerDefenseGame.getControlPanel().refresh();
}
repaint();
}
}
/**
* TimerListener. Responds to the timer.
*
* @author haolidu
* @version 1.00
*/
private class TimerListener implements ActionListener, Runnable {
/**
* Responds to timer trigger. Creates a new Thread each time timer
* triggers.
*/
public void actionPerformed(ActionEvent e) {
(new Thread(new TimerListener())).start();
}
/**
* Runs the thread. Contains the sequence of events that happens each
* time timer triggers
*/
@Override
public void run() {
synchronized (this) {
moveAll();
attackEnemy();
checkStatus();
remove();
resetTower();
repaint();
}
}
}
}