[Solved] Using a single array for enemy and friendly entities?

       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);
			}
		}

You can use a simple if statement that checks if that factions(int) are the same, if so then they are friendly. If you are comparing ships in an array you will need an iterator I believe and use the compare method? I’m not too sure on that part. Sorry I can’t supply too much help, I’m on a train atm ;D

Edit, though you can use a for loop that checks the current ship with the next ship and if they are not friendly, they target each other. Ignore my iterator comment xD

Well I just passed the ship iteration to the ship for it to check it and its returning nothing, the Target Ship is always null.

// Update and Render Ships
		for (Iterator<Ship> shipIter = shipList.iterator(); shipIter.hasNext();) {
			Ship ship = shipIter.next();
		

			ship.render(batch);
			ship.update(mousePos, select2);
			ship.findEnemy(ship);
			

			if (ship.isSelected()) {
				currentShip = ship;
				updateFollowCamera(camera);
			}
			if (ship.isSelected() == false) {
				currentShip = null;
			}
			if (camera.zoom > 15) {
				ship.renderGlobal(GlobalShipGreen, GlobalShipRed,
						GlobalShipWhite, batch);
			}
		}

Ship Code:


	@Override
	public void findEnemy(Ship ship) {

		if (this.TargetShip == null && ship.getFaction() != this.Faction) {
			this.TargetShip = ship;
		}

	}

	@Override
	public void targetEnemy() {
		if (TargetShip != null) {
			GunDirection = (int) ((int) Math.atan2(TargetShip.getPosition().y
					- Position.y, TargetShip.getPosition().x - Position.x) - Math.PI / 2);
		}
	}

And yes, I have 3 ships set to different factions.

Maybe in your Ship class:

private int id;

public Ship(int id)
{
	this.id = id;
}

public int getFaction()
{
	return id;
}

Your class:

public static final int ID_FACTION_1 = 1;
public static final int ID_FACTION_2 = 2;

shipList.add(new Ship(ID_FACTION_1));

So, you only need to use a condition with .getFaction() to know if is a friendly (same faction int value) or enemy.

Maybe you could store a boolean in the Ship class to determine whether it is an enemy or not?


public class Ship {
	
	private boolean isEnemy;

	public Ship(boolean isEnemy) {
		this.isEnemy = isEnemy;
	}

	public boolean isEnemy() {
		return isEnemy;
	}
}

(Pardon my bad formatting, that was written in my text editor.)

Or do what craftm outlined!

They are already set to different Factions. Above the code I showed you a created the instances and then set their factions


shipList.get(0).setFaction(0);
shipList.get(1).setFaction(1);
shipList.get(2).setFaction(2);

and the Ship Code for finding the enemy is supposed to check for similarities in factions

@Override
	public void findEnemy(Ship ship) {

		if (this.TargetShip == null && ship.getFaction() != this.Faction) {
			this.TargetShip = ship;
		}

	}

Alright, so what exactly is the issue? It seems like your code should work fine.

Edit:
Sorry, I just read the part where you say what your issue is. I’ll edit this post if I figure out what’s wrong!

I can’t tell without seeing the rest of your code, but are your variables “TargetShip” and “Faction” static?

[quote]but I have no idea how to check if two ships in the Array List are friendly or not.
[/quote]
Two ships:

public boolean checkEnemy(Ship ship1, Ship ship2)
{
	if (ship1.getFaction() == ship2.getFaction())
		return false; //Friendly
	else
		return true; //Enemy
}

All list:

for (int i=0; i < shipList.size(); i++)
	for (int j=0; j < shipList.size(); j++)
		if (i != j)
			if (checkEnemy(shipList.getIndex(i), shipList.getIndex(j))) //True = Enemy, False = Friendly.
			{
				//Any code, maybe your targetEnemy with shipList.getIndex(j) as param.
			}

How many ships at same time? If you have 10000 Ships can be really bad use this code.

ok, so what exactly do you want?

  • find all enemy ships for all ships?
  • find any enemy ship for all ships?
  • find all enemy ships for the currently selected ship?
  • find any enemy ship for the currently selected ship?

Edit:
craftms solution is finding all enemies for all ships and offers a codeblock for them to engage in a fight -> this might exaclty be, what you want…

Perfect, just what I needed! Thank you!