简体   繁体   English

Java - 如何根据与玩家的距离动态排序对象的 Arraylist

[英]Java - How to dynamically sort an Arraylist of objects based on distance from player

I'm building a small game in java where i have an Arraylist of Zombie objects that is used to select the next target for the player.我正在 java 中构建一个小游戏,其中我有一个 Arraylist 僵尸对象,用于 select 玩家的下一个目标。 The zombies are comprised of an x and y variable which dictates their initial position.僵尸由 x 和 y 变量组成,它们指示它们的初始 position。

public Zombie(double positionX, double positionY){
        this.image = new Image("res/images/zombie.png");
        this.Position = new Point(positionX, positionY);
        this.visible = true;
    }

I have written this method which selects the next zombie in the array in which the player automatically targets, but I'm stuck trying to figure out how to dynamically sort the Arraylist so the player targets the closest zombie by distance, rather than the one next in the list.我已经编写了这个方法,它选择玩家自动瞄准的数组中的下一个僵尸,但我一直在试图弄清楚如何动态排序 Arraylist 以便玩家按距离瞄准最近的僵尸,而不是下一个在列表中。

public void zombieSpawn(){
        for(int i = 0; i < zombies.size(); i++){
            zombie = zombies.get(i);
            if(!zombie.isVisible()){
                removeZombie(zombie);
            }
            if(zombies.size() != 0){
                this.zombie = zombies.get(0);
            }
        }
    }

tl:dr I want to dynamically sort an Arraylist of zombies by shortest distance to the player. tl:dr 我想通过与玩家的最短距离动态地对 Arraylist 的僵尸进行排序。

Here I would use a custom ZombieSort class, which implements Comparator, that sorts the Zombies by comparing their distance from the player's coordinates.在这里,我将使用自定义的 ZombieSort class,它实现了 Comparator,通过比较僵尸与玩家坐标的距离来对僵尸进行排序。

Here is ZombieSort.java这是 ZombieSort.java

public class ZombieSort implements Comparator<Zombie> {
    private final double playerX, playerY;

    public ZombieSort(double playerX, double playerY) {
        this.playerX = playerX;
        this.playerY = playerY;
    }

    @Override
    public int compare(Zombie z1, Zombie z2) {
        return Double.compare(getDistance(z1), getDistance(z2));
    }

    public double getDistance(Zombie zombie) {
        return Math.sqrt(
                Math.pow(zombie.x - playerX, 2) + Math.pow(zombie.y - playerY, 2)
        );
    }
}

And here would be your implementation for zombieSpawn(), which basically sorts the list of Zombies every time it is called.:这将是您对zombiesSpawn() 的实现,它基本上每次调用时都会对Zombies 列表进行排序。:

public void zombieSpawn(){
    sortZombies(zombies, player);

    for(int i = 0; i < zombies.size(); i++){
        zombie = zombies.get(i);
        if(!zombie.isVisible()){
            removeZombie(zombie);
        }
        if(zombies.size() != 0){
            this.zombie = zombies.get(0);
        }
    }
}

public void sortZombies(List<Zombie> zombies, Player player) {
    Collections.sort(zombies, new ZombieSort(player.getX(), player.getY()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM