游戏AI算法,敌人跟随玩家

时间:2013-08-18 15:18:10

标签: algorithm artificial-intelligence libgdx

我正在使用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));
        }
}

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

1 个答案:

答案 0 :(得分:2)

您可能需要查看A*

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

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

  1. 计算成本高
  2. 它始终找到最佳解决方案,使您的机器人比普通玩家更聪明:)
  3. 如果计算和存储成本确实存在问题,您可以选择其中一个A *的表兄弟,例如

    • IDA*用于降低内存需求,迭代解决方案的深度
    • SMA*限制了算法可以使用的内存量