101 lines
2.4 KiB
JavaScript
101 lines
2.4 KiB
JavaScript
/**
|
|
* Point - Represents a 3D coordinate in the Minecraft world
|
|
* Inspired by runi95/turtle-control-panel Point class
|
|
*/
|
|
export class Point {
|
|
constructor(x, y, z) {
|
|
this.x = x;
|
|
this.y = y;
|
|
this.z = z;
|
|
}
|
|
|
|
/**
|
|
* Returns the Manhattan distance to another point
|
|
*/
|
|
distanceTo(other) {
|
|
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y) + Math.abs(this.z - other.z);
|
|
}
|
|
|
|
/**
|
|
* Returns the Euclidean distance to another point (used for D* Lite heuristic)
|
|
*/
|
|
euclideanDistanceTo(other) {
|
|
const dx = this.x - other.x;
|
|
const dy = this.y - other.y;
|
|
const dz = this.z - other.z;
|
|
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
}
|
|
|
|
/**
|
|
* Check equality with another point
|
|
*/
|
|
equals(other) {
|
|
if (!other) return false;
|
|
return this.x === other.x && this.y === other.y && this.z === other.z;
|
|
}
|
|
|
|
/**
|
|
* Get all 6 neighboring points (up, down, north, south, east, west)
|
|
*/
|
|
getNeighbors() {
|
|
return [
|
|
new Point(this.x + 1, this.y, this.z), // East
|
|
new Point(this.x - 1, this.y, this.z), // West
|
|
new Point(this.x, this.y + 1, this.z), // Up
|
|
new Point(this.x, this.y - 1, this.z), // Down
|
|
new Point(this.x, this.y, this.z + 1), // South
|
|
new Point(this.x, this.y, this.z - 1), // North
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the cardinal direction needed to move from this point to another adjacent point
|
|
* Returns: 'forward', 'back', 'up', 'down', or facing adjustment needed
|
|
*/
|
|
directionTo(other) {
|
|
const dx = other.x - this.x;
|
|
const dy = other.y - this.y;
|
|
const dz = other.z - this.z;
|
|
|
|
if (dy === 1) return 'up';
|
|
if (dy === -1) return 'down';
|
|
if (dx === 1) return { facing: 1 }; // East
|
|
if (dx === -1) return { facing: 3 }; // West
|
|
if (dz === 1) return { facing: 2 }; // South
|
|
if (dz === -1) return { facing: 0 }; // North
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Unique string key for maps
|
|
*/
|
|
toKey() {
|
|
return `${this.x},${this.y},${this.z}`;
|
|
}
|
|
|
|
/**
|
|
* Create Point from key string
|
|
*/
|
|
static fromKey(key) {
|
|
const [x, y, z] = key.split(',').map(Number);
|
|
return new Point(x, y, z);
|
|
}
|
|
|
|
/**
|
|
* Create Point from object with x,y,z properties
|
|
*/
|
|
static from(obj) {
|
|
if (!obj) return null;
|
|
return new Point(obj.x, obj.y, obj.z);
|
|
}
|
|
|
|
toString() {
|
|
return `(${this.x}, ${this.y}, ${this.z})`;
|
|
}
|
|
|
|
toJSON() {
|
|
return { x: this.x, y: this.y, z: this.z };
|
|
}
|
|
}
|