import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* Wombat. A Wombat is controlled by the up, down, left and right
* arrow keys. The Wombat can only move north, south, east or west.
*
* Wombats try to find leaves to eat but must go around rocks that get in the way.
*
* */
public class Wombat extends Actor
{
// constants that hold the four directions:
private static final int EAST = 0;
private static final int WEST = 180;
private static final int NORTH = 270;
private static final int SOUTH = 90;
// keep track of the wombat's current direction
private int direction;
private Counter counter;
public void fire()
{
// if(Greenfoot.isKeyDown("space"))
// {
Bullet bullet = new Bullet();
getWorld().addObject(new Bullet(), this.getX(), this.getY());
bullet.setRotation( this.getRotation() );
bullet.move(20);
/// testing
///end of testing
// }
}
public Wombat(Counter pointCounter)
{
this();
counter = pointCounter;
}
/**
* Constructor in used to review the concept of constructors
*/
public Wombat()
{
direction = EAST;
setRotation( direction );
}
/**
* Do whatever the wombat likes to to just now.
*/
public void act()
{
checkKeys();
lookForFood();
// fire();
}
/**
* Moves the wombat according to which keys are pressed
*/
public void checkKeys()
{
if (Greenfoot.isKeyDown("up")) {
direction = NORTH;
setRotation( direction );
move();
}
if (Greenfoot.isKeyDown("right")) {
direction = EAST;
setRotation( direction );
move();
}
if (Greenfoot.isKeyDown("down")) {
direction = SOUTH;
setRotation( direction );
move();
}
if (Greenfoot.isKeyDown("left")) {
direction = WEST;
setRotation( direction );
move();
}
//if(Greenfoot.isKeyDown("space")) {
// fire();
//}
if ("space".equals(Greenfoot.getKey() ) )
{
fire();
}
}
/**
* Search for food
*/
public void lookForFood()
{
Actor leaf = getOneIntersectingObject( Leaf.class );
if (leaf != null) {
getWorld().removeObject(leaf);
counter.setValue(counter.getValue()+1);}
}
/**
* Returns the number of leaves the wombat has eaten
*/
// public int getLeavesEaten()
//{
// return numOfLeaf;//counter.getValue();
//counter.setValue(counter.getValue()+1);
//}
/**
* Moves the wombat forward one location
*/
public void move()
{
if (canMove()) {
switch( getRotation() ) {
case EAST:
setLocation( getX() + 1, getY() );
break;
case SOUTH:
setLocation( getX(), getY()+1 );
break;
case WEST:
setLocation( getX() - 1, getY() );
break;
case NORTH:
setLocation( getX(), getY() - 1);
break;
}
}
}
/**
* Determines if a Rock is located in front of the Wombat
*/
public boolean canMove()
{
switch( getRotation() ) {
case EAST:
return getOneObjectAtOffset(25,0,wallRect.class)==null;
case SOUTH:
return getOneObjectAtOffset(0,25,wallRect.class)==null;
case WEST:
return getOneObjectAtOffset(-25,0,wallRect.class)==null;
case NORTH:
return getOneObjectAtOffset(0,-25,wallRect.class)==null;
}
return true;
}
}
