广度优先搜索障碍

时间:2018-06-09 07:29:58

标签: javafx breadth-first-search path-finding

我目前正在制作一个游戏作为学校项目,而我正试图弄清楚敌人的寻路。我做了一个非常好的基本BFS,但没有考虑障碍,所以当试图接触到玩家时,敌人会遇到障碍物。我尝试了不同的东西,但我得到的只是空指针(我有点理解,但我不知道如何使其工作)。

public class BFS {

    private Player player;
    private Field field;
    private Queue<Tile> queue;
    private HashMap<Tile, Tile> parents;
    private ArrayList<Tile> adjTiles;

    public BFS(Player player, Field field) {
        this.player = player;
        this.field = field;
        this.queue = new LinkedList<>();
        this.parents = new HashMap<Tile, Tile>();
        this.adjTiles = new ArrayList<>();
    }

    public void lancerBFS() {
        int x = player.getIndiceX();
        int y = player.getIndiceY();
        Tile player = field.getNextTile(y, x);

        this.parents.clear();
        this.queue.clear();
        this.adjTiles.clear();

        this.queue.add(field.getNextTile(y, x));
        this.parents.put(field.getNextTile(y, x), field.getNextTile(y, x));

        while (!queue.isEmpty()) {
            Tile temp = queue.remove();
            y = temp.getI();
            x = temp.getJ();

            if (x > 0) {
                this.adjTiles.add(field.getNextTile(y, x-1));
            }
            if (y > 0) {
                this.adjTiles.add(field.getNextTile(y-1, x));
            }
            if (x < 24) {
                this.adjTiles.add(field.getNextTile(y, x+1));
            }
            if (y < 24) {
                this.adjTiles.add(field.getNextTile(y+1, x));
            }

            for (int i = 0 ; i < adjTiles.size() ; i++) {

                if (!this.parents.containsKey(adjTiles.get(i))) {
                    this.parents.put(this.adjTiles.get(i), temp);
                    this.queue.add(this.adjTiles.get(i));
                }               
            }

            this.adjTiles.clear();
        }       
    }

    public Tile searchWay(AnimatedEntity entity) {
        int x = entity.getIndiceX();
        int y = entity.getIndiceY();

        Tile t = this.field.getNextTile(y, x);

        return this.parents.get(t);
    }

    public HashMap<Tile, Tile> getParents() {
        return parents;
    }   
}

我如何使用它(我的瓷砖在25x25地图上是32x32,敌人移动4像素乘4像素)

public void moveEnemy(AnimatedEntity e) {
    Tile nextTile = this.bfs.searchWay(e);
    Tile enemyAt = this.map.getNextTile(e.getIndiceY(), e.getIndiceX());

    if (nextTile.getI() == enemyAt.getI() && nextTile.getJ() < enemyAt.getJ()) {
        e.moveLeft(entities, inanimatedEntities);
    }
    if (nextTile.getI() < enemyAt.getI() && nextTile.getJ() == enemyAt.getJ()) {
        e.moveUp(entities, inanimatedEntities);
    }
    if (nextTile.getI() == enemyAt.getI() && nextTile.getJ() > enemyAt.getJ()) {
        e.moveRight(entities, inanimatedEntities);
    }
    if (nextTile.getI() > enemyAt.getI() && nextTile.getJ() == enemyAt.getJ()) {
        e.moveDown(entities, inanimatedEntities);
    }   
}

敌人如何陷入游戏中:

enter image description here

在尝试包含isObstacle概念后,敌人如何陷入困境

enter image description here

0 个答案:

没有答案