Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Finds the Entity nearest to another inside a rectangle.
- * @param type The Entity type to check for.
- * @param e The Entity to find the nearest to.
- * @param rect [x,y,width,height] create a rect
- * @return The nearest Entity to e.
- */
- public function nearestToEntityinRect(type:String, e:Entity, rect:Array):Entity
- {
- var n:Entity = _typeFirst[type],
- nearDist:Number = Number.MAX_VALUE,
- near:Entity, dist:Number,
- x:Number = e.x - e.originX,
- y:Number = e.y - e.originY;
- while (n)
- {
- if (n.x > rect[0] &&
- n.y > rect[1] &&
- n.x < rect[2] &&
- n.y < rect[3])
- {
- dist = (x - n.x) * (x - n.x) + (y - n.y) * (y - n.y);
- if (dist < nearDist)
- {
- nearDist = dist;
- near = n;
- }
- }
- n = n._typeNext;
- }
- return near;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement