-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.java
More file actions
168 lines (139 loc) · 4.75 KB
/
Copy pathEnemy.java
File metadata and controls
168 lines (139 loc) · 4.75 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import greenfoot.*;
import java.util.List;
/**
* Kelas dasar untuk semua musuh (Enemy).
* Musuh akan mengejar Player.
* Musuh juga tidak bisa menembus tembok (Wall01–Wall09).
*/
public class Enemy extends Actor {
protected int baseHealth = 200;
protected int baseSpeed = 2;
protected int health;
protected int damage = 15;
protected int speed;
private Actor target;
public Enemy() {
adjustStatsByLevel();
}
private void adjustStatsByLevel() {
int currentLevel = 1;
World world = getWorld();
if (world != null) {
if (world instanceof Level3) {
currentLevel = 3;
} else if (world instanceof Level2) {
currentLevel = 2;
} else if (world instanceof Level1) {
currentLevel = 1;
}
}
}
@Override
protected void addedToWorld(World world) {
adjustStatsByLevelAtRuntime(world);
}
private void adjustStatsByLevelAtRuntime(World world) {
int levelMultiplier = 1;
double speedMultiplier = 1.0;
if (world instanceof Level3) {
levelMultiplier = 3;
speedMultiplier = 1.5;
} else if (world instanceof Level2) {
levelMultiplier = 2;
speedMultiplier = 1.25;
}
this.health = baseHealth + ((levelMultiplier - 1) * (baseHealth / 2));
this.speed = (int) Math.round(baseSpeed * speedMultiplier);
if (this.speed == 0) this.speed = 1;
}
public void act() {
if (getWorld() == null) return;
chaseTarget();
checkWallCollision();
checkHitTarget();
}
private boolean willHitWall(int x, int y) {
for (Class<? extends Actor> wallClass : new Class[]{Wall01.class, Wall02.class, Wall03.class,
Wall04.class, Wall05.class, Wall06.class,
Wall07.class, Wall08.class, Wall09.class}) {
List<? extends Actor> walls = getWorld().getObjects(wallClass);
for (Actor wall : walls) {
if (wall != null) {
int dx = x - wall.getX();
int dy = y - wall.getY();
double distance = Math.hypot(dx, dy);
if (distance < (getImage().getWidth() + wall.getImage().getWidth()) / 2) {
return true;
}
}
}
}
return false;
}
protected void chaseTarget() {
World world = getWorld();
if (world == null) return;
Player player = (Player) findNearestTarget(Player.class);
if (player != null && !player.isDead()) {
target = player;
} else {
target = null;
}
if (target != null) {
turnTowards(target.getX(), target.getY());
int nextX = getX() + (int)(Math.cos(Math.toRadians(getRotation())) * speed);
int nextY = getY() + (int)(Math.sin(Math.toRadians(getRotation())) * speed);
if (!willHitWall(nextX, nextY)) {
move(speed);
}
}
}
protected void checkWallCollision() {
if (isTouching(Wall01.class) || isTouching(Wall02.class) || isTouching(Wall03.class) ||
isTouching(Wall04.class) || isTouching(Wall05.class) || isTouching(Wall06.class) ||
isTouching(Wall07.class) || isTouching(Wall08.class) || isTouching(Wall09.class)) {
move(-speed);
}
}
protected Actor findNearestTarget(Class<? extends Actor> cls) {
if (getWorld() == null) return null;
List<? extends Actor> objects = getWorld().getObjects(cls);
if (objects.isEmpty()) return null;
Actor nearest = null;
double nearestDistance = Double.MAX_VALUE;
for (Actor obj : objects) {
double distance = Math.hypot(getX() - obj.getX(), getY() - obj.getY());
if (distance < nearestDistance) {
nearest = obj;
nearestDistance = distance;
}
}
return nearest;
}
protected void checkHitTarget() {
if (target instanceof Player) {
Player p = (Player) getOneIntersectingObject(Player.class);
if (p != null && !p.isDead()) {
p.takeDamage(damage);
}
}
}
public void takeDamage(int amount) {
health -= amount;
if (health <= 0) {
die();
}
}
public void die() {
World world = getWorld();
if (world != null) {
world.removeObject(this);
}
}
public boolean isDead() {
return health <= 0;
}
public int getHealth() {
return health;
}
}