-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rocket.java
51 lines (44 loc) · 1.31 KB
/
Rocket.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* A rocket can be fired from an ambulance to destroy ladybugs
*
* @author Manuel Kienlein
*/
public class Rocket extends Actor
{
private int speed = 6;
private int direction;
private boolean autoFollow;
public Rocket(int direction, boolean autoFollow)
{
this.direction = direction;
this.autoFollow = autoFollow;
turn(direction);
}
public void act()
{
move(speed);
if(autoFollow){
Ladybug nearestLadybug = null;
java.util.List ladybugs = getNeighbours(5000, true, Ladybug.class);
if(ladybugs.size() > 0){
nearestLadybug = (Ladybug)ladybugs.get(0);
}
if(nearestLadybug != null){
turnTowards(nearestLadybug.getX(), nearestLadybug.getY());
}
}
if(isAtEdge()){
SpaceWorld w = (SpaceWorld)this.getWorld();
w.despawnObject(this);
return;
}
if(isTouching(Ladybug.class)){
removeTouching(Ladybug.class);
SpaceWorld w = (SpaceWorld)this.getWorld();
w.checkForRespawn();
w.despawnObject(this);
return;
}
}
}