Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Actor structure
- function Actor( posx, posy){
- this.posx = posx;
- this.posy = posy;
- }
- //calc abs from a to b
- function dist(a, b){return Math.abs(b-a);}
- //finds nearest entity and return it ID
- function findNearest(MainActor){
- var nearest = 0;
- if(ActorList[0].posx === MainActor.posx && ActorList[0].posy === MainActor.posy){
- var minDistX = dist(MainActor.posx,ActorList[1].posx);
- var minDistY = dist(MainActor.posy,ActorList[1].posy);
- } else {
- var minDistX = dist(MainActor.posx,ActorList[0].posx);
- var minDistY = dist(MainActor.posy,ActorList[0].posy);
- }
- for(var i = 0; i < ActorList.length; i++){
- if(ActorList[i].posx === MainActor.posx && ActorList[i].posy === MainActor.posy) continue;
- else
- if(dist(MainActor.posx,ActorList[i].posx) <= minDistX && dist(MainActor.posy,ActorList[i].posy) <= minDistY){
- minDistX = dist(MainActor.posx,ActorList[i].posx);
- minDistY = dist(MainActor.posy,ActorList[i].posy);
- nearest = i;
- }
- }
- return nearest;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement