Retreating From the Target
This function is used to move the NPC away from its target when the two are too close to each other.

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

The first thing this function will do is compare this.distx to this.disty . If the x distance is shorter, that means the NPC can have a better chance of escaping if it goes along the y axis. So if that is the case, the next thing this function will do is check to see if the target is above or below the NPC. If it is above, that means the y of the NPC is less than this.testy . So to escape, the baddy needs to move to the up, so the walls are checked and its y value is altered by this.speed/2 as define in the Initialize() function. The rest is the same but for when the y distances are shorter.

This function will need to be called within the Movement() function when the distance between the NPC and its target, this.dist, is less than or equal to the distance being kept by the NPC, this.keepdist, as define in the function Initialize().

Previous Topic         Home         Next Topic