-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyBullet.java
More file actions
72 lines (63 loc) · 2.22 KB
/
EnemyBullet.java
File metadata and controls
72 lines (63 loc) · 2.22 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
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class EnemyBullet {
private int x, y;
private int dx, dy;
private int size = 5;
private int speed = 8;
private GamePanel panel;
private Image bulletImage;
private int enemyIndex;
public EnemyBullet(GamePanel panel, int width, int startX, int startY, Car target, int enemyIndex) {
this.panel = panel;
this.enemyIndex = enemyIndex;
this.x = startX + width/2;
this.y = startY;
if (target != null) {
int targetX = target.getX() + target.getWidth()/2;
int targetY = target.getY() + target.getHeight()/2;
int deltaX = targetX - x;
int deltaY = targetY - y;
double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if(distance == 0)
distance = 1;
this.dx = (int)(speed * deltaX / distance);
this.dy = (int)(speed * deltaY / distance);
} else {
this.dx = 0;
this.dy = speed;
}
bulletImage= ImageManager.loadImage("images/enemyBullet.png");
}
public void shoot() {
x += dx;
y += dy;
}
public void draw(Graphics2D g2, double cameraX, double cameraY) {
int drawX = (int)(x - cameraX - size/2);
int drawY = (int)(y - cameraY - size/2);
if (bulletImage != null) {
g2.drawImage(bulletImage, drawX, drawY, size, size, null);
} else {
g2.setColor(Color.RED);
g2.fillOval(drawX, drawY, size, size);
}
}
public boolean isOffScreen() {
TileMap tileMap = panel.getTileMap();
if (tileMap == null) return false;
int worldWidth = tileMap.getWidthPixels();
int worldHeight = tileMap.tilesToPixels(tileMap.getHeight());
return x < 0 || x > worldWidth || y < 0 || y > worldHeight;
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double(x - size/2, y - size/2, size, size);
}
public int getEnemyIndex() {
return enemyIndex;
}
}