Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] Using a single array for enemy and friendly entities?  (Read 2703 times)
0 Members and 1 Guest are viewing this topic.
Offline MrPork
« Posted 2015-03-24 18:48:45 »

           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.

   
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
// 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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   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);
         }
      }



"f**k it, maybe it'll work." -Me
Offline SauronWatchesYou

JGO Ninja


Medals: 33
Projects: 4
Exp: 2 years


Hi there! :)


« Reply #1 - Posted 2015-03-24 19:00:29 »

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 Grin

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

Hey, you! Back to work
Offline MrPork
« Reply #2 - Posted 2015-03-24 19:23:15 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
// 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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   @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);
      }
   }

"f**k it, maybe it'll work." -Me
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MrPork
« Reply #3 - Posted 2015-03-24 19:24:07 »

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

"f**k it, maybe it'll work." -Me
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #4 - Posted 2015-03-24 19:27:35 »

Maybe in your Ship class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
private int id;

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

public int getFaction()
{
   return id;
}


Your class:
1  
2  
3  
4  
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.

_ Wink
Offline Opiop
« Reply #5 - Posted 2015-03-24 19:31:02 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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!
Offline MrPork
« Reply #6 - Posted 2015-03-24 19:33:06 »

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

1  
2  
3  
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

   
1  
2  
3  
4  
5  
6  
7  
8  
@Override
   public void findEnemy(Ship ship) {

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

   }

"f**k it, maybe it'll work." -Me
Offline Opiop
« Reply #7 - Posted 2015-03-24 19:36:37 »

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?
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #8 - Posted 2015-03-24 19:50:45 »

Quote
but I have no idea how to check if two ships in the Array List are friendly or not.
Two ships:
1  
2  
3  
4  
5  
6  
7  
public boolean checkEnemy(Ship ship1, Ship ship2)
{
   if (ship1.getFaction() == ship2.getFaction())
      return false; //Friendly
   else
      return true; //Enemy
}

All list:
1  
2  
3  
4  
5  
6  
7  
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.

_ Wink
Offline cylab

JGO Kernel


Medals: 195



« Reply #9 - Posted 2015-03-24 19:51:14 »

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...

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MrPork
« Reply #10 - Posted 2015-03-24 20:03:56 »

Perfect, just what I needed! Thank you!

"f**k it, maybe it'll work." -Me
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (579 views)
2019-09-04 15:33:17

hadezbladez (5497 views)
2018-11-16 13:46:03

hadezbladez (2398 views)
2018-11-16 13:41:33

hadezbladez (5757 views)
2018-11-16 13:35:35

hadezbladez (1220 views)
2018-11-16 13:32:03

EgonOlsen (4659 views)
2018-06-10 19:43:48

EgonOlsen (5680 views)
2018-06-10 19:43:44

EgonOlsen (3196 views)
2018-06-10 19:43:20

DesertCoockie (4094 views)
2018-05-13 18:23:11

nelsongames (5113 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!