寻路(Android Studio + libgdx)

时间:2018-11-15 19:20:45

标签: android libgdx

所以我的问题是,我使用Android Studio和libgdx AI软件包为两个玩家创建了基于回合的游戏,这时我可以设法移动玩家,唯一让我烦恼的问题是玩游戏的敌人有一个target是附近的玩家,我可以设法将其移动到目标位置,但是当它遇到障碍物却无法绕开它时,我已经在互联网上搜索了寻路算法,但是我不能将它们应用到我的游戏中

有敌人的代码

public class Enemy extends Entities {


private static final Logger log = new Logger(Enemy.class.getSimpleName(), Logger.DEBUG);

public Player target;
public float movement = GameConfig.ENEMY_MOVEMENTS;


//Obligatoire pour le JSON
public Enemy() {
}

public Enemy(float x, float y, float width, float height,Color color) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.color = color;
    target= new Player();
}

/**
 * Cette méthode vérifie si l'ennemie est en collision avec soit un obstacle(Murs, caisses) ou un joueur
 */
public boolean isCollidingObstacle(float x, float y, Array<ObstacleBase> obstacles, Array<Enemy> enemies, Player player1, Player player2, int index) {

    for (ObstacleBase obstacle : obstacles) {
        if (x >= obstacle.getBounds().getX() && y >= obstacle.getBounds().getY()) {
            if (x <= obstacle.getBounds().getWidth() + obstacle.getBounds().getX() - 1 && y <= obstacle.getBounds().getHeight() + obstacle.getBounds().getY() - 1) {
                //log.debug("Un obstacle se trouve en " + obstacle.getBounds().getX() + " " + obstacle.getBounds().getY());
                return true;
            }
        }
    }

    int i = 0;
    for (Enemy enemy : enemies) {

        if (i == index) {

        } else {
            if (x == enemy.getX() && y == enemy.getY()) {
                return true;
            }
        }
        i++;
    }
    if (x == player1.getX() && y == player1.getY()) {
        return true;
    }
    if (x == player2.getX() && y == player2.getY()) {
        return true;
    }
    return false;
}

/**
 * Cette méthode permet de vérifier si l'ennemie se trouve à côté de la cible
 * @return
 */
public boolean nextToTarget(){
    if (x == target.getX()+1 && y == target.getY()||x == target.getX()+1 && y == target.getY()||
            x == target.getX() && y == target.getY()-1||x == target.getX() && y == target.getY()+1) {
        return true;
    }
    return false;
}

/**
 * Cette méthode permet de savoir quel joueur est le plus proche et de le mettre en target pour l'ennemie
 *
 * @param player1
 * @param player2
 */
public void nearPlayer(Player player1, Player player2) {
    float diffXP1 = (this.getX() - player1.getX());
    float diffXP2 = (this.getX() - player2.getX());
    float diffYP1 = (this.getY() - player1.getY());
    float diffYP2 = (this.getY() - player2.getY());

    if (diffXP1 < 0) {
        diffXP1 *= -1;
    }
    if (diffXP2 < 0) {
        diffXP2 *= -1;
    }
    if (diffYP1 < 0) {
        diffYP1 *= -1;
    }
    if (diffYP2 < 0) {
        diffYP2 *= -1;
    }
    float diffP1=diffYP1-diffXP1;
    float diffP2=diffYP2-diffXP2;

    if(diffP1<0){
        diffP1 *= -1;
    }
    if(diffP2<0){
        diffP2 *= -1;
    }

    if(diffP1<diffP2){
        target.setX(player1.getX());
        target.setY(player1.getY());
    }else{
        target.setX(player2.getX());
        target.setY(player2.getY());
    }
}


public void update(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index) {
    move(obstacles, enemies, player1, player2, index);
    nearPlayer(player1,player2);
}


public void move(final Array<ObstacleBase> obstacles, final Array<Enemy> enemies, Player player1, Player player2, int index) {
    if (nextToTarget()) {
        endTurn();
    }else if(!(movements_done==0)&& movement==movements_done){
        endTurn();
    } else if (movement > movements_done || movements_done == 0) {
        if (target.getX() > this.getX()) {
            if (!(isCollidingObstacle(getX() + 1, getY(), obstacles, enemies, player1, player2, index))) {
                movements_done += 1;
                setX(getX() + 1);
            }
        } else if (target.getX() < this.getX()) {
            if (!(isCollidingObstacle(getX() - 1, getY(), obstacles, enemies, player1, player2, index))) {
                movements_done += 1;
                setX(getX() - 1);
            }
        }

        if (target.getY() > this.getY()) {
            if (!(isCollidingObstacle(getX(), getY() + 1, obstacles, enemies, player1, player2, index))) {
                movements_done += 1;
                setY(getY() + 1);
            }
        } else if (target.getY() < this.getY()) {
            if (!(isCollidingObstacle(getX(), getY() - 1, obstacles, enemies, player1, player2, index))) {
                movements_done += 1;
                setY(getY() - 1);
            }
        }

    }
}
}

我想澄清一下,我也在libgdx AI文档中寻找了寻路方法,但是即使那样,我仍然不知道如何将其应用于游戏。

有关更多精度或代码,我随时为您服务。

0 个答案:

没有答案