I'm working on a project where Zombies chase the player. There is a building in the world. I'm trying to get the zombies to navigate around the building, but as soon as a zombie touches a building it stops moving entirely. I'm not sure
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Zombie extends Actor
{
private GreenfootImage zombieleft;
private GreenfootImage zombieright;
public Zombie()
{
System.out.println("Zombie created.");
zombieleft = new GreenfootImage("image_zombie_left.png");
zombieright = new GreenfootImage("image_zombie_right.png");
}
public void act()
{
if (getOneIntersectingObject(Building.class) == null)
{
chasePlayer();
}
else
{
pathing();
}
}
public void chasePlayer()
{
Player player = (Player)getWorld().getObjects(Player.class).get(0);
int pX = player.getX();
int pY = player.getY();
int dx = 1;
int dy = 1;
if (getX() > pX)
{
dx = -1;
setImage(zombieleft);
}
else
{
setImage(zombieright);
}
if (getY() > pY)
{
dy = -1;
}
setLocation(getX() + dx, getY() + dy);
}
public void pathing()
{
Player player = (Player)getWorld().getObjects(Player.class).get(0);
Building building = (Building)getWorld().getObjects(Building.class).get(0);
int pX = player.getX();
int pY = player.getY();
int bX = building.getX();
int bY = building.getY();
int deltaX = player.getX() - getX();
int deltaY = player.getY() - getY();
// if player is to the right or left of the zombie, the zombie will only move up or down to clear the building
if ((getX() + 1 == bX) || (getX() - 1 == bX))
{
if (deltaY < 0)
{
setLocation(getX(), getY() - 1);
}
else
{
setLocation(getX(), getY() + 1);
}
}
// if player is above or below the zombie, the zombie will only move left or right to clear the building
if ((getY() + 1 == bY) || (getY() + 1 == bY))
{
if (deltaX < 0)
{
setLocation(getX() + 1, getY());
}
else
{
setLocation(getX() - 1, getY());
}
}
}
}
