Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         /**
  2.          * Finds the Entity nearest to another inside a rectangle.
  3.          * @param   type        The Entity type to check for.
  4.          * @param   e           The Entity to find the nearest to.
  5.          * @param   rect        [x,y,width,height] create a rect
  6.          * @return  The nearest Entity to e.
  7.          */
  8.         public function nearestToEntityinRect(type:String, e:Entity, rect:Array):Entity
  9.         {
  10.             var n:Entity = _typeFirst[type],
  11.                 nearDist:Number = Number.MAX_VALUE,
  12.                 near:Entity, dist:Number,
  13.                 x:Number = e.x - e.originX,
  14.                 y:Number = e.y - e.originY;
  15.             while (n)
  16.             {
  17.                 if (n.x > rect[0] &&
  18.                     n.y > rect[1] &&
  19.                     n.x < rect[2] &&
  20.                     n.y < rect[3])
  21.                     {
  22.                         dist = (x - n.x) * (x - n.x) + (y - n.y) * (y - n.y);
  23.                         if (dist < nearDist)
  24.                         {
  25.                             nearDist = dist;
  26.                             near = n;
  27.                         }
  28.                     }
  29.                 n = n._typeNext;
  30.             }
  31.             return near;
  32.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement