/* Love Bug Code
* By Andrew Woods
* APSI Las Vegas June 2010
* LoveBug follows a given Bug around the screen
*/
import info.gridworld.actor.Bug;
import info.gridworld.grid.Location;
/**
* A <code>LoveBug</code> points in the direction of the given bug,
* and moves towards that bug. Step offset can be used to skip acts.
*/
public class LoveBug extends Bug
{
private Bug myLove ;
private int intStepOffset = 0 ;
private int intStep = 0 ;
/**
*
*/
public LoveBug( Bug inputBug )
{
myLove = inputBug ;
}
public LoveBug( Bug inputBug , int inputOffset)
{
myLove = inputBug ;
intStepOffset = inputOffset ;
}
/**
* Moves to the next location of the square.
*/
public void act()
{
//set the direction based on the return of the method
//that points the bug towards
//their specified bug
setDirection ( this.getLocation().getDirectionToward( myLove.getLocation() ) );
//Move if possible to move
if ( canMove() && intStep == 0)
{
move();
//reset the step counter
intStep = intStepOffset ;
}//End If
else if ( intStep > 0 )
{
intStep--;
}//End Else if
}
}
***********************************************************************
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;
/**
* This class runs a world that contains box bugs. <br />
* This class is not tested on the AP CS A and AB exams.
*/
public class BoxBugRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
//first set
BoxBug alice = new BoxBug(6);
alice.setColor(Color.RED);
LoveBug bob = new LoveBug( alice , 1 );
bob.setColor( Color.PINK );
world.add(new Location(7, 8), alice);
world.add(new Location(5, 5), bob);
//Second set
BoxBug june = new BoxBug(6);
june.setColor(Color.BLUE);
LoveBug kyle = new LoveBug( june, 2 );
kyle.setColor( Color.CYAN );
world.add(new Location(0, 0), june);
world.add(new Location(8, 8), kyle);
//Show
world.show();
}
}