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. |