UNPKG

mylingo3d

Version:

Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor

269 lines 7.94 kB
import cubeShape from "./cannon/shapes/cubeShape"; import { Cancellable } from "@lincode/promiselikes"; import { assertExhaustive } from "@lincode/utils"; import { Point3d } from "@lincode/math"; import SimpleObjectManager from "../SimpleObjectManager"; import bvhContactMap from "./bvh/bvhContactMap"; import { cannonContactBodies, cannonContactMap } from "./cannon/cannonLoop"; import characterCameraPlaced from "../CharacterCamera/characterCameraPlaced"; export default class PhysicsObjectManager extends SimpleObjectManager { _mAV; getMAV() { return (this._mAV ??= new Point3d(Infinity, Infinity, Infinity)); } get maxAngularVelocityX() { return this._mAV?.x ?? Infinity; } set maxAngularVelocityX(val) { this.getMAV().x = val; } get maxAngularVelocityY() { return this._mAV?.y ?? Infinity; } set maxAngularVelocityY(val) { this.getMAV().y = val; } get maxAngularVelocityZ() { return this._mAV?.z ?? Infinity; } set maxAngularVelocityZ(val) { this.getMAV().z = val; } _mV; getMV() { return (this._mV ??= new Point3d(Infinity, Infinity, Infinity)); } get maxVelocityX() { return this._mV?.x ?? Infinity; } set maxVelocityX(val) { this.getMV().x = val; } get maxVelocityY() { return this._mV?.y ?? Infinity; } set maxVelocityY(val) { this.getMV().y = val; } get maxVelocityZ() { return this._mV?.z ?? Infinity; } set maxVelocityZ(val) { this.getMV().z = val; } cannonBody; applyForce(x, y, z) { setTimeout(() => this.cannonBody?.applyForce({ x, y, z })); } applyImpulse(x, y, z) { setTimeout(() => this.cannonBody?.applyImpulse({ x, y, z })); } applyLocalForce(x, y, z) { setTimeout(() => this.cannonBody?.applyLocalForce({ x, y, z })); } applyLocalImpulse(x, y, z) { setTimeout(() => this.cannonBody?.applyLocalImpulse({ x, y, z })); } applyTorque(x, y, z) { setTimeout(() => this.cannonBody?.applyTorque({ x, y, z })); } get velocity() { if (this.bvhVelocity) return this.bvhVelocity; if (this.cannonBody) return this.cannonBody.velocity; return new Point3d(0, 0, 0); } set velocity(val) { if (this.bvhVelocity) Object.assign(this.bvhVelocity, val); else if (this.cannonBody) Object.assign(this.cannonBody.velocity, val); } positionUpdate; rotationUpdate; refreshCannon() { this.positionUpdate && (this.physics = this._physics ?? false); } _noTumble; get noTumble() { return this._noTumble; } set noTumble(val) { this._noTumble = val; this.refreshCannon(); } _slippery; get slippery() { return this._slippery; } set slippery(val) { this._slippery = val; this.refreshCannon(); } _mass; get mass() { return this._mass; } set mass(val) { this._mass = val; this.refreshCannon(); } _physicsGroup; get physicsGroup() { return this._physicsGroup; } set physicsGroup(val) { this._physicsGroup = val; this.refreshCannon(); } _ignorePhysicsGroups; get ignorePhysicsGroups() { return this._ignorePhysicsGroups; } set ignorePhysicsGroups(val) { this._ignorePhysicsGroups = val; this.refreshCannon(); } _physicsShape; get physicsShape() { return (this._physicsShape ??= cubeShape); } set physicsShape(val) { this._physicsShape = val; this.refreshCannon(); } bvhVelocity; bvhOnGround; bvhRadius; bvhHalfHeight; bvhMap; bvhCharacter; initPhysics(val, handle) { if (!val || handle.done) return; switch (val) { case true: case "2d": import("./enableCannon").then((module) => module.default.call(this, handle)); break; case "map": this.bvhMap = true; import("./enableBVHMap").then((module) => module.default.call(this, handle, false)); break; case "map-debug": this.bvhMap = true; import("./enableBVHMap").then((module) => module.default.call(this, handle, true)); break; case "character": this.bvhCharacter = true; import("./enableBVHCharacter").then((module) => module.default.call(this, handle)); break; default: assertExhaustive(val); } } _physics; get physics() { return this._physics ?? false; } set physics(val) { this._physics = val; this.initPhysics(val, this.cancelHandle("physics", () => new Cancellable())); } _gravity; get gravity() { return this._gravity ?? true; } set gravity(val) { this._gravity = val; } intersects(target) { if (this.done) return false; if (target.done) return false; if (this === target) return false; if (target instanceof PhysicsObjectManager) { if ((this.bvhMap && target.bvhCharacter) || (this.bvhCharacter && target.bvhMap)) return (bvhContactMap.get(this)?.has(target) || bvhContactMap.get(target)?.has(this) || false); if (this.cannonBody && target.cannonBody) { cannonContactBodies.add(this.cannonBody); cannonContactBodies.add(target.cannonBody); return (cannonContactMap .get(this.cannonBody) ?.has(target.cannonBody) || cannonContactMap .get(target.cannonBody) ?.has(this.cannonBody) || false); } } return super.intersects(target); } get x() { return super.x; } set x(val) { super.x = val; this.positionUpdate?.updateX(); } get y() { return super.y; } set y(val) { super.y = val; this.positionUpdate?.updateY(); } get z() { return super.z; } set z(val) { super.z = val; this.positionUpdate?.updateZ(); } get rotationX() { return super.rotationX; } set rotationX(val) { super.rotationX = val; this.rotationUpdate?.updateX(); } get rotationY() { return super.rotationY; } set rotationY(val) { super.rotationY = val; this.rotationUpdate?.updateY(); } get rotationZ() { return super.rotationZ; } set rotationZ(val) { super.rotationZ = val; this.rotationUpdate?.updateZ(); } lookAt(a0, a1, a2) { super.lookAt(a0, a1, a2); this.rotationUpdate?.updateXYZ(); } placeAt(object) { super.placeAt(object); this.positionUpdate?.updateXYZ(); this.rotationUpdate?.updateXYZ(); characterCameraPlaced.add(this); } lerpTo(x, y, z, alpha) { super.lerpTo(x, y, z, alpha, () => this.positionUpdate?.updateXYZ()); } moveTo(x, y, z, speed) { super.moveTo(x, y, z, speed, (y) => y === undefined ? this.positionUpdate?.updateXZ() : this.positionUpdate?.updateXYZ()); } } //# sourceMappingURL=index.js.map