Repository navigation
Expand file tree
/
Copy pathBullet.java
More file actions
99 lines (87 loc) · 3.63 KB
/
Copy pathBullet.java
File metadata and controls
99 lines (87 loc) · 3.63 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
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.animation.AnimationTimer;
import javafx.scene.Node;
// The Bullet class represents a projectile fired in a specific direction.
public class Bullet extends ImageView {
// Fields
private static final int SPEED = 5;
private AnimationTimer timer;
private boolean paused = false;
// Constructs a Bullet object at the given (x, y) position, traveling in the specified direction.
public Bullet(double x, double y, String direction, GameManager gameManager) {
Image bulletImage = new Image("file:assets/bullet.png");
setImage(bulletImage);
setFitWidth(10);
setFitHeight(13);
setTranslateX(x);
setTranslateY(y);
int xVelocity = 0;
int yVelocity = 0;
int rotationAngle = 0;
// Determine velocity vector and rotation angle based on direction
switch (direction) {
case "UP":
yVelocity = -1;
rotationAngle = 270;
break;
case "DOWN":
yVelocity = 1;
rotationAngle = 90;
break;
case "LEFT":
xVelocity = -1;
rotationAngle = 180;
break;
case "RIGHT":
xVelocity = 1;
rotationAngle = 0;
break;
}
final int finalXVelocity = xVelocity;
final int finalYVelocity = yVelocity;
setRotate(rotationAngle);
// Add the bullet to the game scene
gameManager.getChildren().add(this);
// Timer that moves the bullet each frame
timer = new AnimationTimer() {
@Override
public void handle(long now) {
if (paused){
return; // Do not update if the game is paused
}
// Update bullet's position
setTranslateX(getTranslateX() + finalXVelocity * SPEED);
setTranslateY(getTranslateY() + finalYVelocity * SPEED);
// Remove bullet if it goes outside the game area
if (getTranslateX() < 0 || getTranslateX() > 800 || getTranslateY() < 0 || getTranslateY() > 700) {
stop();
gameManager.getChildren().remove(Bullet.this);
return;
}
// Check for collision with Walls
for (Node node : gameManager.getChildren()) {
if (node instanceof Wall && getBoundsInParent().intersects(node.getBoundsInParent())) {
stop();
new SmallExplosion(getTranslateX(), getTranslateY(), gameManager);
gameManager.getChildren().remove(Bullet.this);
return;
}
// Check for collision with Enemies
if (node instanceof Enemy && getBoundsInParent().intersects(node.getBoundsInParent())) {
stop();
((GameObject) node).destroy(); // Remove or damage the enemy
gameManager.addScore(1); // Increment score
gameManager.getChildren().remove(Bullet.this); // Remove bullet
return;
}
}
}
};
timer.start(); // Start bullet movement
}
// Pauses or resumes bullet movement.
public void setPaused(boolean value) {
this.paused = value;
}
}