Getting the closest target
Posted: Wed Nov 23, 2011 9:42 pm
How would I go about finding the closest enemy?
Game Editor discussion board
http://game-editor.com/forum/
int n;
double mindist = 10000;//set closest distance to out of range so the first check will work
Actor *actors, *closest = NULL;// NULL since 0 can be an actor technically and the check will fail
actors = getAllActorsInCollision("Event Actor", &n);// get all actors in the collision
// actors is the actors, n is the number of actors
if(actors)// if we have actors
{
int i;
for(i = 0; i < n; i++)// cycle through them
{
if(strcmp(actors[i].name, "a") == 0 ||// js it actor a
strcmp(actors[i].name, "c") == 0)// or is it actor c
{
double d = distance(parent.x, parent.y, actors[i].x, actors[i].y);// d is the distance of this actor from the player
if(d < mindist)// if the actor is closer than the the previous closest actor
{
closest = (actors + i);//make the closest actor
mindist = d;//new mindist
}
}
}
}
if(closest)//if we have a closest actor
{
MoveTo("Parent Actor", 0.000000, 0.000000, 2.000000, closest->clonename, "");// move our parent to the closest actor
}