import info.gridworld.actor.Actor;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
public class RollingStone extends Rock
{
public RollingStone()
{
super();
setDirection(90);
}
public void act ()
{
if (canMove())
move();
else
turn();
}
public void turn()
{
setDirection(getDirection() + Location.HALF_CIRCLE);
}
public void move ()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
Actor neighbor = gr.get(next);
if (neighbor != null)
neighbor.removeSelfFromGrid();
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
public boolean canMove()
{
Grid<Actor> gg = getGrid();
if(gg == null)
return false;
Location last = getLocation();
Location next = last.getAdjacentLocation(getDirection());
Actor neighbor = gg.get(next);
return true;
}
}