0

我正在使用 LigGdx 制作游戏,它看起来像一个 RPG 游戏。当敌人处于警戒状态时,它必须跟随玩家,但它只能向前、向后、向左和向右移动,并且在碰撞时还必须转移物体,寻找到达玩家的最佳方式,我是游戏开发的新手,我的算法可能完全错误,所以,我真的需要帮助......

private void enemyInAlert(Enemy enemy, float delta) {

    detectDirection(enemy);

    enemyWalking(enemy, delta);

    if (Math.round(getDistanceXofLastPosition(enemy)) == 0 && Math.round(getDistanceYofLastPosition(enemy)) == 0) {
        enemy.setState(States.IDLE);
        lastPosition = null;
    }
}

private void detectDirection(Enemy enemy) {
    float diff = getDistanceXofLastPosition(enemy) - getDistanceYofLastPosition(enemy);
    if (diff < 0) {
        getDirectionX(enemy);
    } else if (diff > 0) {
        getDirectionY(enemy);
    }
}

private void getDirectionY(Enemy enemy) {
        int enemyY = Math.round(enemy.getY());
        int lastPositionY = Math.round(lastPosition.getY());

        if (enemyY < lastPositionY && enemy.isDirectionBlocked(Direction.FORWARD) == false) { //Enemy needs to go forward
            enemy.setDirection(Direction.FORWARD);
            enemy.blockDirection(Direction.BACKWARD);
        } else if (enemyY > lastPositionY && enemy.isDirectionBlocked(Direction.FORWARD) == false) { //Enemy needs to go backward
            enemy.setDirection(Direction.BACKWARD);
            enemy.blockDirection(Direction.FORWARD);
        } else { //Enemy needs to change direction
            if (enemy.isDirectionBlocked(Direction.LEFT) == false || enemy.isDirectionBlocked(Direction.LEFT) == false) {
                enemy.blockDirection(Direction.BACKWARD);
                enemy.blockDirection(Direction.FORWARD);
                getDirectionX(enemy);
            } else {
                sortRandomDirection(enemy);
            }
        }
    }

    private void getDirectionX(Enemy enemy) {
        int enemyX = Math.round(enemy.getX());
        int lastPositionX = Math.round(lastPosition.getX());

        if (enemyX < lastPositionX && enemy.isDirectionBlocked(Direction.RIGHT) == false) { //Enemy needs to go right 
            enemy.setDirection(Direction.RIGHT);
            enemy.blockDirection(Direction.LEFT);
        } else if (enemyX > lastPositionX && enemy.isDirectionBlocked(Direction.LEFT) == false) {
            enemy.setDirection(Direction.LEFT);
            enemy.blockDirection(Direction.RIGHT);
        } else { //Enemy needs to change direction
            if (enemy.isDirectionBlocked(Direction.FORWARD) == false && enemy.isDirectionBlocked(Direction.BACKWARD) == false) {
                enemy.blockDirection(Direction.LEFT);
                enemy.blockDirection(Direction.RIGHT);
                getDirectionY(enemy);
            } else {
                sortRandomDirection(enemy);
            }
        }
    }

我正在接受建议,我可以更改所有代码,毫不留情......抱歉英语不好:D

谢谢!!

编辑:现在,我正在尝试使用 A* 或类似的东西。:D ...我的代码:

    private void calculateRoute(Enemy enemy) {
        int lowerPath = getDistanceXofLastPosition(enemy.getBounds()) + getDistanceYofLastPosition(enemy.getBounds());

        path = new ArrayList<Rectangle>();
        Rectangle finalRect = new Rectangle(enemy.getBounds());

        List<Rectangle> openList = new ArrayList<Rectangle>();
        while (getDistanceXofLastPosition(finalRect) > 0 || getDistanceYofLastPosition(finalRect) > 0) {

            for (int i = -1; i < 2; i+= 1) {
                outerloop:
                for (int j = -1; j < 2; j+= 1) {

                    Rectangle temp = new Rectangle(finalRect);
                    temp.offSet(i, j);

                    if (openList.contains(temp)) {
                        continue;
                    }
                    if ((i == -1 && j == -1) || (i == 1 && j == -1) || (i == 0 && j == 0) || (i == 1 && j == -1) || (i == 1 && j == 1)) {
                        continue;
                    }
                    for (Collider collider : colliders) {
                        if (collider.isSolid() && Utils.detectCollision(temp, collider.getBounds())) {
                            continue outerloop;
                        }
                    }
                    openList.add(temp);
                }
            }

            int lowerDistance = Integer.MAX_VALUE;

            for (Rectangle rect : openList) {
                int distance = getDistanceXofLastPosition(rect) + getDistanceYofLastPosition(rect);
                distance = distance + lowerPath;

                if (distance < lowerDistance) {
                    lowerDistance = distance;
                    finalRect = rect;
                }
            }
            path.add(new Rectangle(finalRect));
        }
}

但是速度很慢,我能做些什么来提高性能?

4

1 回答 1

2

您可能想查看A*

您可以轻松地将地图转换为图形,将每个图块用作顶点,并将所述顶点与边缘连接到靠近它的所有其他顶点。与边缘相关的成本可能是可变的,例如,在河流上移动 i 块可能比在平面上移动一块块花费更多。

然后您可以使用路径搜索算法找到从一个点到另一个点的最佳路径。使用这个算法会有两个缺点:

  1. 它的计算成本很高
  2. 它总能找到最佳解决方案,让您的机器人比普通玩家更聪明:)

如果计算和存储成本确实是个问题,您可以求助于 A* 的表亲之一,例如

  • IDA*用于更便宜的内存需求,迭代解决方案的深度
  • SMA*限制了算法可以使用的内存量
于 2013-08-18T15:28:43.680 回答