import { Mesh } from "three"; import { Gizmos } from "../engine/engine_gizmos.js"; import { syncDestroy } from "../engine/engine_networking_instantiate.js"; import { getParam } from "../engine/engine_utils.js"; import { BoxHelperComponent } from "./BoxHelperComponent.js"; import { Behaviour, GameObject } from "./Component.js"; import { UsageMarker } from "./Interactable.js"; const debug = getParam("debugdeletable"); /** * A box-shaped area that can be used to delete objects that get into it. Useful for sandbox-style builders or physics simulations. * @category Interactivity * @group Components */ export class DeleteBox extends BoxHelperComponent { static _instances: DeleteBox[] = []; onEnable(): void { DeleteBox._instances.push(this); } onDisable(): void { const idx = DeleteBox._instances.indexOf(this); if (idx >= 0) DeleteBox._instances.splice(idx, 1); } } /** Objects with this component can be destroyed by the {@link DeleteBox} component. * @category Interactivity * @group Components */ export class Deletable extends Behaviour { update(): void { for (const box of DeleteBox._instances) { const obj = this.gameObject as unknown as Mesh; const res = box.isInBox(obj); if (res === true) { const marker = GameObject.getComponentInParent(this.gameObject, UsageMarker); if (!marker) { if (debug) { try { if (box["box"]) { const deleteBoxArea = box["box"]; const deletedObjectArea = BoxHelperComponent["testBox"]; Gizmos.DrawWireBox3(deleteBoxArea, 0xff0000, 5); Gizmos.DrawWireBox3(deletedObjectArea, 0x0000ff, 5); console.log("DeleteBox: Destroying", this.gameObject, { deleteBoxArea, deletedObjectArea }); } else { console.log("DeleteBox: Destroying", this.gameObject); } } catch (_e) {} } syncDestroy(this.gameObject, this.context.connection); } else if (debug) console.warn("DeleteBox: Not deleting object with usage marker", this.guid, marker) } } } }