Page 1 of 1

Code Question

PostPosted: Mon Aug 16, 2010 9:51 am
by Zehper48
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.

Re: Code Question

PostPosted: Mon Aug 16, 2010 1:04 pm
by Game A Gogo
you need the GetClone2 function; so place this code in the global script and the GetClone2 should work
Code: Select all
Actor *GetClone2(const char *cname, int cindex)
{
    char buffer[50];
    sprintf(buffer,"%s.%i",cname,cindex);
    return(getclone(buffer));
}

and then to make the bullet go towards the closest enemy, here in the create actor of the bullet:
Code: Select all
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


I hope this work!

Re: Code Question

PostPosted: Mon Aug 16, 2010 4:25 pm
by Bee-Ant
You can also use this method :
Bullet -> Create Actor -> Script Editor :
Code: Select all
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;

Re: Code Question

PostPosted: Tue Aug 17, 2010 11:53 pm
by Zehper48
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 =]

Re: Code Question

PostPosted: Wed Aug 18, 2010 1:07 am
by Game A Gogo
do the same thing for creating the bullet, and check if the clones are withing region and create the actor according to that

Re: Code Question

PostPosted: Wed Aug 18, 2010 11:58 am
by Bee-Ant
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 =]

Code: Select all
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;
}