83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import { Player, Vector3, world } from "@minecraft/server";
|
|
import { vector3 } from "../utils/vectorUtils";
|
|
|
|
export class Trigger {
|
|
point1: Vector3 = vector3(0, 0, 0);
|
|
point2: Vector3 = vector3(0, 0, 0);
|
|
eventToDispatch: string = "";
|
|
|
|
isPlayerInTrigger: Map<string, boolean> = new Map();
|
|
|
|
hasTriggerd: boolean = false;
|
|
|
|
constructor(point1: Vector3, point2: Vector3) {
|
|
this.point1 = point1;
|
|
this.point2 = point2;
|
|
}
|
|
|
|
hasPlayerEnterdTrigger(player: Player): boolean {
|
|
const minX = Math.min(this.point1.x, this.point2.x);
|
|
const maxX = Math.max(this.point1.x, this.point2.x);
|
|
const minY = Math.min(this.point1.y, this.point2.y);
|
|
const maxY = Math.max(this.point1.y, this.point2.y);
|
|
const minZ = Math.min(this.point1.z, this.point2.z);
|
|
const maxZ = Math.max(this.point1.z, this.point2.z);
|
|
|
|
const playerPos = player.location;
|
|
|
|
const inside =
|
|
playerPos.x >= minX &&
|
|
playerPos.x <= maxX &&
|
|
playerPos.y >= minY &&
|
|
playerPos.y + 1 <= maxY &&
|
|
playerPos.z >= minZ &&
|
|
playerPos.z <= maxZ;
|
|
|
|
let entererdTrigger = false;
|
|
if (!this.isPlayerInTrigger.get(player.name) && inside) {
|
|
entererdTrigger = true;
|
|
}
|
|
|
|
this.isPlayerInTrigger.set(player.name, inside);
|
|
|
|
return entererdTrigger;
|
|
}
|
|
|
|
IsPlayerInside(player: Player): boolean {
|
|
return this.isPlayerInTrigger.get(player.name) || false;
|
|
}
|
|
|
|
IsAnyPlayerInside(): boolean {
|
|
let isInside = false;
|
|
world.getAllPlayers().forEach((player) => {
|
|
if (this.isPlayerInTrigger.get(player.name)) {
|
|
isInside = true;
|
|
}
|
|
});
|
|
return isInside;
|
|
}
|
|
|
|
ShouldTrigger(): boolean {
|
|
let isSomethingInTrigger = false;
|
|
const players = world.getAllPlayers();
|
|
players.forEach((player) => {
|
|
if (this.hasPlayerEnterdTrigger(player)) {
|
|
isSomethingInTrigger = true;
|
|
}
|
|
});
|
|
return isSomethingInTrigger;
|
|
}
|
|
|
|
Update() {
|
|
world
|
|
.getDimension("overworld")
|
|
.getEntities({ type: "minecraft:player" })
|
|
.forEach((player) => {
|
|
//Check if the distance between the player and the trigger is less than the width of the trigger
|
|
// if (this.hasPlayerEnterdTrigger(player.location)) {
|
|
// world.sendMessage(`Player ${player.nameTag} is in trigger`);
|
|
// }
|
|
});
|
|
}
|
|
}
|