UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 3.85 kB
import{guid as t}from"../core/guid.js";import{GraphNode as e}from"../scene/graph-node.js";import{getApplication as i}from"./globals.js";const s=[];class n extends e{constructor(t,e=i()){super(t),this.anim=void 0,this.animation=void 0,this.audiolistener=void 0,this.button=void 0,this.camera=void 0,this.collision=void 0,this.element=void 0,this.gsplat=void 0,this.layoutchild=void 0,this.layoutgroup=void 0,this.light=void 0,this.model=void 0,this.particlesystem=void 0,this.render=void 0,this.rigidbody=void 0,this.screen=void 0,this.script=void 0,this.scrollbar=void 0,this.scrollview=void 0,this.sound=void 0,this.sprite=void 0,this.c={},this._app=void 0,this._destroying=!1,this._guid=null,this._template=!1,this._app=e}addComponent(t,e){const i=this._app.systems[t];return i?this.c[t]?null:i.addComponent(this,e):null}removeComponent(t){const e=this._app.systems[t];e&&this.c[t]&&e.removeComponent(this)}findComponent(t){const e=this.findOne((e=>e.c&&e.c[t]));return e&&e.c[t]}findComponents(t){return this.find((e=>e.c&&e.c[t])).map((e=>e.c[t]))}findScript(t){const e=this.findOne((e=>{var i;return null==(i=e.c)||null==(i=i.script)?void 0:i.has(t)}));return null==e?void 0:e.c.script.get(t)}findScripts(t){return this.find((e=>{var i;return null==(i=e.c)||null==(i=i.script)?void 0:i.has(t)})).map((e=>e.c.script.get(t)))}getGuid(){return this._guid||this.setGuid(t.create()),this._guid}setGuid(t){const e=this._app._entityIndex;this._guid&&delete e[this._guid],this._guid=t,e[this._guid]=this}_notifyHierarchyStateChanged(t,e){let i=!1;t===this&&0===s.length&&(i=!0),t._beingEnabled=!0,t._onHierarchyStateChanged(e),t._onHierarchyStatePostChanged&&s.push(t);const n=t._children;for(let t=0,i=n.length;t<i;t++)n[t]._enabled&&this._notifyHierarchyStateChanged(n[t],e);if(t._beingEnabled=!1,i){for(let t=0;t<s.length;t++)s[t]._onHierarchyStatePostChanged();s.length=0}}_onHierarchyStateChanged(t){super._onHierarchyStateChanged(t);const e=this.c;for(const i in e)if(e.hasOwnProperty(i)){const s=e[i];s.enabled&&(t?s.onEnable():s.onDisable())}}_onHierarchyStatePostChanged(){const t=this.c;for(const e in t)t.hasOwnProperty(e)&&t[e].onPostStateChange()}findByGuid(t){if(this._guid===t)return this;const e=this._app._entityIndex[t];return e&&(e===this||e.isDescendantOf(this))?e:null}destroy(){this._destroying=!0;for(const t in this.c)this.c[t].enabled=!1;for(const t in this.c)this.c[t].system.removeComponent(this);super.destroy(),this._guid&&delete this._app._entityIndex[this._guid],this._destroying=!1}clone(){const t={},e=this._cloneRecursively(t);return t[this.getGuid()]=e,o(this,this,e,t),e}_cloneRecursively(t){const e=new this.constructor(void 0,this._app);super._cloneInternal(e);for(const t in this.c){this.c[t].system.cloneComponent(this,e)}for(let i=0;i<this._children.length;i++){const s=this._children[i];if(s instanceof n){const i=s._cloneRecursively(t);e.addChild(i),t[s.getGuid()]=i}}return e}}function o(t,e,i,s){if(e instanceof n){const r=e.c;for(const e in r){const n=r[e],o=n.system.getPropertiesOfType("entity");for(let r=0,c=o.length;r<c;r++){const c=o[r].name,h=n[c];if(!!t.findByGuid(h)){const t=s[h].getGuid();t&&(i.c[e][c]=t)}}}r.script&&!i._app.useLegacyScriptAttributeCloning&&i.script.resolveDuplicatedEntityReferenceProperties(r.script,s),r.render&&i.render.resolveDuplicatedEntityReferenceProperties(r.render,s),r.button&&i.button.resolveDuplicatedEntityReferenceProperties(r.button,s),r.scrollview&&i.scrollview.resolveDuplicatedEntityReferenceProperties(r.scrollview,s),r.scrollbar&&i.scrollbar.resolveDuplicatedEntityReferenceProperties(r.scrollbar,s),r.anim&&i.anim.resolveDuplicatedEntityReferenceProperties(r.anim,s);const c=e.children.filter((t=>t instanceof n)),h=i.children.filter((t=>t instanceof n));for(let e=0,i=c.length;e<i;e++)o(t,c[e],h[e],s)}}n.EVENT_DESTROY="destroy";export{n as Entity};