Line data Source code
1 : /** 2 : * @file An Area is a set of Vector2D points and a cache to lookup membership quickly. 3 : */ 4 : 5 : function Area(points) 6 : { 7 10 : this.points = deepfreeze(points); 8 : 9 10 : let mapSize = g_Map.getSize(); 10 : 11 4544 : this.cache = new Array(mapSize).fill(0).map(() => new Uint8Array(mapSize)); 12 10 : for (let point of points) 13 23956 : this.cache[point.x][point.y] = 1; 14 : } 15 : 16 6 : Area.prototype.getPoints = function() 17 : { 18 3 : return this.points; 19 : }; 20 : 21 6 : Area.prototype.contains = function(point) 22 : { 23 1276 : return g_Map.inMapBounds(point) && this.cache[point.x][point.y] == 1; 24 : }; 25 : 26 6 : Area.prototype.getClosestPointTo = function(position) 27 : { 28 0 : if (!this.points.length) 29 0 : return undefined; 30 : 31 0 : let closestPoint = this.points[0]; 32 0 : let shortestDistance = Infinity; 33 : 34 0 : for (let point of this.points) 35 : { 36 0 : let currentDistance = point.distanceToSquared(position); 37 0 : if (currentDistance < shortestDistance) 38 : { 39 0 : shortestDistance = currentDistance; 40 0 : closestPoint = point; 41 : } 42 : } 43 : 44 0 : return closestPoint; 45 : };