I am having trouble figuring out how to write the code to check if a wall is beside the player.
Also, I am unsure of how to write the facingWall Method. Help greatly appreciated!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class Player extends Actor
{
private static final int EAST = 0;
private static final int SOUTH = 90;
private static final int WEST = 180;
private static final int NORTH = 270;
public Player()
{
GreenfootImage endpoint = new GreenfootImage(25,25);
endpoint.setColor(Color.BLUE);
endpoint.fill();
setImage(endpoint);
}
public boolean wallOnLeft()
{
return false;
}
public boolean canWalk()
{
int xOffset=0, yOffset=0;
switch( getRotation() ) {
case EAST: xOffset=1; break;
case SOUTH: yOffset=1; break;
case WEST: xOffset=-1; break;
case NORTH: yOffset=-1; break;
}
Actor wall = getOneObjectAtOffset(xOffset,yOffset,Wall.class);
return wall != null;
//return !facingWall() && !facingEdge();
}
public boolean facingWall()
{
switch ( getRotation()) {
case EAST: return getX()==getWorld().getWidth()-1;
case SOUTH: return getY()==getWorld().getHeight()-1;
case WEST: return getX()==0;
case NORTH: return getY()==0;
}
return false;
}
public void walk()
{
int dx=0, dy=0;
switch( getRotation() ) {
case EAST: dx=1; break;
case SOUTH: dy=1; break;
case WEST: dx=-1; break;
case NORTH: dy=-1; break;
}
setLocation(getX()+dx,getY()+dy);
}
public void turnLeft()
{
setRotation(getRotation()-90);
}
public void turnRight()
{
setRotation(getRotation()+90);
}
public boolean facingEdge()
{
switch ( getRotation()) {
case EAST: return getX()==getWorld().getWidth()-1;
case SOUTH: return getY()==getWorld().getHeight()-1;
case WEST: return getX()==0;
case NORTH: return getY()==0;
}
return false;
}
public void act()
{
if(wallOnLeft()) {
if(canWalk()) {
walk();
}else{
turnRight();
}
}else{
turnLeft();
walk();
}
}
}
