Hey Everybody,
I'm making a tower defense sort of game.
How can I make the bullet from the tower move to the closest enemy clone within a certain pixel distance?
Thanks alot, and tell me If you don't understand what I am trying to do.
Actor *GetClone2(const char *cname, int cindex)
{
char buffer[50];
sprintf(buffer,"%s.%i",cname,cindex);
return(getclone(buffer));
}
unsigned int CDist=640;//So 0 isn't the closest value to 0... :P note any clone OUTSIDE this distance will not be considered... so set this at your need
for(i=0; i<ActorCount("Enemy"); i++)//Let's repeat this for all the clones "alive"
{
Clone=GetClone2("Enemy",i);//Let's get the current clone's information
if(distance(x,y,Clone->x,Clone->y)<CDist)//Check if the current clone is the closest yet
{
angle=direction(x,y,Clone->x,Clone->y); //set the angle
CDist=distance(x,y,Clone->x,Clone->y); //reset the closest distance
}
directional_velocity=3;//Speed of bullet
Actor *check;
char str[32];
int i,point,dist=1000;
//Search the closest target
for(i=0;i<ActorCount("enemy");i++)
{
sprintf(str,"enemy.%i",i);
check=getclone(str);
if(distance(x,y,check->x,check->y)<dist)
{
dist=distance(x,y,check->x,check->y);
point=i;
}
}
//Shoot the closest target
sprintf(str,"enemy.%i",point);
check=getclone(str);
angle=direction(x,y,check->x,check->y);
directional_velocity=10;
Zehper48 wrote:Thanks so much for replying Game A Gogo and Bee-Ant.
Both methods work great, but for each method, how can I make it so that when an enemy is not within the radius, the bullet isnt created?
Thanks so much for helping,
Greatly appreciated =]
Actor *check;
char str[32];
int i,point,radius=1000,create;
//Search the closest target
for(i=0;i<ActorCount("enemy");i++)
{
sprintf(str,"enemy.%i",i);
check=getclone(str);
if(distance(x,y,check->x,check->y)<radius)
{
radius=distance(x,y,check->x,check->y);
point=i;
create=1;
}
}
//Shoot the closest target if someone in range
if(create==1)
{
sprintf(str,"enemy.%i",point);
check=getclone(str);
angle=direction(x,y,check->x,check->y);
directional_velocity=10;
}
Users browsing this forum: No registered users and 1 guest