Java A *路径查找算法不起作用?

时间:2015-09-17 09:00:37

标签: java algorithm path entity

我正在开发一个Java游戏,我在使用和实现我的A *路径查找算法时遇到了问题。目前,我已经设置了实体,以便实体不能存在于某个级别之外(每个实体都可以访问当前级别)。因此,我将算法方法'findPath'存储在基础Level类中。下面是算法代码,以及实体的移动代码,如您所见,它在级别中调用查找路径方法。我的问题是,当我运行代码时,我将播放器移向实体,实体什么都不做。我已经放入了一些调试打印行(我将保留这些内容以便您可以看到),并且不断返回的列表path仅返回[]。我已经完成了所有事情,并且可以解决这个问题。希望有人可以帮忙!

由于

代码: 在Level.java类中:

private Comparator<Node> nodeSorter = new Comparator<Node>() {
    public int compare(Node n0, Node n1) {
        if (n1.fCost < n0.fCost) return +1;
        if (n1.fCost > n0.fCost) return -1;
        return 0;
    }
};

public List<Node> findPath(Vector2i start, Vector2i goal) {
    List<Node> openList = new ArrayList<Node>();
    List<Node> closedList = new ArrayList<Node>();
    Node current = new Node(start, null, 0, getDistance(start, goal));
    openList.add(current);
    while (openList.size() > 0) {
        Collections.sort(openList, nodeSorter);
        current = openList.get(0);
        if (current.tile.equals(goal)) {
            List<Node> path = new ArrayList<Node>();
            while (current.parent != null) {
                path.add(current);
                current = current.parent;
            }
            openList.clear();
            closedList.clear();
            return path;
        }
        openList.remove(current);
        closedList.add(current);
        for (int i = 0; i < 9; i ++){
            if (i == 4) continue;
            int x = current.tile.getX();
            int y = current.tile.getY();
            int xi = (i % 3) - 1;
            int yi = (i / 3) - 1;
            Tile at = getTile(x + xi, y + yi);
            if (at == null) continue;
            if (at.solid()) continue;
            Vector2i a = new Vector2i(x + xi, y + yi);
            double gCost = current.gCost + getDistance(current.tile, a);
            double hCost = getDistance(a, goal);
            Node node = new Node(a, current, gCost, hCost);
            if (vecInList(closedList, a) && gCost >= node.gCost) continue;
            if (!vecInList(openList, a) || gCost < node.gCost) openList.add(node);

        }
    }
    closedList.clear();
    return null;
}

在我的“Wizard.java”中,它扩展了Mob,反过来实体:

private List<Node> path = null;

public Wizard(int x, int y) {
    this.x = x << 4;
    this.y = y << 4;
    sprite = animSprite.getSprite();
}

private void move() {
    xa = 0;
    ya = 0;
    int px = (int)level.getPlayerAt(0).getX();
    int py = (int)level.getPlayerAt(0).getY();
    Vector2i start = new Vector2i((int)getX() >> 4, (int)getY() >> 4);
    Vector2i destination = new Vector2i(px >> 4, py >> 4);
    if (time % 60 == 0) path = level.findPath(start, destination);
    if (path != null){
        if (path.size() > 0){
            Vector2i vec = path.get(path.size() -1).tile;
            if (x < vec.getX() << 4) xa += speed;
            if (x > vec.getX() << 4) xa -= speed;
            if (y < vec.getY() << 4) ya += speed;
            if (y > vec.getY() << 4) ya -= speed;

        }
    }
    if (xa != 0 || ya != 0) {
        move(xa, ya);
        walking = true;
    } else
        walking = false;
}

public void update() {
    time++;
    if (!collision(xa, ya)){
        move();
    }
    move();
    if (walking) animSprite.update();
    else
        animSprite.setFrameRate(0);
    if (ya < 0) {
        animSprite = up;
        dir = Direction.UP;
    } else if (ya > 0) {
        animSprite = down;
        dir = Direction.DOWN;
    }
    if (xa < 0) {
        animSprite = left;
        dir = Direction.LEFT;
    } else if (xa > 0) {
        animSprite = right;
        dir = Direction.RIGHT;
    }
}

public void render(Screen screen) {
    sprite = animSprite.getSprite();
    if (level.getTile((int)x, (int)y).solid()){
        x += 16;
        y += 16;
    }
    screen.renderMob((int)(x - 16), (int)(y - 16), this);
}

节点等于代码:

public boolean equals(Object object){
    if(!(object instanceof Vector2i)) return false;
    Vector2i vec = (Vector2i) object;
    if (vec.getX() == this.getX() && vec.getY() == this.getY()) return true;
    else return false;
}

0 个答案:

没有答案