This site requires JavaScript, please enable it in your browser!
Greenfoot back
B_rob1
B_rob1 wrote ...

2018/7/11

Targeting Multiple different classes

B_rob1 B_rob1

2018/7/11

#
So with my current targeting system, It finds nearest unit.class and spawns the bullets in the direction of the units using lists, but how would I change my current code to apply to multiple targets in the list like Heavyunit.class and Fastunit.class
public void findClosestInRange()
    {
        List<unit> units = getObjectsInRange(maxRange, unit.class);
        
        
        if(units.size() != 0)
        {
            int unitX = units.get(0).getX();
            int unitY = units.get(0).getY();
            turnTowards(unitX, unitY);
            
            
            
        }
        
    }
    public void addBullet()
    {
        List<unit> ListOfTargets = getObjectsInRange(maxRange, unit.class);
        if(ListOfTargets.size() != 0 && reload >= 0)
        {
            LazerAmmo bullet = new LazerAmmo();
            getWorld().addObject(bullet, getX(),getY());
            bullet.setRotation(getRotation());
            bullet.move(95);
            
            reload -=reloadtimer;
        }
        if (reload < 0)
        {
            reload +=1;
        }
    }
danpost danpost

2018/7/11

#
B_rob1 wrote...
So with my current targeting system, It finds nearest unit.class and spawns the bullets in the direction of the units using lists, but how would I change my current code to apply to multiple targets in the list like Heavyunit.class and Fastunit.class << Code Omitted >>
Instead of a list of unit objects, make a list of Actor objects. Then you can add the lists of the other type objects to the list. Finally, you can find the closest Actor object contained in the concatenated list.
B_rob1 B_rob1

2018/7/11

#
I know what you are saying but I don't know how to actually code what that is I'm guessing an alternative list with something like
List<Actor> as = getObjectsInRange(maxRange, Actor.class);
danpost danpost

2018/7/12

#
B_rob1 wrote...
I know what you are saying but I don't know how to actually code what that is I'm guessing an alternative list with something like
List<Actor> as = getObjectsInRange(maxRange, Actor.class);
That is closer -- but, you still only want those particular type of actors:
List<Actor> as = getObjectsInRange(maxRange, unit.class);
This would still create a list of unit objects in range. This list, however, is not restricted to hold just unit objects. You can now add a list of Heavyunit object and a list of Fastunit objects to this list. See here. Apply that method to your as list.
B_rob1 B_rob1

2018/7/12

#
I have no idea what I'm supposed to do, I don't know the list feature at all
danpost danpost

2018/7/12

#
B_rob1 wrote...
I have no idea what I'm supposed to do, I don't know the list feature at all
Create the three lists (as, as2 and as3) and combine them into one list:
as.addAll(as2);
as.addAll(as3);
Then get the closest actor from the combined list.
danpost danpost

4 days ago

#
I was taking a closer look at your code. You said:
B_rob1 wrote...
So with my current targeting system, It finds nearest unit.class and spawns the bullets in the direction of the units using lists
where, in fact, it does not find the nearest one. It only has the actor turn toward the first one listed -- which is not necessarily the closest one. You need to go through the list and compare the distances from the actor to determine which is the closest. For that, you need two variables -- one for the closest actor found so far while going through the list and one for its distance from the actor to compare to other distances:
/** variables (not fields) */
Actor closest = null;
int distance = maxRange+1;
/** finding closest */
for (Actor a : as)
{
    int aDist = (int)Math.hypot(getX()-a.getX(), getY()-a.getY());
    if (aDist < distance)
    {
        distance = aDist;
        closest = a;
    }
}
/** turn/fire */
if (a != null)
{
    // turn code
    // bullet timer/spawn code
}
You need to login to post a reply.