Repository navigation
Expand file tree
/
Copy pathSpike.java
More file actions
55 lines (47 loc) · 1.58 KB
/
Copy pathSpike.java
File metadata and controls
55 lines (47 loc) · 1.58 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
import java.awt.Color;
import java.awt.Graphics;
import java.util.List;
public class Spike extends KillingObject {
public Spike(int x, int y, int width, int height) {
super(x, y, width, height, "Spike");
}
public Spike(int x, int y) {
super(x, y, Game.DEFAULT_GRID_SIZE, Game.DEFAULT_GRID_SIZE, "Spike");
}
// Method to draw the spike relative to the camera's position
public void draw(Graphics g, int xOffset) {
super.draw(g, xOffset);
if (this.objectImage == null) {
g.setColor(Color.RED); // Set the color for the spike
int[] xPoints = { x - xOffset, x + width / 2 - xOffset, x + width - xOffset };
int[] yPoints = { y + height, y, y + height };
g.fillPolygon(xPoints, yPoints, 3); // Draws a triangle shape for the spike relative to the camera
}
}
/**
* Method to check collision with a KillableObjects instance and kill it if a
* collision is detected.
*/
public void checkCollision(KillableObject killable) {
// Only process collisions if the object is still alive
if (killable.isAlive() && hitDetection(killable)) {
kill(killable);
}
}
/**
* Update method to handle collisions with killable objects. This should be
* called in the game loop.
*/
public void update(KillableObject player, List<Enemy> enemies) {
// Check collision with the player
checkCollision(player);
// Check collision with each enemy
for (Enemy enemy : enemies) {
checkCollision(enemy);
}
}
@Override
public void kill(KillableObject object) {
object.die(); // Call the die method on the killable object
}
}