public class Human extends Actor
{
int offsetX = 0; //global variable storing the random X direction (1, 0 or -1)
int offsetY = 0; //global variable storing the random Y direction (1, 0 or -1)
public Human() // This sets the function for the Human
{
setDirection(); // This sets the direction for the Human
}
/**
* Human will walk continuously in a random direction turning randomly when it hits something
* if the Human hits a spider it will eat it.
*/
public void act() // This defines the way that the Human moves (Greenfoot is in control)
{
int currX = getX(); //gets the currect X co-ordinate
int currY = getY(); //gets the currect Y co-ordinate
if(canMove(currX + offsetX, currY + offsetY)) //
setLocation(currX + offsetX, currY + offsetY); //
else
setDirection(); //this sets the new direction of the Human
}
private void setDirection() //This is the function that the Human is in control of, the function defines the amount of rotation the Human does per move.
{
do{
offsetX = Greenfoot.getRandomNumber(3) - 1;// This generates a random number, it will be -1, 0 or 1. Stored in X
offsetY = Greenfoot.getRandomNumber(3) - 1;//This generates a random number, it will be -1, 0 or 1. Stored in Y
}while(offsetX==0 && offsetY==0);
if(offsetX == -1 && offsetY == -1) //if the Human will go north-west
setRotation(315); //set rotation to north-west
else if(offsetX == -1 && offsetY == 0) //if the Human will go west
setRotation(270); //set rotation to west
else if(offsetX == -1 && offsetY == 1) //if the Human will go south-west
setRotation(225); //set rotation to south west
else if(offsetX == 0 && offsetY == -1) //if the Human will go north
setRotation(0); //set rotation to north
else if(offsetX == 0 && offsetY == 1) //if the Human will go south
setRotation(180); //set rotation to south
else if(offsetX == 1 && offsetY == -1) //if the Human will go north east
setRotation(45); //set rotation to north east
else if(offsetX == 1 && offsetY == 0) //if the Human will go East
setRotation(90); //set rotation to East
else if(offsetX == 1 && offsetY == 1) //if the lizard will go south east
setRotation(135); //set rotation to South-East
}
private boolean canMove(int x,int y) //
{
List foundTree = getWorld().getObjectsAt(x,y, Tree.class); //canMove is false if a Wall is found
if(!foundTree.isEmpty ()) return false; //if wall found return false and stop otherwise continue
List foundLampPost = getWorld().getObjectsAt(x,y, LampPost.class); //canMove is false if a Wall is found
if(!foundLampPost.isEmpty ()) return false; //if wall found return false and stop otherwise continue
List foundPurpleCar = getWorld().getObjectsAt(x,y, PurpleCar.class); //canMove is false if a Wall is found
if(!foundPurpleCar.isEmpty ()) return false; //if wall found return false and stop otherwise continue
List foundYellowCar = getWorld().getObjectsAt(x,y, YellowCar.class); //canMove is false if a Wall is found
if(!foundYellowCar.isEmpty ()) return false; //if wall found return false and stop otherwise continue
List foundGreenCar = getWorld().getObjectsAt(x,y, GreenCar.class); //canMove is false if a Wall is found
if(!foundGreenCar.isEmpty ()) return false; //if wall found return false and stop otherwise continue
return true; //this must be at the end - canMove will only be true if it got this far ie it didn't find any of the listed objects.
}
}
