-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.java
More file actions
481 lines (389 loc) · 8.95 KB
/
Copy pathEnemy.java
File metadata and controls
481 lines (389 loc) · 8.95 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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.ArrayList;
/**
Parent class for all enemy entities
@author Kush Banker and Jack Basinet
@version 11.14.19
*/
abstract class Enemy
{
protected int enemyX;
protected int enemyY;
protected int enemyWidth;
protected int enemyHeight;
protected int health;
protected int meleeDamage;
protected int earthDamage;
protected int shotTimer;
protected int shotFrequency;
protected Rectangle bounds = null;
protected String imgFileName;
protected BufferedImage image;
// Variables of movement counters
protected int moveCount;
protected int numEns;
protected int speed;
// Array of bullets
protected ArrayList<EnemyBullet> bullets = new ArrayList<EnemyBullet>();
// Constructor
public Enemy(int x, int y, int width, int height, String imageFile)
{
enemyX = x;
enemyY = y;
enemyWidth = width;
enemyHeight = height;
imgFileName = imageFile;
shotTimer = 0;
shotFrequency = 1500;
bounds = new Rectangle(enemyX, enemyY, enemyWidth, enemyHeight);
this.loadImage();
}
public void updateEnemy(int tickMS)
{
this.enemyMove(speed);
this.tick(tickMS);
if(this.getTimer() > this.getShotFrequency()) { this.shoot(); }
}
// Manage enemy image processing
public void loadImage()
{
BufferedImage enemyImg = null;
try
{
enemyImg = ImageIO.read(new File(imgFileName));
} catch (IOException e) {}
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D imgG = image.createGraphics();
imgG.drawImage(enemyImg, 0, 0, null);
}
public BufferedImage getImage()
{
return image;
}
// Returns the bounding Box
public Rectangle getBounds()
{
return bounds = new Rectangle(enemyX, enemyY, enemyWidth, enemyHeight);
}
public int getTimer()
{
return shotTimer;
}
public void tick(int ms)
{
shotTimer += ms;
}
public void resetTimer()
{
shotTimer = 0;
}
// Returns the shot frequency in ms
public int getShotFrequency()
{
return shotFrequency;
}
public int getX()
{
return enemyX;
}
public int getY()
{
return enemyY;
}
public int getWidth()
{
return enemyWidth;
}
public int getHeight()
{
return enemyHeight;
}
public void setX(int x)
{
enemyX = x;
}
public void setY(int y)
{
enemyY = y;
}
// Enemy movement
public void enemyMove(int speed)
{
if(moveCount >= 100) { moveCount = 0; }
if( (moveCount >= 20 && moveCount < 30) || (moveCount >= 70 && moveCount < 80) )
{
this.setY(this.getY() + speed);
moveCount++;
}
else if(moveCount < 20 || moveCount >= 80)
{
this.setX(this.getX() - speed);
moveCount++;
}
else if(moveCount < 70)
{
this.setX(this.getX() + speed);
moveCount++;
}
}
// Manage enemy bullets
abstract public void shoot();
public ArrayList<EnemyBullet> getBullets()
{
return bullets;
}
public void removeBullet(EnemyBullet b)
{
bullets.remove(b);
}
public int getMeleeDamage()
{
return meleeDamage;
}
public int getEarthDamage()
{
if(earthDamage == 0)
{
return meleeDamage;
}
else
{
return earthDamage;
}
}
// Enemy health and hurt
public int getHealth()
{
return health;
}
public void hurt(int damage)
{
health -= damage;
}
}
/**
Alien: basic enemy type
*/
class Alien extends Enemy
{
public static final int ALIEN_WIDTH = 32;
public static final int ALIEN_HEIGHT = 32;
public static final String IMG_FILE_NAME = "img/Alien.png";
// Constructor
public Alien(int x, int y)
{
super(x, y, ALIEN_WIDTH, ALIEN_HEIGHT, IMG_FILE_NAME);
meleeDamage = 1;
health = 2;
speed = 5;
shotFrequency = 1500 + (int) (Math.random() * 2000);
}
public void shoot()
{
EnemyBullet enBullet = new EnemyBullet(this.getX() + this.getWidth()/2, this.getY() + this.getHeight());
bullets.add(enBullet);
SoundHandler.playSound("sound/alshoot.wav");
this.resetTimer();
}
}
/**
Twin: basic enemy type with two bullets
*/
class Twin extends Enemy
{
public static final int TWIN_WIDTH = 32;
public static final int TWIN_HEIGHT = 32;
public static final String IMG_FILE_NAME = "img/Twin.png";
// Constructor
public Twin(int x, int y)
{
super(x, y, TWIN_WIDTH, TWIN_HEIGHT, IMG_FILE_NAME);
meleeDamage = 1;
health = 2;
speed = 5;
shotFrequency = 2500 + (int) (Math.random() * 2000);
}
public void shoot()
{
int centerX = this.getX() + this.getWidth()/2;
EnemyBullet enBullet1 = new EnemyBullet(centerX + 10, this.getY() + this.getHeight());
EnemyBullet enBullet2 = new EnemyBullet(centerX - 10, this.getY() + this.getHeight());
bullets.add(enBullet1);
bullets.add(enBullet2);
SoundHandler.playSound("sound/twshoot.wav");
this.resetTimer();
}
}
/**
Cannon: enemy type that fires 1 big bullet
*/
class Cannon extends Enemy
{
public static final int CANNON_WIDTH = 40;
public static final int CANNON_HEIGHT = 44;
public static final String IMG_FILE_NAME = "img/Cannon.png";
// Constructor
public Cannon(int x, int y)
{
super(x, y, CANNON_WIDTH, CANNON_HEIGHT, IMG_FILE_NAME);
meleeDamage = 2;
health = 6;
speed = 2;
shotFrequency = 3500 + (int) (Math.random() * 1000);
}
public void shoot()
{
EnemyMissile enMissile = new EnemyMissile(getX() + this.getWidth()/2, this.getY() + this.getHeight());
bullets.add(enMissile);
SoundHandler.playSound("sound/cashoot.wav");
this.resetTimer();
}
}
/**
Mini: small fast and in waves
*/
class Mini extends Enemy
{
public static final int WIDTH = 24;
public static final int HEIGHT = 24;
public static final String IMG_FILE_NAME = "img/Mini.png";
// Constructor
public Mini(int x, int y)
{
super(x, y, WIDTH, HEIGHT, IMG_FILE_NAME);
meleeDamage = 1;
health = 1;
speed = 10;
}
public void shoot(){}
}
/**
Hearty: basic enemy type that when killed gives life
only does damage through bullets
*/
class Hearty extends Enemy
{
public static final int HEARTY_WIDTH = 32;
public static final int HEARTY_HEIGHT = 32;
public static final String IMG_FILE_NAME = "img/Hearty.png";
// Constructor
public Hearty(int x, int y)
{
super(x, y, HEARTY_WIDTH, HEARTY_HEIGHT, IMG_FILE_NAME);
meleeDamage = 0;
health = 3;
speed = 6;
shotFrequency = 1500 + (int) (Math.random() * 2000);
}
public void shoot()
{
EnemyBullet enBullet = new EnemyBullet(this.getX() + this.getWidth()/2, this.getY() + this.getHeight());
bullets.add(enBullet);
SoundHandler.playSound("sound/alshoot.wav");
this.resetTimer();
}
}
/**
Spike: goes straight down and does more damage if it hits earth
*/
class Spike extends Enemy
{
public static final int WIDTH = 32;
public static final int HEIGHT = 32;
public static final String IMG_FILE_NAME = "img/Spike.png";
// Constructor
public Spike(int x, int y)
{
super(x, y, WIDTH, HEIGHT, IMG_FILE_NAME);
earthDamage = 4;
meleeDamage = 2;
health = 9;
speed = 4;
}
public void shoot(){}
@Override
public void enemyMove(int speed)
{
enemyY += speed;
}
}
/**
Tank: beefee unit and spwns two tankis when dies
*/
class Tank extends Enemy
{
public static final int WIDTH = 40;
public static final int HEIGHT = 40;
public static final String IMG_FILE_NAME = "img/Tank.png";
// Constructor
public Tank(int x, int y)
{
super(x, y, WIDTH, HEIGHT, IMG_FILE_NAME);
earthDamage = 0;
meleeDamage = 1;
health = 13;
speed = 2;
}
public void shoot(){}
@Override
public void enemyMove(int speed)
{
enemyY += speed;
}
}
/**
Tanki2: first slow mini spawned by tank
*/
class Tanki extends Enemy
{
public static final int WIDTH = 24;
public static final int HEIGHT = 24;
private int imgFileC;
// Constructor
public Tanki(int x, int y, int imgFileNum)
{
super(x, y, WIDTH, HEIGHT, "img/tanki" + imgFileNum + ".png");
imgFileC = imgFileNum;
meleeDamage = 1;
health = 1;
speed = 4;
}
@Override
public void enemyMove(int speed)
{
int mod;
if(imgFileC == 1) { mod = 1; }
else { mod = -1; }
if(moveCount >= 44) { moveCount = 0; }
if(moveCount < 17)
{
this.setX(this.getX() - (mod*speed));
moveCount++;
}
else if( moveCount >= 17 && moveCount < 22 )
{
this.setY(this.getY() + speed);
moveCount++;
}
else if(moveCount >= 22 && moveCount < 39)
{
this.setX(this.getX() + (mod*speed));
moveCount++;
}
else if( moveCount >= 39)
{
this.setY(this.getY() + speed);
moveCount++;
}
}
public void shoot()
{
EnemyBullet enBullet = new EnemyBullet(this.getX() + this.getWidth()/2, this.getY() + this.getHeight());
bullets.add(enBullet);
SoundHandler.playSound("sound/alshoot.wav");
this.resetTimer();
}
}