Taking Aim
This function is used to move the NPC along the x or y cord to align it with its target so that it can shoot at it.

function TakingAim(){
if (this.distx<this.disty){
   if (x<this.testx&&!onwall(x+3+this.speed,y+1.5)){
     x+=this.speed;
     dir=3;
   }
   if (x>
this.testx&&!onwall(x-this.speed,y+1.5)){
     x-=this.speed;
     dir=1;
   }
}else{
   if (y< this.testy&&!onwall(x+1.5,y+3+this.speed)){
     y+=this.speed;
     dir=2;
   }
   if (y>
this.testy&&!onwall(x+1.5,y-this.speed)){
     y-=this.speed;
     dir=0;
   }
}
}

The first thing this function will do is compare this.distx to this.disty. If the x distance is shorter, that means that the baddy would have less distance to travel on the x cord to line up a shot with its target. So if that is the case, the next thing this function will do is check to see if the target is to the right, or to the left. If it is to the right, that means the x of the NPC is less than this.testx. So to line up the shot, the baddy needs to move to the right, so the walls are checked and its x value is added to as defined by this.speed in the Initialize() function. Because an archer moves differently than other baddies, this function automatically dictates the direction the NPC is to face. The rest of the function, from the }else{ is just the same thing, but for the y cords. Which check to see if the target is up or down, instead of left or right.

This function will need to be called within the Movement() function every timeout to keep the baddy moving.

Previous Topic         Home         Next Topic