I have one Array List that holds all the ships, and I want to somehow use that to have the enemy and friendly ships target each other. Now, I have no idea if it's even possible to do this with a single array. I'm just using a for loop in an infinite loop to update and render ships, and figure out how to implement my findEnemy() method to actually find an enemy and test out if it works. Each ship has an int called Faction which tells if its friendly or not, but I have no idea how to check if two ships in the Array List are friendly or not.
// Update and Render Ships
for (Iterator<Ship> shipIter = shipList.iterator(); shipIter.hasNext();) {
Ship ship = shipIter.next();
ship.render(batch);
ship.update(mousePos, select2);
if (ship.isSelected()) {
currentShip = ship;
updateFollowCamera(camera);
}
if (ship.isSelected() == false) {
currentShip = null;
}
if (camera.zoom > 15) {
ship.renderGlobal(GlobalShipGreen, GlobalShipRed,
GlobalShipWhite, batch);
}
}
in my Ship Class (It’s supposed to lock on as soon as I start to any enemy ship, which is supposed to be supplied by the Array List in my other class):
public void findEnemy(Ship ship){
if(this.TargetShip == null){
this.TargetShip = ship;
}
}
public void targetEnemy(){
if(TargetShip != null){
GunDirection = (int) Math.atan2(TargetShip.getPosition().y - Position.y, TargetShip.getPosition().x - Position.x);
}
}