|
| 1 | +// Script example for ScriptAPI |
| 2 | +// Author: Nperma <https://github.com/nperma> |
| 3 | +// Project: https://github.com/JaylyDev/ScriptAPI |
| 4 | +/** |
| 5 | + * Class for mathematical and spatial operations related to land usage. |
| 6 | + * @param {number[]} value - The initial coordinates [x, z] representing a point in space. |
| 7 | + * @constructor |
| 8 | + * @writter @Nperma |
| 9 | + */ |
| 10 | +export class MATH { |
| 11 | + constructor(value) { |
| 12 | + /** |
| 13 | + * Initial coordinates representing a point in space. |
| 14 | + * @type {number[]} |
| 15 | + */ |
| 16 | + this.value = value; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Check if the point is within a specified box. |
| 21 | + * @param {Object} box - The box object with start and end points defining the region. |
| 22 | + * @param {number[]} box.start - The starting coordinates [x, z] of the box. |
| 23 | + * @param {number[]} box.end - The ending coordinates [x, z] of the box. |
| 24 | + * @returns {boolean} - True if the point is inside the box, false otherwise. |
| 25 | + */ |
| 26 | + testInbox(box) { |
| 27 | + const pos = this.value; |
| 28 | + const { start, end } = box; |
| 29 | + return ( |
| 30 | + pos[0] >= Math.min(start[0], end[0]) && |
| 31 | + pos[0] <= Math.max(start[0], end[0]) && |
| 32 | + pos[1] >= Math.min(start[1], end[1]) && |
| 33 | + pos[1] <= Math.max(start[1], end[1]) |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the center point of a specified box. |
| 39 | + * @param {Object} box - The box object with start and end points defining the region. |
| 40 | + * @param {number[]} box.start - The starting coordinates [x, z] of the box. |
| 41 | + * @param {number[]} box.end - The ending coordinates [x, z] of the box. |
| 42 | + * @returns {Object} - Object with x and z properties representing the center coordinates. |
| 43 | + */ |
| 44 | + getCenter(box) { |
| 45 | + const { start, end } = box; |
| 46 | + const centerX = (start[0] + end[0]) / 2; |
| 47 | + const centerZ = (start[1] + end[1]) / 2; |
| 48 | + return { x: centerX, z: centerZ }; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get information about the proximity of the point to a specified tile. |
| 53 | + * @param {Object} box - The box object with start and end points defining the tile. |
| 54 | + * @param {number[]} box.start - The starting coordinates [x, z] of the tile. |
| 55 | + * @param {number[]} box.end - The ending coordinates [x, z] of the tile. |
| 56 | + * @returns {Object} - Object with status, center, distanceToEnter, and tile properties. |
| 57 | + */ |
| 58 | + getTile(box) { |
| 59 | + const center = this.getCenter(box); |
| 60 | + |
| 61 | + const status = this.testInbox(box); |
| 62 | + if (!status) { |
| 63 | + const distanceToEnterX = Math.min( |
| 64 | + Math.abs(this.value[0] - box.start[0]), |
| 65 | + Math.abs(this.value[0] - box.end[0]) |
| 66 | + ); |
| 67 | + |
| 68 | + const distanceToEnterZ = Math.min( |
| 69 | + Math.abs(this.value[1] - box.start[1]), |
| 70 | + Math.abs(this.value[1] - box.end[1]) |
| 71 | + ); |
| 72 | + |
| 73 | + return { |
| 74 | + status, |
| 75 | + center, |
| 76 | + distanceToEnter: Math.round( |
| 77 | + Math.sqrt(distanceToEnterX ** 2 + distanceToEnterZ ** 2) |
| 78 | + ), |
| 79 | + tile: "Tile Information" |
| 80 | + }; |
| 81 | + } |
| 82 | + return { |
| 83 | + status, |
| 84 | + center, |
| 85 | + distanceToEnter: 0, |
| 86 | + tile: "Tile Information" |
| 87 | + }; |
| 88 | + } |
| 89 | + } |
0 commit comments