-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpider.java
executable file
·58 lines (52 loc) · 1.4 KB
/
Spider.java
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
import greenfoot.World;
public class Spider extends Mobs {
private final int hp;
private final double speed;
private final int dmg;
private World w;
private long startTime;
private boolean oob = false;
private int direction = 1;
public Spider() {
hp = 2;
speed = 0.5;
dmg = 1;
}
public void addedToWorld(World w) {
this.w = w;
}
public void act() {
movement();
attack();
timeout();
}
protected void movement() {
if (getWorld() == null) {
return;
}
turnTowards(direction * 999, getY());
if (getOneObjectAtOffset(direction * getImage().getWidth(), 0, Brick.class) != null) {
direction *= -1;
} else {
move(speed);
}
}
public void timeout() {
if (w == null) {
return;
}
if (!oob && (getX() < 0 || getX() > w.getWidth() || getY() < 0 || getY() > w.getHeight())) {
oob = true;
startTime = System.currentTimeMillis();
}
if (oob && !(getX() < 0 || getX() > w.getWidth() || getY() < 0 || getY() > w.getHeight())) {
oob = false;
}
if (oob) {
long elapsedTime = System.currentTimeMillis() - startTime;
if (elapsedTime > 10 * 1000) {
w.removeObject(this);
}
}
}
}