Setting up the Movement
This function is designed to some nice round variables to use to move the baddy in the next function.  It basically sets up some ratios to tell the NPC how far to move in each direction, and looks really smooth doing so.
 
function MoveCheck(){ 
  GetDist()
  if (this.distx< this.speed)this.addy= this.speed
  else if (this.disty< this.speed)this.addx= this.speed
  else if (this.distx> this.disty){ 
    this.ratio=this.disty/ this.distx
    this.addx= this.speed
    this.addy= this.speed*this.ratio; 
  }else if (this.disty> this.distx){ 
    this.ratio=this.distx/ this.disty
    this.addy= this.speed
    this.addx= this.speed*this.ratio; 
  } 
}
This function has to start with the previous function I called: GetDist().  Since that has been called, we are free to read the x and y distances to the target through our two familiar vars: this.distx and this.disty.  Once those have been obtained, the function proceeds to see if this.distx or this.disty is less than the speed value set in the Initialize() function, and if it is, then the vars we are going to use to move the baddy, this.addy and this.addx will be set to the speed variable.  However, if the x and y distances are greater than that, the it checks to see if the x distance is greater than the y distance.  If it is, then that means that the NPC has a greater distance to travel in the x direction than in the y direction.  So, this.addx is set to the speed var, and this.addy is set to a much smaller number, which is a ratio of the y distance to the x distance multiplied by the speed variable.  If the opposite is true, then this.disty is greater than this.distx, then all the opposite is done to the add vars.
This will only be called right before you want to move the baddy.  Of course this all assuming you want to move the baddy directly towards the target.  I have some scripts for keeping a close distance to a target, and that NPC does not use this function at all.  But the most common baddy moves directly towards the target so I included this function here.
 
Previous Topic         Home         Next Topic