Shooting nearest target

Talk about making games.

Shooting nearest target

Postby sportmaster » Thu Jul 31, 2014 11:11 pm

I don't know how to do this. Please help me.
draw.png
User avatar
sportmaster
 
Posts: 32
Joined: Thu Jun 25, 2009 8:29 pm
Score: 0 Give a positive score

Re: Shooting nearest target

Postby skydereign » Fri Aug 01, 2014 12:26 am

You need to loop through all the blue clones. In the current build there isn't a built in way to do this, so you should create a variable called highestBlueCloneindex, and in the blue clone actor's create actor event, have this code.
Code: Select all
highestBlueCloneindex = cloneindex;

You want to track this number as you'll need to loop through every clone, starting from the lowest indexed to the highest. The reason the above code works to track the highest indexed clone is that the newest clone is always the one with the highest cloneindex. And we don't need to track the lowest cloneindex because the default actor struct can be used to get the lowest.

You'll also need to put this in global code, so you can use cloneindex to get references to the clones via cloneindex.
Code: Select all
Actor *getclone2(char* cname, int cindex)
{
    char buffer[30];
    sprintf(buffer,"%s.%d", cname, cindex);
    return(getclone(buffer));
}


That way whenever you want to get the angle to the closest clone you can do this.
Code: Select all
int i;
Actor* closest = NULL;
float closest_dist = 0;

for(i=blueClone.cloneindex; i<=highestBlueCloneindex; i++)
{
    Actor* i_clone = getclone2("blueClone", i);

    if(i_clone->cloneindex!=-1) // the clone's cloneindex is -1 when the clone does not exist, this checks only if the clone exists
    {
        float i_dist = distance(x, y, i_clone->x, i_clone->y);
        if(closest == NULL || closest_dist > i_dist) // either there is no closest assigned yet, or the current one is closer
        {
            closest_dist = i_dist; // update to new distance
            closest = i_clone; // store it
        }
    }
}

if(closest != NULL)
{
    // than closest is the blueClone that is closest to the event actor
    float ang = direction(x, y, closest->x, closest->y); // this is the angle you want
}

Let me know if you have any questions.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron