Skip to content

Commit

Permalink
Create math.js (#338)
Browse files Browse the repository at this point in the history
* Create math.js

* Create Readme.md

* Create test.js

* Update test.js

* Rename Readme.md to Readme.md

* Update test.js

* Update test.js

* Update and rename math.js to index.js

* Rename test.js to tests.js

* Update tests.js

* Update Readme.md

* Update index.js

---------

Co-authored-by: bot174 <91819282+bot174@users.noreply.github.com>
  • Loading branch information
nperma and bot174 authored Dec 19, 2023
1 parent 2d9988d commit df6e5c1
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
11 changes: 11 additions & 0 deletions scripts/land-calculate/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Description
This Class to Calculate Land [X,Z]

### Method
- testInBox({ start: [x,z], end: [x,z] })
- getTile({ start: [x,z], end: [x,z] })

check `tests.js`

# Credits
@Nperma
89 changes: 89 additions & 0 deletions scripts/land-calculate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Script example for ScriptAPI
// Author: Nperma <https://github.com/nperma>
// Project: https://github.com/JaylyDev/ScriptAPI
/**
* Class for mathematical and spatial operations related to land usage.
* @param {number[]} value - The initial coordinates [x, z] representing a point in space.
* @constructor
* @writter @Nperma
*/
export class MATH {
constructor(value) {
/**
* Initial coordinates representing a point in space.
* @type {number[]}
*/
this.value = value;
}

/**
* Check if the point is within a specified box.
* @param {Object} box - The box object with start and end points defining the region.
* @param {number[]} box.start - The starting coordinates [x, z] of the box.
* @param {number[]} box.end - The ending coordinates [x, z] of the box.
* @returns {boolean} - True if the point is inside the box, false otherwise.
*/
testInbox(box) {
const pos = this.value;
const { start, end } = box;
return (
pos[0] >= Math.min(start[0], end[0]) &&
pos[0] <= Math.max(start[0], end[0]) &&
pos[1] >= Math.min(start[1], end[1]) &&
pos[1] <= Math.max(start[1], end[1])
);
}

/**
* Get the center point of a specified box.
* @param {Object} box - The box object with start and end points defining the region.
* @param {number[]} box.start - The starting coordinates [x, z] of the box.
* @param {number[]} box.end - The ending coordinates [x, z] of the box.
* @returns {Object} - Object with x and z properties representing the center coordinates.
*/
getCenter(box) {
const { start, end } = box;
const centerX = (start[0] + end[0]) / 2;
const centerZ = (start[1] + end[1]) / 2;
return { x: centerX, z: centerZ };
}

/**
* Get information about the proximity of the point to a specified tile.
* @param {Object} box - The box object with start and end points defining the tile.
* @param {number[]} box.start - The starting coordinates [x, z] of the tile.
* @param {number[]} box.end - The ending coordinates [x, z] of the tile.
* @returns {Object} - Object with status, center, distanceToEnter, and tile properties.
*/
getTile(box) {
const center = this.getCenter(box);

const status = this.testInbox(box);
if (!status) {
const distanceToEnterX = Math.min(
Math.abs(this.value[0] - box.start[0]),
Math.abs(this.value[0] - box.end[0])
);

const distanceToEnterZ = Math.min(
Math.abs(this.value[1] - box.start[1]),
Math.abs(this.value[1] - box.end[1])
);

return {
status,
center,
distanceToEnter: Math.round(
Math.sqrt(distanceToEnterX ** 2 + distanceToEnterZ ** 2)
),
tile: "Tile Information"
};
}
return {
status,
center,
distanceToEnter: 0,
tile: "Tile Information"
};
}
}
26 changes: 26 additions & 0 deletions scripts/land-calculate/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as mc from "@minecraft/server";
import { MATH } from "./index.js";

const AreaLobby = { start: [200, -200], end: [-200, 200] }; // Center 0,0

let playerLog = {};

mc.system.runInterval(async () => {
mc.world.getPlayers().forEach((ply) => {
if (!playerLog[ply.name]) {
playerLog[ply.name] = {
status: "out land",
};
}

const playerLocation = new MATH([ply.location.x, ply.location.z]);

if (playerLocation.testInbox(AreaLobby) && playerLog[ply.name].status === "out land") {
ply.sendMessage("§7You are inside this Area Lobby");
playerLog[ply.name].status = "in land";
} else if (!playerLocation.testInbox(AreaLobby) && playerLog[ply.name].status === "in land") {
ply.sendMessage("§7You are out of this Area Lobby!!");
playerLog[ply.name].status = "out land";
}
});
});

0 comments on commit df6e5c1

Please sign in to comment.