-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.java
More file actions
90 lines (82 loc) · 3.01 KB
/
Bullet.java
File metadata and controls
90 lines (82 loc) · 3.01 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
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
public class Bullet {
private int x, y;
private int dx, dy;
private int size = 12; // ADJUST FOR SIZE
private int speed = 10;
private GamePanel panel;
private Image bulletImage, bulletUpImage, bulletDownImage, bulletLeftImage, bulletRightImage;
private int carDirection;
public Bullet(GamePanel panel, int height, int width, int startX, int startY, int direction) {
this.panel = panel;
int carCenterX = startX + width / 2;
int carCenterY = startY + height / 2;
if(direction == 0){
this.x = carCenterX;
this.y = startY;
} else if(direction == 1){
this.x = carCenterX;
this.y = startY + height;
} else if(direction == 2){
this.x = startX;
this.y = carCenterY;
} else if(direction == 3){
this.x = startX + width;
this.y = carCenterY;
}
this.dx = 0;
this.dy = -speed;
this.carDirection = direction;
this.bulletUpImage = ImageManager.loadImage("images/bulletUp.png");
this.bulletDownImage = ImageManager.loadImage("images/bulletDown.png");
this.bulletLeftImage = ImageManager.loadImage("images/bulletLeft.png");
this.bulletRightImage = ImageManager.loadImage("images/bulletRight.png");
this.bulletImage = bulletUpImage;
}
public void shoot() {
if(carDirection== 0){
dx=0;
dy= -speed;
bulletImage= bulletUpImage;
}else if(carDirection== 1){
dx=0;
dy= speed;
bulletImage= bulletDownImage;
}else if(carDirection== 2){
dx= -speed;
dy=0;
bulletImage= bulletLeftImage;
}else if(carDirection== 3){
dx= speed;
dy=0;
bulletImage= bulletRightImage;
}
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.YELLOW);
g2.fillOval(drawX, drawY, size, size);
}
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double(x - size/2, y - size/2, size, size);
}
public boolean isOffScreen() {
TileMap tileMap = panel.getTileMap();
if (tileMap == null) return false;
double cameraX = tileMap.getCameraX();
double cameraY = tileMap.getCameraY();
int screenWidth = panel.getWidth();
int screenHeight = panel.getHeight();
return x < cameraX || x > cameraX + screenWidth || y < cameraY || y > cameraY + screenHeight;
}
}