-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
148 lines (121 loc) · 4.16 KB
/
Player.java
File metadata and controls
148 lines (121 loc) · 4.16 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.awt.*;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
/**
* Represents the player in the Space Invaders game.
* This class manages the player's position, movement, and actions such as
* firing bullets.
*/
public final class Player {
private int x;
private int y;
private long lastBulletTime;
private final List<Bullet> bullets;
public static final int PLAYER_SIZE = 40;
/**
* Constructs a new Player with specified coordinates.
*
* @param x The x-coordinate of the player.
* @param y The y-coordinate of the player.
*/
public Player(int x, int y) {
this.x = x;
this.y = y;
this.bullets = new ArrayList<>();
this.lastBulletTime = System.currentTimeMillis();
}
/**
* Moves the player to the left within the bounds of the game screen.
*/
public final void moveLeft() {
x = Math.max(x - 10, 0);
}
/**
* Moves the player to the right within the bounds of the game screen.
*/
public final void moveRight() {
x = Math.min(x + 10, 700);
}
/**
* Updates the state of the player and its bullets.
* This includes moving the bullets and removing any that are out of bounds.
*/
public final void update() {
// Update player bullets
Iterator<Bullet> playerBulletIterator = bullets.iterator();
while (playerBulletIterator.hasNext()) {
Bullet bullet = playerBulletIterator.next();
bullet.update();
// Remove bullets that are out of bounds
if (bullet.getY() < 0) {
playerBulletIterator.remove();
}
}
}
/**
* Fires a bullet from the player's position if the cooldown period has elapsed.
*/
public final void fireBullet() {
long currentTime = System.currentTimeMillis();
if (currentTime - lastBulletTime > 4000) {
bullets.add(new Bullet(x + PLAYER_SIZE / 2, y, -1));
}
}
/**
* Renders the player on the game panel.
*
* @param g2d The Graphics2D object used for drawing.
*/
public final void render(Graphics2D g2d) {
g2d.fillRect(x, y, 40, 40);
}
/**
* Checks if the player intersects with a given rectangle.
* This method is commonly used for collision detection.
*
* @param other The rectangle to check for intersection with the player.
* @return true if the player intersects with the given rectangle, false
* otherwise.
*/
public final boolean intersects(Rectangle other) {
return new Rectangle(x, y, 40, 40).intersects(other);
}
/**
* Checks if the player intersects with an alien.
* This is used to detect collisions between the player and aliens.
*
* @param alien The alien to check for intersection.
* @return true if the player intersects with the given alien, false otherwise.
*/
public final boolean intersects(Alien alien) {
return intersects(new Rectangle(alien.getX(), alien.getY(), Alien.ALIEN_SIZE, Alien.ALIEN_SIZE));
}
/**
* Checks if the player intersects with a bullet.
* This is used to detect collisions between the player and bullets.
*
* @param bullet The bullet to check for intersection.
* @return true if the player intersects with the given bullet, false otherwise.
*/
public final boolean intersects(Bullet bullet) {
return intersects(new Rectangle(bullet.getX(), bullet.getY(), Bullet.BULLET_SIZE, Bullet.BULLET_SIZE));
}
/**
* Checks if the player intersects with a given obstacle.
* This is used to detect collisions between the player and obstacles.
*
* @param obstacle The obstacle to check for intersection.
* @return true if the player intersects with the given obstacle, false
* otherwise.
*/
public final boolean intersects(Obstacle obstacle) {
return intersects(new Rectangle(obstacle.getX(), obstacle.getY(), Obstacle.WIDTH, Obstacle.HEIGHT));
}
public final int getX() {
return x;
}
public final int getY() {
return y;
}
}