How To Target Any Nearby Enemy

Non-platform specific questions.

How To Target Any Nearby Enemy

Postby happyjustbecause » Sat Mar 16, 2013 8:37 pm

Hello everyone,

I have been slacking on game developing for a while and I'm ready to get back into it for a little bit.

So, what I need help doing is targeting any nearby enemy with a crescent (a projectile weapon). I want to have an upgrade for the crescent that starts constantly moving towards an enemy (like heat seeking missiles or whatever). Usually the crescent just goes in one direction only changing direction by colliding with something. But I want this to kind of pull towards a nearby enemy when the upgrade is active.

I know that for the actual following I would just have to have directional velocity and an angle in the draw actor of the crescent with some if statements to be upgrade specific, but how could I be specific about what enemy to target?

for the code I would need something like:

Code: Select all
directional velocity=10;
angle=direction(x,y, Nearby Enemy.x, Nearby Enemy.y);


How should I make the variable nearby enemy, basically.

And also how could I prevent the crescent from messing up the targeting when there are multiple nearby enemies?

Hopefully it's understood what I want, I have an idea on how to do this, but not completely.
For small creatures such as we the vastness is bearable only through love.
-Carl Sagan

Night Knight Development Thread
User avatar
happyjustbecause
 
Posts: 267
Joined: Tue Jul 26, 2011 3:10 pm
Location: Frazier Park, Ca
Score: 15 Give a positive score

Re: How To Target Any Nearby Enemy

Postby skydereign » Sat Mar 16, 2013 9:12 pm

happyjustbecause wrote:How should I make the variable nearby enemy, basically.

Do you remember for the pausing all actors, you had to loop through every possible enemy actors? Same idea here. You have to loop through all enemies, checking the distances.
Code: Select all
Actor* clone;
int dist = 10000;
int t_dist; // used to prevent calling distance twice per match
int i;

for(i=enemy.cloneindex; i=max_enemy_index; i++)
{
    clone = getclone2("enemy", i);
    if(clone->cloneindex!=-1) // make sure the clone exists
    {
        t_dist = distance(x, y, clone->x, clone->y);
        if(t_dist<dist)
        {
            dist = t_dist;
            enemy_index = i; // holds the closest enemy's index
        }
    }
}
// now you know the closest enemy, and can get it using getclone2("enemy", enemy_index)

Now you'll probably need to do that for every enemy type as well, and I don't really recommend putting that in missile's draw event. But, you do need to call that pretty frequently if you want it to be able to change targets.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron