Repository navigation
Expand file tree
/
Copy pathPlayer.java
More file actions
111 lines (94 loc) · 3.78 KB
/
Copy pathPlayer.java
File metadata and controls
111 lines (94 loc) · 3.78 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
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.Node;
// The Player class represents the controllable tank in the game.
public class Player extends ImageView implements GameObject {
// Fields
private static final double SPEED = 1;
private static final int SIZE = 32;
private static final long RELOAD_INTERVAL_MS = 500;
private Image sprite1;
private Image sprite2;
private boolean spriteToggle = false;
private String facingDirection = "RIGHT";
private long lastFireTime = 0;
// Constructs the player tank at the given (x, y) position.
public Player(double x, double y) {
sprite1 = new Image("file:assets/yellowTank1.png");
sprite2 = new Image("file:assets/yellowTank2.png");
setImage(sprite1);
setFitWidth(SIZE);
setFitHeight(SIZE);
setTranslateX(x);
setTranslateY(y);
}
// Moves the player tank in the specified direction if no wall collision occurs.
@Override
public void move(String direction) {
this.facingDirection = direction;
double nextX = getTranslateX();
double nextY = getTranslateY();
// Calculate next position and rotate sprite accordingly
switch (direction) {
case "UP":
nextY -= SPEED;
setRotate(270);
break;
case "DOWN":
nextY += SPEED;
setRotate(90);
break;
case "LEFT":
nextX -= SPEED;
setRotate(180);
break;
case "RIGHT":
nextX += SPEED;
setRotate(0);
break;
}
boolean canMove = true;
// Check collision with walls
if (getParent() instanceof GameManager) {
GameManager gamePane = (GameManager) getParent();
for (Node node : gamePane.getChildren()) {
if (node instanceof Wall) {
double wallX = node.getTranslateX();
double wallY = node.getTranslateY();
boolean horizontallyOverlapping = nextX + getFitWidth() > wallX &&
nextX < wallX + Wall.WIDTH;
boolean verticallyOverlapping = nextY + getFitHeight() > wallY &&
nextY < wallY + Wall.HEIGHT;
boolean collidesWithWall = horizontallyOverlapping && verticallyOverlapping;
if (collidesWithWall) {
canMove = false;
break;
}
}
}
}
// Apply movement and toggle sprite if no collision
if (canMove) {
setTranslateX(nextX);
setTranslateY(nextY);
spriteToggle = !spriteToggle;
setImage(spriteToggle ? sprite2 : sprite1); // Simulate movement animation
}
}
//Fires a bullet in the direction the player is currently facing.
//Enforces a reload delay between shots.
public void fire(GameManager gameManager) {
long now = System.currentTimeMillis();
if (now - lastFireTime < RELOAD_INTERVAL_MS) return;
lastFireTime = now;
// Bullet starts at center of player sprite
double bulletX = getTranslateX() + getFitWidth() / 2 - 5;
double bulletY = getTranslateY() + getFitHeight() / 2 - 5;
new Bullet(bulletX, bulletY, facingDirection, gameManager);
}
// Triggers an explosion effect when the player is destroyed.
@Override
public void destroy() {
new Explosion(getTranslateX(), getTranslateY(), (GameManager) getParent());
}
}