-
Notifications
You must be signed in to change notification settings - Fork 0
/
A3JPanel.java
400 lines (365 loc) · 11.7 KB
/
A3JPanel.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
/*
*Author: Cameron Ekblad
*UPI: cekb635
*Date: 27/5/2011
*This is the main class for the game Mission: Infection Prevention! Contained in this class are most of the general mechanics of the game: various timers to control
*player and enemy movement, enemy respawning and firing delay, the array containing the enemies defined by the Zombie class, the borders of the playing-area and the
*sounds and graphics. It also contains methods for dealing with input from the user in the form of key presses, mouse movements and mouse clicks, and interaction
*between the player and the enemies (i.e. computing whether an enemy has been shot or come into contact with the player), and how to respond to these situations.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;
import java.awt.geom.*;
public class A3JPanel extends JPanel implements MouseMotionListener, KeyListener, ActionListener, MouseListener {
public static final int MAX_NUMBER_OF_ZOMBIES = 10;
public static final char UP = 'w';
public static final char DOWN = 's';
public static final char LEFT = 'a';
public static final char RIGHT = 'd';
private boolean newInstance;
private Font header;
private Font subHeader;
private Font mainText;
private Font difficultyMode;
private Font killCounter;
private Line2D.Double topBorder;
private Line2D.Double leftBorder;
private Line2D.Double bottomBorder;
private Line2D.Double rightBorder;
private Crosshair c;
private Player player;
private Timer upwardMovementTimer;
private Timer downwardMovementTimer;
private Timer leftMovementTimer;
private Timer rightMovementTimer;
private Timer zombieTimer;
private Timer zombieRespawnTimer;
private Timer firingDelay;
private Timer reloadTimer;
private char pressedKey;
private boolean hasFired;
private Zombie[] zombies;
private AudioClip bgMusic;
private AudioClip shotSound;
private AudioClip reloadSound;
private AudioClip zombieDies;
private AudioClip playerDies;
private double zombieSpeedIncreaseFactor;
private boolean hardcoreMode;
private boolean easyMode;
private boolean normalMode;
private boolean inFreezeMoment;
public A3JPanel() {
setBackground(new Color(70, 30, 10));
header = new Font("SansSerif", Font.BOLD, 30);
subHeader = new Font("SansSerif", Font.BOLD, 22);
mainText = new Font("SansSerif", 12, 12);
difficultyMode = new Font("Dialog", Font.BOLD, 36);
killCounter = new Font("Monospaced", Font.BOLD, 20);
topBorder = new Line2D.Double(0.0, 0.0, 600.0, 0.0);
bottomBorder = new Line2D.Double(0.0, 600.0, 600.0, 600.0);
leftBorder = new Line2D.Double(0.0, 0.0, 0.0, 600.0);
rightBorder = new Line2D.Double(600.0, 0.0, 600.0, 600.0);
c = new Crosshair();
player = new Player();
upwardMovementTimer = new Timer(30, this);
downwardMovementTimer = new Timer(30, this);
leftMovementTimer = new Timer(30, this);
rightMovementTimer = new Timer(30, this);
zombieTimer = new Timer(30, this);
zombieRespawnTimer = new Timer(3000, this);
addMouseMotionListener(this);
addKeyListener(this);
addMouseListener(this);
zombieSpeedIncreaseFactor = 0.15;
zombies = new Zombie[MAX_NUMBER_OF_ZOMBIES];
for(int i = 0; i < zombies.length; i++){
zombies[i] = new Zombie(player.getMiddle(), zombieSpeedIncreaseFactor);
}
bgMusic = loadSound("MIP Theme.mid");
shotSound = loadSound("shot.wav");
reloadSound = loadSound("pump.wav");
zombieDies = loadSound("hit.wav");
playerDies = loadSound("dammit.wav");
bgMusic.loop();
newInstance = true;
}
public void actionPerformed(ActionEvent e){
int zombieSpawnCount = 0;
if(!(easyMode) && !(hardcoreMode)){
normalMode = true;
}
if(newInstance){
if(hardcoreMode){
firingDelay = new Timer(2000, this);
reloadTimer = new Timer(1500, this);
}
else if(easyMode){
firingDelay = new Timer(500, this);
reloadTimer = new Timer(100, this);
}
else if(normalMode){
firingDelay = new Timer(1500, this);
reloadTimer = new Timer(1000, this);
}
newInstance = false;
inFreezeMoment = true;
}
else if(inFreezeMoment){
repaint();
inFreezeMoment = false;
}
else {
zombieTimer.start();
zombieRespawnTimer.start();
if(e.getSource() == upwardMovementTimer && !(player.playerIsDead())){
player.moveUp(topBorder);
repaint();
player.updatePlayerArea();
}
if(e.getSource() == downwardMovementTimer && !(player.playerIsDead())){
player.moveDown(bottomBorder);
repaint();
player.updatePlayerArea();
}
if(e.getSource() == leftMovementTimer && !(player.playerIsDead())){
player.moveLeft(leftBorder);
repaint();
player.updatePlayerArea();
}
if(e.getSource() == rightMovementTimer && !(player.playerIsDead())){
player.moveRight(rightBorder);
repaint();
player.updatePlayerArea();
}
if(e.getSource() == zombieTimer && !(player.playerIsDead())){
for(int i = 0; i < zombies.length; i++){
if(!(zombies[i].zombieIsDead())){
zombies[i].move(player.getMiddle());
}
}
repaint();
for(int i = 0; i < zombies.length; i++){
if(zombies[i].getZombieArea().intersects(player.getPlayerArea()) && !(zombies[i].zombieIsDead())){
playerDies.play();
player.die();
zombieTimer.stop();
zombieRespawnTimer.stop();
}
}
}
if(e.getSource() == zombieRespawnTimer && !(player.playerIsDead())){
for(int i = 0; i < zombies.length; i++){
if(zombies[i].zombieIsDead()){
zombies[i] = new Zombie(player.getMiddle(), zombieSpeedIncreaseFactor, player.getZombieKills() * zombieSpeedIncreaseFactor);
if(hardcoreMode){
zombieSpawnCount++;
if(zombieSpawnCount == 3){
break;
}
}
else {
break;
}
}
}
}
if(e.getSource() == firingDelay){
player.playerCanFire();
firingDelay.stop();
}
if(e.getSource() == reloadTimer && !(player.playerIsDead())){
reloadSound.play();
reloadTimer.stop();
}
}
}
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == '1'){
zombieSpeedIncreaseFactor = 0.1;
for(int i = 0; i < zombies.length; i++){
zombies[i] = new Zombie(player.getMiddle(), zombieSpeedIncreaseFactor);
}
zombieRespawnTimer.setDelay(6000);
hardcoreMode = false;
normalMode = false;
easyMode = true;
repaint();
}
else if(e.getKeyChar() == '3'){
zombieSpeedIncreaseFactor = 0.2;
for(int i = 0; i < zombies.length; i++){
zombies[i] = new Zombie(player.getMiddle(), zombieSpeedIncreaseFactor);
}
zombieRespawnTimer.setDelay(2000);
easyMode = false;
normalMode = false;
hardcoreMode = true;
repaint();
}
else if(e.getKeyChar() == '2'){
zombieSpeedIncreaseFactor = 0.15;
for(int i = 0; i < zombies.length; i++){
zombies[i] = new Zombie(player.getMiddle(), zombieSpeedIncreaseFactor);
}
zombieRespawnTimer.setDelay(3000);
hardcoreMode = false;
easyMode = false;
normalMode = true;
repaint();
}
else {
if(e.getKeyChar() == UP){
upwardMovementTimer.start();
}
if(e.getKeyChar() == DOWN){
downwardMovementTimer.start();
}
if(e.getKeyChar() == LEFT){
leftMovementTimer.start();
}
if(e.getKeyChar() == RIGHT){
rightMovementTimer.start();
}
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyChar() == UP){
upwardMovementTimer.stop();
}
if(e.getKeyChar() == DOWN){
downwardMovementTimer.stop();
}
if(e.getKeyChar() == LEFT){
leftMovementTimer.stop();
}
if(e.getKeyChar() == RIGHT){
rightMovementTimer.stop();
}
}
public void mouseMoved(MouseEvent e){
c.setCentre(e.getX(), e.getY());
repaint();
}
public void mousePressed(MouseEvent e){
if((player.canPlayerFire() && !(player.playerIsDead())) && (!(newInstance) && zombieTimer.isRunning())){
shotSound.play();
player.fire(c.getCentre());
for(int i = 0; i < zombies.length; i++){
if(zombies[i].getZombieArea().intersectsLine(player.getShot()) && !(zombies[i].zombieIsDead())){
zombieDies.play();
zombies[i].die();
player.addZombieKill();
for(int j = 0; j < zombies.length; j++){
zombies[j].speedUp();
}
}
}
hasFired = true;
player.playerCannotFire();
firingDelay.start();
reloadTimer.start();
repaint();
}
}
public void mouseReleased(MouseEvent e){
hasFired = false;
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(newInstance){
drawTitleScreen(g);
if(easyMode){
g.setColor(Color.RED);
g.setFont(difficultyMode);
g.drawString("1", 200, 550);
g.drawRect(195, 520, 30, 35);
g.setFont(mainText);
g.drawString("easy", 198, 570);
}
else if(normalMode){
g.setColor(Color.RED);
g.setFont(difficultyMode);
g.drawString("2", 275, 550);
g.drawRect(270, 520, 30, 35);
g.setFont(mainText);
g.drawString("Normal", 265, 570);
}
else if(hardcoreMode){
g.setColor(Color.RED);
g.setFont(difficultyMode);
g.drawString("3", 350, 550);
g.drawRect(345, 520, 30, 35);
g.setFont(mainText);
g.drawString("HARDCORE", 324, 570);
}
}
else {
if(hasFired){
player.drawShot(g, c.getCentre());
}
for(int i = 0; i < zombies.length; i++){
if(!(zombies[i].zombieIsDead())){
zombies[i].draw(g);
}
}
player.draw(g);
c.draw(g);
drawKillCounter(g);
if(player.playerIsDead()){
g.setFont(header);
g.setColor(Color.BLACK);
g.drawString("GAME OVER", 200, 100);
}
}
}
public void drawKillCounter(Graphics g){
g.setColor(Color.RED);
g.setFont(killCounter);
g.drawString("Kills: " + (int)(player.getZombieKills()), 0, 600);
}
public void drawTitleScreen(Graphics g){
g.setColor(Color.red);
g.setFont(header);
g.drawString("Mission: Infection Prevention! (MIP!)", 50, 100);
g.setFont(subHeader);
g.drawString("by cekb635", 100, 140);
g.setFont(mainText);
g.drawString("Welcome to the apocalypse! In this game, the objective is to hold off the horde for as long as possible.", 25, 200);
g.drawString("To do this, take out the oncoming zombies with your shotgun. But beware; your shotgun is pump-action,", 25, 250);
g.drawString("and takes a moment to reload, and each time you take one out, the zombies get faster.", 25, 275);
g.drawString("Oh, and they don't stay down... TIP: Try and trap multiple zombies in your firing line by aiming past them!", 25, 300);
g.drawString("Use the W, A, S and D keys to move your player around the screen, aim using the mouse and click to fire!", 25, 350);
g.drawString("Press 1 for easy-mode if you're having trouble! Your shotgun reloads faster and the zombies are slower,", 25, 400);
g.drawString("and respawn less frequently. Press 2 to switch back to normal mode, or...", 25, 425);
g.drawString("Press 3 for hardcore-mode: your shotgun is a little rusty, and loads a little slower... Plus, the zombies", 25, 450);
g.drawString("are faster, and come back to life very quickly!", 25, 475);
g.drawLine(25, 452, 175, 452);
g.drawString("Press W, A, S or D to begin...", 225, 500);
g.setFont(difficultyMode);
g.setColor(Color.black);
g.drawString("1", 200, 550);
g.drawRect(195, 520, 30, 35);
g.drawString("2", 275, 550);
g.drawRect(270, 520, 30, 35);
g.drawString("3", 350, 550);
g.drawRect(345, 520, 30, 35);
g.setFont(mainText);
g.drawString("easy", 198, 570);
g.drawString("Normal", 265, 570);
g.drawString("HARDCORE", 324, 570);
}
public void mouseDragged(MouseEvent e){}
public void keyTyped(KeyEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
private AudioClip loadSound(String fileName) {
URL url = getClass().getResource(fileName);
return Applet.newAudioClip(url);
}
}