Поиск в ширину с препятствиями - PullRequest
0 голосов
/ 09 июня 2018

В настоящее время я делаю игру в качестве школьного проекта и пытаюсь выяснить пути поиска врагов.Я сделал базовую 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;
    }   
}

Как я использую это (мои плитки32x32 на карте 25x25, и враги перемещаются на 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

Как враги застревают после попытки включить понятие препятствия

enter image description here

...