-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThrowable.java
More file actions
63 lines (53 loc) · 1.93 KB
/
Copy pathThrowable.java
File metadata and controls
63 lines (53 loc) · 1.93 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
import javax.swing.*;
public abstract class Throwable extends GameObject {
protected double velocityX, velocityY;
protected int damage;
protected boolean isSliding;
private static final double GRAVITY = 0.2;
private static final double BOUNCE_REDUCTION = 0.6;
private static final double SLIDE_FRICTION = 0.1;
public Throwable(int x, int y, ImageIcon image, double initialVelocity, double angle, int damage) {
super(x, y, image); // Call the parent (GameObject) constructor
this.damage = damage;
isSliding = false;
// Calculate initial velocities based on throw angle
this.velocityX = initialVelocity * Math.cos(Math.toRadians(angle));
this.velocityY = initialVelocity * Math.sin(Math.toRadians(angle));
}
public void update() {
if (!isSliding) {
x += velocityX;
y += velocityY;
velocityY += GRAVITY; // Apply gravity
} else {
slowDown();
}
}
@Override
public boolean checkCollision(GameObject object) {
boolean collided = super.checkCollision(object);
if (collided) {
if (object instanceof Fence || object instanceof Player) {
handleBounceCollision();
} else if (object instanceof LandObstacle) {
handleGroundCollision();
}
}
return collided;
}
protected void handleBounceCollision() {
velocityX *= -BOUNCE_REDUCTION; // Reverse direction, lose some velocity
isSliding = true;
}
private void handleGroundCollision() {
isSliding = true;
}
private void slowDown() {
if (Math.abs(velocityX) > 0.1) {
velocityX -= Math.signum(velocityX) * SLIDE_FRICTION;
} else {
velocityX = 0;
isSliding = false;
}
}
}