@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 7.18 kB
JavaScript
import{Quat as t}from"../../../core/math/quat.js";import{Vec3 as e}from"../../../core/math/vec3.js";import{Asset as s}from"../../asset/asset.js";import{Component as i}from"../component.js";const a=new e,n=new t;class h extends i{constructor(t,e){super(t,e),this._compoundParent=null,this._hasOffset=!1,this.entity.on("insert",this._onInsert,this),this.on("set_type",this.onSetType,this),this.on("set_convexHull",this.onSetModel,this),this.on("set_halfExtents",this.onSetHalfExtents,this),this.on("set_linearOffset",this.onSetOffset,this),this.on("set_angularOffset",this.onSetOffset,this),this.on("set_radius",this.onSetRadius,this),this.on("set_height",this.onSetHeight,this),this.on("set_axis",this.onSetAxis,this),this.on("set_asset",this.onSetAsset,this),this.on("set_renderAsset",this.onSetRenderAsset,this),this.on("set_model",this.onSetModel,this),this.on("set_render",this.onSetRender,this)}get data(){const t=this.system.store[this.entity.getGuid()];return t?t.data:null}set enabled(t){this._setValue("enabled",t)}get enabled(){return this.data.enabled}set type(t){this._setValue("type",t)}get type(){return this.data.type}set halfExtents(t){this._setValue("halfExtents",t)}get halfExtents(){return this.data.halfExtents}set linearOffset(t){this._setValue("linearOffset",t)}get linearOffset(){return this.data.linearOffset}set angularOffset(t){this._setValue("angularOffset",t)}get angularOffset(){return this.data.angularOffset}set radius(t){this._setValue("radius",t)}get radius(){return this.data.radius}set axis(t){this._setValue("axis",t)}get axis(){return this.data.axis}set height(t){this._setValue("height",t)}get height(){return this.data.height}set asset(t){this._setValue("asset",t)}get asset(){return this.data.asset}set renderAsset(t){this._setValue("renderAsset",t)}get renderAsset(){return this.data.renderAsset}set convexHull(t){this._setValue("convexHull",t)}get convexHull(){return this.data.convexHull}set shape(t){this._setValue("shape",t)}get shape(){return this.data.shape}set model(t){this._setValue("model",t)}get model(){return this.data.model}set render(t){this._setValue("render",t)}get render(){return this.data.render}set checkVertexDuplicates(t){this._setValue("checkVertexDuplicates",t)}get checkVertexDuplicates(){return this.data.checkVertexDuplicates}_setValue(t,e){const s=this.data,i=s[t];s[t]=e,this.fire("set",t,i,e)}onSetType(t,e,s){e!==s&&this.system.changeType(this,e,s)}onSetHalfExtents(t,e,s){const i=this.data.type;this.data.initialized&&"box"===i&&this.system.recreatePhysicalShapes(this)}onSetOffset(s,i,a){this._hasOffset=!this.data.linearOffset.equals(e.ZERO)||!this.data.angularOffset.equals(t.IDENTITY),this.data.initialized&&this.system.recreatePhysicalShapes(this)}onSetRadius(t,e,s){const i=this.data.type;!this.data.initialized||"sphere"!==i&&"capsule"!==i&&"cylinder"!==i&&"cone"!==i||this.system.recreatePhysicalShapes(this)}onSetHeight(t,e,s){const i=this.data.type;!this.data.initialized||"capsule"!==i&&"cylinder"!==i&&"cone"!==i||this.system.recreatePhysicalShapes(this)}onSetAxis(t,e,s){const i=this.data.type;!this.data.initialized||"capsule"!==i&&"cylinder"!==i&&"cone"!==i||this.system.recreatePhysicalShapes(this)}onSetAsset(t,e,i){const a=this.system.app.assets;if(e){const t=a.get(e);t&&t.off("remove",this.onAssetRemoved,this)}if(i){i instanceof s&&(this.data.asset=i.id);const t=a.get(this.data.asset);t&&(t.off("remove",this.onAssetRemoved,this),t.on("remove",this.onAssetRemoved,this))}this.data.initialized&&"mesh"===this.data.type&&(i||(this.data.model=null),this.system.recreatePhysicalShapes(this))}onSetRenderAsset(t,e,i){const a=this.system.app.assets;if(e){const t=a.get(e);t&&t.off("remove",this.onRenderAssetRemoved,this)}if(i){i instanceof s&&(this.data.renderAsset=i.id);const t=a.get(this.data.renderAsset);t&&(t.off("remove",this.onRenderAssetRemoved,this),t.on("remove",this.onRenderAssetRemoved,this))}this.data.initialized&&"mesh"===this.data.type&&(i||(this.data.render=null),this.system.recreatePhysicalShapes(this))}onSetModel(t,e,s){this.data.initialized&&"mesh"===this.data.type&&this.system.implementations.mesh.doRecreatePhysicalShape(this)}onSetRender(t,e,s){this.onSetModel(t,e,s)}onAssetRemoved(t){t.off("remove",this.onAssetRemoved,this),this.data.asset===t.id&&(this.asset=null)}onRenderAssetRemoved(t){t.off("remove",this.onRenderAssetRemoved,this),this.data.renderAsset===t.id&&(this.renderAsset=null)}getCompoundChildShapeIndex(t){const e=this.data.shape,s=e.getNumChildShapes();for(let i=0;i<s;i++){const s=e.getChildShape(i);if(Ammo.getPointer(s)===Ammo.getPointer(t))return i}return null}_onInsert(t){if("undefined"!=typeof Ammo)if(this._compoundParent)this.system.recreatePhysicalShapes(this);else if(!this.entity.rigidbody){let t=this.entity.parent;for(;t;){if(t.collision&&"compound"===t.collision.type){0===t.collision.shape.getNumChildShapes()?this.system.recreatePhysicalShapes(t.collision):this.system.recreatePhysicalShapes(this);break}t=t.parent}}}_updateCompound(){const t=this.entity;if(t._dirtyWorld){let e=t._dirtyLocal,s=t;for(;s&&!e&&(!s.collision||s.collision!==this._compoundParent);)s._dirtyLocal&&(e=!0),s=s.parent;if(e){t.forEach(this.system.implementations.compound._updateEachDescendantTransform,t);const e=this._compoundParent.entity.rigidbody;e&&e.activate()}}}getShapePosition(){const t=this.entity.getPosition();if(this._hasOffset){const e=this.entity.getRotation(),s=this.data.linearOffset;return n.copy(e).transformVector(s,a),a.add(t)}return t}getShapeRotation(){const t=this.entity.getRotation();return this._hasOffset?n.copy(t).mul(this.data.angularOffset):t}onEnable(){if("mesh"===this.data.type&&(this.data.asset||this.data.renderAsset)&&this.data.initialized){const t=this.system.app.assets.get(this.data.asset||this.data.renderAsset);if(t&&(!t.resource||!this.data.shape))return void this.system.recreatePhysicalShapes(this)}if(this.entity.rigidbody)this.entity.rigidbody.enabled&&this.entity.rigidbody.enableSimulation();else if(this._compoundParent&&this!==this._compoundParent)if(0===this._compoundParent.shape.getNumChildShapes())this.system.recreatePhysicalShapes(this._compoundParent);else{const t=this.system._getNodeTransform(this.entity,this._compoundParent.entity);this._compoundParent.shape.addChildShape(t,this.data.shape),Ammo.destroy(t),this._compoundParent.entity.rigidbody&&this._compoundParent.entity.rigidbody.activate()}else this.entity.trigger&&this.entity.trigger.enable()}onDisable(){this.entity.rigidbody?this.entity.rigidbody.disableSimulation():this._compoundParent&&this!==this._compoundParent?this._compoundParent.entity._destroying||(this.system._removeCompoundChild(this._compoundParent,this.data.shape),this._compoundParent.entity.rigidbody&&this._compoundParent.entity.rigidbody.activate()):this.entity.trigger&&this.entity.trigger.disable()}onBeforeRemove(){this.asset&&(this.asset=null),this.renderAsset&&(this.renderAsset=null),this.entity.off("insert",this._onInsert,this),this.off()}}h.EVENT_CONTACT="contact",h.EVENT_COLLISIONSTART="collisionstart",h.EVENT_COLLISIONEND="collisionend",h.EVENT_TRIGGERENTER="triggerenter",h.EVENT_TRIGGERLEAVE="triggerleave";export{h as CollisionComponent};