source:
ps/trunk/binaries/data/mods/public/maps/random/rmgen/Area.js
| Last change on this file was 28036, checked in by , 11 months ago | |
|---|---|
|
|
| File size: 935 bytes | |
| Rev | Line | |
|---|---|---|
| [20400] | 1 | /** |
| [21296] | 2 | * @file An Area is a set of Vector2D points and a cache to lookup membership quickly. |
| [20400] | 3 | */ |
| [9271] | 4 | |
| [21390] | 5 | function Area(points) |
| [9096] | 6 | { |
| [21296] | 7 | this.points = deepfreeze(points); |
| 8 | ||
| [28036] | 9 | const mapSize = g_Map.getSize(); |
| [21296] | 10 | |
| [21436] | 11 | this.cache = new Array(mapSize).fill(0).map(() => new Uint8Array(mapSize)); |
| [28036] | 12 | for (const point of points) |
| [21296] | 13 | this.cache[point.x][point.y] = 1; |
| [9096] | 14 | } |
| [9271] | 15 | |
| [21296] | 16 | Area.prototype.getPoints = function() |
| [9271] | 17 | { |
| [21296] | 18 | return this.points; |
| [18839] | 19 | }; |
| [21224] | 20 | |
| [21296] | 21 | Area.prototype.contains = function(point) |
| 22 | { | |
| 23 | return g_Map.inMapBounds(point) && this.cache[point.x][point.y] == 1; | |
| 24 | }; | |
| 25 | ||
| [21224] | 26 | Area.prototype.getClosestPointTo = function(position) |
| 27 | { | |
| 28 | if (!this.points.length) | |
| 29 | return undefined; | |
| 30 | ||
| 31 | let closestPoint = this.points[0]; | |
| 32 | let shortestDistance = Infinity; | |
| 33 | ||
| [28036] | 34 | for (const point of this.points) |
| [21224] | 35 | { |
| [28036] | 36 | const currentDistance = point.distanceToSquared(position); |
| [21224] | 37 | if (currentDistance < shortestDistance) |
| 38 | { | |
| 39 | shortestDistance = currentDistance; | |
| 40 | closestPoint = point; | |
| 41 | } | |
| 42 | } | |
| 43 | ||
| 44 | return closestPoint; | |
| 45 | }; |
Note:
See TracBrowser
for help on using the repository browser.
