I want to implement code where a zombie chases after a person but I don't want the image for the actor rotate because it looks weird. A solution would be appreciated. Thanks!
private void targetClosestHuman()
{
int dist = 400;
Actor closest = null;
if(!getObjectsInRange(dist, Citizen.class).isEmpty())
{
for (Object obj: getObjectsInRange(dist, Citizen.class))
{
Actor guy = (Actor) obj;
int guyDist = (int)Math.hypot(guy.getX() - getX(), guy.getY() - getY());
if (closest == null || guyDist< dist)
{
closest = guy;
dist = guyDist;
}
}
turnTowards(closest.getX(),closest.getY());
}
}if (obj instanceof Zombie) continue; //rest of for-loop
public void act()
{
if (hp > 0){ //if zombie has more then 0 hp it will remain active
hp -= VehicleWorld.ZOMBIE_DEATH_RATE;
hpBar.update(hp);
//Move
targetClosestHuman();
}else if(getY() < 100){
getWorld().removeObject(this);
}else if ( getY() > 550){
getWorld().removeObject(this);
}else{ //dies when it reaches 0 hp
getWorld().removeObject(this);
}
} private void targetClosestHuman()
{
int dist = 100;
Actor closest = null;
if(!getObjectsInRange(dist, Pedestrian.class).isEmpty())
{
for (Object obj: getObjectsInRange(dist, Pedestrian.class))
{
if (obj instanceof Zombie) continue; // loops through all actors in Pedestrian class and ignores all Zombies
Actor guy = (Actor) obj;
int guyDist = (int)Math.hypot(guy.getX() - getX(), guy.getY() - getY());
if (closest == null || guyDist < dist)
{
closest = guy;
dist = guyDist;
}
}
//So my actor doesnt appear to rotate when he moves after a human
setRotation(rotation); //Before turning and moving it is set to 0
move(maxSpeed); //Move
//After turning and moving
turnTowards(closest.getX(),closest.getY()); //Turn
rotation = getRotation(); //Get Rotation and do the turn
setRotation(0); //Set the Rotation back to 0
}
}