我认为您的问题归结为想要将角斗士阵列传递给另一个班级。这应该很容易。如果你的主班有这两个定义(注意你只需要一个,我推荐这个列表,因为它更通用,数组有固定长度)。
你想要这样的东西:
public class Main {
// ....stuff
// This is the main class that keeps the list of gladiators
private List<Gladiator> gladiatorsList;
private Gladiator[] gladiatorsArray;
private MovementAI movementAI;
public Main() {
// You initialize gladiatorsList and gladiatorsArray as before
// gladiatorsList = ...
// gladiatorsArrray = ...
// Now you want to pass this list/array to another class (the AI), you
// can do this in the constructor of that class like so:
movementAI = new MovementAI(gladiatorsList);
}
// ...stuff as before
}
人工智能
public class MovementAI {
private List<Gladiator> gladiators;
// Giving the class the list-reference, this list will be the same as the
// list in main, when main-list changes so does this one, they point to the
// same list-object, so the reference is only needed once.
public MovementAI(List<Gladiator> gladiatorsList) {
this.gladiators = gladiatorsList;
}
// The class already has a reference to the list from its constructor so it
// doesn't need the list again as a parameter
public void moveAI() {
}
// If you don't want to keep a reference to the list in this class but only
// use it in a method (I would not recommend this)
public MovementAI() {
}
// You need to pass it gladiatorsList everytime you call this method.
public void moveAI(List<Gladiator> gladiators) {
}
}
我在您的上一条评论中看到,您已决定让 AI 决定在符合标准时重新绘制,这是不推荐的,您应该在您的类中保持职责分开,减少出错和更好的开发。建议让 AI 更改角斗士列表(移动、杀死它们等),渲染器类只需绘制每个角斗士。
似乎您还希望每个角斗士都能够将另一个角斗士作为目标,他们最好将目标作为对象,这样您就不必搜索整个列表来找出哪个角斗士角斗士编号是指并且您不必考虑在列表中排序。像这样的东西:
public class Gladiator {
// ..other stuff
private Gladiator target;
public Gladiator getTarget() {
return target;
}
public void setTarget(Gladiator target) {
this.target = target;
}
}