Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 游戏AI算法,敌后玩家_Algorithm_Artificial Intelligence_Libgdx - Fatal编程技术网

Algorithm 游戏AI算法,敌后玩家

Algorithm 游戏AI算法,敌后玩家,algorithm,artificial-intelligence,libgdx,Algorithm,Artificial Intelligence,Libgdx,我用LigGdx做了一个游戏,看起来像RPG游戏。当敌人处于警戒状态时,它必须跟随玩家,但它只能向前、向后、向左和向右移动,并且在碰撞时必须转移物体,寻找到达玩家的最佳方式,我是游戏开发新手,我的算法可能完全错了,所以,我真的需要帮助 private void enemyInAlert(Enemy enemy, float delta) { detectDirection(enemy); enemyWalking(enemy, delta); if (Math.ro

我用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);
            }
        }
    }
private void enemyInAlert(敌方,浮动三角洲){
探测方向(敌人);
enemyWalking(敌人,三角洲);
如果(数学回合(getDistanceXofLastPosition(敌人))==0和&Math.round(getDistanceYofLastPosition(敌人))==0){
敌方。设置状态(状态。空闲);
lastPosition=null;
}
}
私有无效检测定向(敌方){
浮动差异=getDistanceXofLastPosition(敌人)-getDistanceYofLastPosition(敌人);
如果(差异<0){
getDirectionX(敌人);
}否则如果(差异>0){
获取方向(敌人);
}
}
二等兵方向(敌方){
int enemyY=Math.round(敌方.getY());
int lastPositionY=Math.round(lastPosition.getY());
如果(enemyYlastPositionY&&敌方.isDirectionBlocked(Direction.FORWARD)==false){//敌方需要后退
敌方设置方向(方向向后);
敌方封锁方向(前进方向);
}否则{//敌人需要改变方向
如果(敌方isDirectionBlocked(Direction.LEFT)==false | |敌方isDirectionBlocked(Direction.LEFT)==false){
敌人。封锁方向(方向。后退);
敌方封锁方向(前进方向);
getDirectionX(敌人);
}否则{
sortRandomDirection(敌人);
}
}
}
私有无效getDirectionX(敌人){
int-enemyX=Math.round(敌方.getX());
int lastPositionX=Math.round(lastPosition.getX());
如果(enemyXlastPositionX&&Foreign.isDirectionBlocked(Direction.LEFT)==false){
敌方方向(方向左);
敌人。封锁方向(方向。右);
}否则{//敌人需要改变方向
if(敌方isDirectionBlocked(Direction.FORWARD)=false和敌方isDirectionBlocked(Direction.BACKWARD)=false){
敌人。封锁方向(方向。左);
敌人。封锁方向(方向。右);
获取方向(敌人);
}否则{
sortRandomDirection(敌人);
}
}
}
我接受建议,我可以更改所有代码,不发慈悲。。。对不起,英语不好:D

谢谢

编辑:现在,我正在尝试使用*,或者类似的东西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));
        }
}
private void calculaterout(敌方){
int lowerPath=getDistanceXofLastPosition(敌方.getBounds())+getDistanceYofLastPosition(敌方.getBounds());
路径=新的ArrayList();
矩形finalRect=新矩形(敌方.getBounds());
List openList=new ArrayList();
而(GetDistancexOflasPosition(finalRect)>0 | | GetDistanceyOflasPosition(finalRect)>0){
对于(int i=-1;i<2;i+=1){
外部环路:
对于(int j=-1;j<2;j+=1){
矩形温度=新矩形(最终选择);
温度偏移量(i,j);
if(openList.contains(temp)){
继续;
}
如果((i=-1&&j=-1)| |(i==1&&j=-1)| |(i==0&&j==0)| |(i==1&&j=-1)| |(i==1&&j==1)){
继续;
}
用于(碰撞器碰撞器:碰撞器){
if(collider.isSolid()&&Utils.detectCollision(temp,collider.getBounds())){
继续外环;
}
}
openList.add(temp);
}
}
int lowerDistance=整数最大值;
用于(矩形矩形:openList){
int distance=getDistanceXofLastPosition(rect)+getDistanceYofLastPosition(rect);
距离=距离+较低路径;
如果(距离<较低距离){
低距离=距离;
finalRect=rect;
}
}
add(新矩形(finalRect));
}
}
但是速度非常慢,我能做些什么来提高性能呢?

您可能想了解一下

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

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

  • 它有很高的计算成本
  • 它总能找到最佳解决方案,使您的机器人比普通玩家更聪明:)
  • 如果计算和存储成本确实是一个问题,那么您可以求助于a*的近亲,例如

    • 对于更便宜的内存需求,请迭代解决方案的深度
    • 限制算法可以使用的内存量

    但我的地图不是用瓷砖制作的,这有问题吗?谢谢你的帮助:不应该是:如果你