// WARNING: This file is auto-generated and any changes to it will be overwritten
import java.util.*;
import greenfoot.*;
import java.awt.Color;
/**
*
*/
public class Roomba extends Actor
{
/* constants. speed + capacity + sensingRadius/10 must be 20 or less*/
private static final int speed = 1;
private static final int capacity = 8;
private static final int sensorRadius = 11;
/* variables*/
private int capacityLeft = capacity;
private int dirtCounter = 0;
private int unloadingDelay = 0;
/* every Roomba has a Base*/
public Base myBase = null;
/* a list with dirt around the Roomba (useful for sensing?)*/
private List<Dirt> dirtInRange;
private List<Barrier> barrierInRange;
/**
*
*/
public void act()
{
/* the next three lines are required for the delay while unloading the dirt*/
if (unloadingDelay > 0) {
unloadingDelay = unloadingDelay - 1;
return;
}
/* the next three lines check whether all dirt (25 pieces) has been removed*/
if (dirtCounter == 25) {
Greenfoot.stop();
}
move();
clean();
sense();
if (capacityLeft == 0) {
dump();
}
if (capacityLeft > 10) {
sense();
}
}
/**
*
*/
public void move()
{
move(speed);
Actor Barrier = getOneObjectAtOffset(getX(), getY() + getImage().getHeight() * 2, Barrier.class);
if (Barrier != null) {
setLocation(getX(), getY() + 5);
}
if (isTouching(Barrier.class)) {
turn(160);
}
if (isAtEdge()) {
turn(Greenfoot.getRandomNumber(180));
}
}
/**
*
*/
public void clean()
{
if (isTouching(Dirt.class) && capacityLeft > 0) {
removeTouching(Dirt.class);
capacityLeft = capacityLeft - 1;
dirtCounter = dirtCounter + 1;
}
}
/**
*
*/
public void sense()
{
List<Dirt> dirtInRange = getObjectsInRange(sensorRadius, Dirt.class);
List<Barrier> barrierInRange = getObjectsInRange(10, Barrier.class);
List<Barrier> intersectingActorsList = getIntersectingObjects(Barrier.class);
if ( ! dirtInRange.isEmpty()) {
Dirt firstActor = dirtInRange.get(0);
turnTowards(firstActor.getX(), firstActor.getY());
}
if ( ! intersectingActorsList.isEmpty() && ! dirtInRange.isEmpty()) {
Barrier firstActor = intersectingActorsList.get(0);
setLocation(getX(), getY() + 5);
}
else if ( ! barrierInRange.isEmpty()) {
if ( ! barrierInRange.isEmpty() && ! dirtInRange.isEmpty()) {
Barrier firstActor = barrierInRange.get(0);
move(-4);
}
}
}
/**
*
*/
public void dump()
{
turnTowards(myBase.getX(), myBase.getY());
move();
if (isTouching(Base.class)) {
unload();
}
}
/**
*
*/
public void unload()
{
capacityLeft = capacity;
unloadingDelay = 1000;
}
}
