This site requires JavaScript, please enable it in your browser!
Greenfoot back
ManiacalPenguin
ManiacalPenguin wrote ...

2012/12/23

pathfinder

ManiacalPenguin ManiacalPenguin

2012/12/23

#
I am making a zombie game, and I am done with most of it except for a pathfinder. As of now, the zombies turnTowards the character and walk towards it. They can't pass through walls or doors or other zombies. I want to make it so the zombies have a pathfinder to find a way to the player. Now, they just keep pushing on walls and doors and other zombies when they hit them.
ManiacalPenguin ManiacalPenguin

2012/12/23

#
here is my code for the class Zombie import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Zombie here. * * @author (your name) * @version (a version number or a date) */ public class Zombie extends Actor { private int x; private int y; /** * Act - do whatever the Zombie wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { follow(); stopAtWalls(); die(); } public void follow() { Actor character = (Character) getWorld().getObjects(Character.class).get(0); turnTowards(character.getX(), character.getY()); move(2); } public void die() { Actor bullet = getOneIntersectingObject(Bullet.class); if (getOneIntersectingObject(Bullet.class) != null) { getWorld().removeObject(bullet); ZombieWorld.zombiesKilled++; Character.score = Character.score + 50; getWorld().removeObject(this); } } public void stopAtWalls() { if (getOneIntersectingObject(Wall1.class) != null || getOneIntersectingObject(Wall2.class) != null || getOneIntersectingObject(Door1.class) != null || getOneIntersectingObject(Door2.class) != null || getOneIntersectingObject(Door3.class) != null || getOneIntersectingObject(Door4.class) != null || getOneIntersectingObject(Zombie.class) != null) { setLocation(x,y); } else { x = getX(); y = getY(); } } }
ManiacalPenguin ManiacalPenguin

2012/12/23

#
I do not want the zombies to navigate around the doors, only the walls and other zombies
MatheMagician MatheMagician

2012/12/23

#
Here is what your new class should look like:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{
    private int x;
    private int y;
    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        follow();
        checkForWall();
        stopAtWalls();
        die();
    }    
    public void checkForWalls()
    {
        if (getOneIntersectingObject(Wall1.class) != null || getOneIntersectingObject(Wall2.class) != null)
        {
            move(-2);
            rotate(90);
            move(2);
        }
    }
    public void follow()
    {
        Actor character = (Character) getWorld().getObjects(Character.class).get(0);
        turnTowards(character.getX(), character.getY());
        move(2);
    }
    public void die()
    {
        Actor bullet = getOneIntersectingObject(Bullet.class);
        if (getOneIntersectingObject(Bullet.class) != null)
        {
            getWorld().removeObject(bullet);
            ZombieWorld.zombiesKilled++;
            Character.score = Character.score + 50;
            getWorld().removeObject(this);
        }
    }
    public void stopAtWalls()
    {
        if (getOneIntersectingObject(Wall1.class) != null || getOneIntersectingObject(Wall2.class) != null
        || getOneIntersectingObject(Door1.class) != null || getOneIntersectingObject(Door2.class) != null
        || getOneIntersectingObject(Door3.class) != null || getOneIntersectingObject(Door4.class) != null
        || getOneIntersectingObject(Zombie.class) != null)
        {
            setLocation(x,y);
        }
        else
        {
            x = getX();
            y = getY();
        }
    }
}
All I did was add this method to the act method after the zombie has moved toward the person.
public void checkForWalls()
    {
        if (getOneIntersectingObject(Wall1.class) != null || getOneIntersectingObject(Wall2.class) != null)
        {
            move(-2);
            rotate(90);
            move(2);
        }
    }
Here is the basic pseudo-code: if we are touching a wall, back up. turn to the right 90 degrees and move two. While not technically not path-finding, this code should solve your problem.
You need to login to post a reply.