-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpider.java
executable file
·92 lines (83 loc) · 2.06 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
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
import greenfoot.GreenfootImage;
import greenfoot.World;
/**
* Spider Class
*
* @author Adrian, Jason(graphics)
* @version June 13 2024
*/
public class Spider extends Mobs {
private final int hp;
private final double speed;
private final int dmg;
private World w;
private int direction = 1;
private long startTime;
private boolean oob = false;
private int holdAct = 0;
private boolean holding = false;
public Spider() {
hp = 2;
speed = 1;
dmg = 1;
GreenfootImage image = new GreenfootImage("spider.png");
image.scale(47, 64);
setImage(image);
}
public void addedToWorld(World w) {
this.w = w;
}
public void act() {
attackAct++;
if (checkBlock() || checkY()) {
holdAct--;
hold();
} else {
movement();
}
attack(dmg);
timeout();
super.act();
}
protected void movement() {
if (getWorld() == null || holding) {
return;
}
setLocation(getX(), getY() + (speed * direction));
}
protected void hold() {
if (!holding) {
holding = true;
holdAct = 60;
}
if (holdAct == 0) {
holding = false;
direction *= -1;
setLocation(getX(), getY() + (5 * direction));
}
}
private boolean checkBlock() {
return getOneIntersectingObject(Tile.class) != null;
}
private boolean checkY() {
return (getY() < 0 || getY() > w.getHeight());
}
public void timeout() {
if (w == null) {
return;
}
if (!oob && (getX() < 0)) {
oob = true;
startTime = System.currentTimeMillis();
}
if (oob && !(getX() < 0)) {
oob = false;
}
if (oob) {
long elapsedTime = System.currentTimeMillis() - startTime;
if (elapsedTime > 10 * 1000 && getWorld() != null) {
w.removeObject(this);
}
}
}
}