-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemyPlane.java
67 lines (66 loc) · 1.92 KB
/
EnemyPlane.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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class EnemyPlane extends GameObject{
private Handler handler;
private HUD hud;
private Random r = new Random();
public EnemyPlane(int x, int y, ID id, Handler handler, HUD hud) {
super(x,y,id);
this.handler = handler;
this.hud = hud;
velX = Math.negateExact(r.nextInt(5)+3);
velY = 0;
}
public void render(Graphics g) {
//draw body
g.setColor(Color.RED);
g.fillRect((int)x, (int)y, 48, 16);
//draw wings
g.setColor(Color.BLUE);
g.drawLine((int)x+20, (int)y, (int)x+38, (int)y-16);
g.drawLine((int)x+48, (int)y, (int)x+66, (int)y-16);
g.drawLine((int)x+66, (int)y-16, (int)x+38, (int)y-16);
g.drawLine((int)x+15, (int)y+14, (int)x+33, (int)y+32);
g.drawLine((int)x+43, (int)y+14, (int)x+61, (int)y+32);
g.drawLine((int)x+61, (int)y+32, (int)x+33, (int)y+32);
}
//repeatedly called to detect hits
public void collision() {
for (int i = 0; i< handler.object.size();i++) {
GameObject tempObject = handler.object.get(i);
if (tempObject.getId() == ID.PlayerBullet) {
for(Rectangle r: getAllBounds()) {
//if bullet hits plane
if(r.getBounds().intersects(tempObject.getBounds())) {
handler.removeObject(this);
handler.removeObject(tempObject);
hud.score(hud.getScore()+50);
}
}
}
}
}
@Override
public void tick() {
x += velX;
y += velY;
//if plane is out of scene, remove plane
if (x<=-20 ) {
handler.removeObject(this);
}
collision();
}
//return hit detection boxes
public Rectangle getBounds() {
return null;
}
public Rectangle[] getAllBounds() {
Rectangle[] bounds = new Rectangle[3];
bounds[0] = new Rectangle((int)x,(int)y,48,16);
bounds[1] = new Rectangle((int)x+20,(int)y-16,46,16);
bounds[2] = new Rectangle((int)x+20,(int)y+16,46,16);
return bounds;
}
}