@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 2.91 kB
JavaScript
import{SortedLoopArray as t}from"../../../core/sorted-loop-array.js";import{ComponentSystem as e}from"../system.js";import{ScriptComponent as o}from"./component.js";import{ScriptComponentData as s}from"./data.js";let n=0;class i extends e{constructor(e){super(e),this.id="script",this.ComponentType=o,this.DataType=s,this._components=new t({sortBy:"_executionOrder"}),this._enabledComponents=new t({sortBy:"_executionOrder"}),this.preloading=!0,this.on("beforeremove",this._onBeforeRemove,this),this.app.systems.on("initialize",this._onInitialize,this),this.app.systems.on("postInitialize",this._onPostInitialize,this),this.app.systems.on("update",this._onUpdate,this),this.app.systems.on("postUpdate",this._onPostUpdate,this)}initializeComponentData(t,e){if(t._executionOrder=n++,this._components.append(t),n>Number.MAX_SAFE_INTEGER&&this._resetExecutionOrder(),t.enabled=!e.hasOwnProperty("enabled")||!!e.enabled,t.enabled&&t.entity.enabled&&this._enabledComponents.append(t),e.hasOwnProperty("order")&&e.hasOwnProperty("scripts")){t._scriptsData=e.scripts;for(let o=0;o<e.order.length;o++)t.create(e.order[o],{enabled:e.scripts[e.order[o]].enabled,attributes:e.scripts[e.order[o]].attributes,preloading:this.preloading})}}cloneComponent(t,e){const o=[],s={};for(let e=0;e<t.script._scripts.length;e++){var n;const i=t.script._scripts[e],p=i.__scriptType.__name;o.push(p);const r=(null==(n=t.script._attributeDataMap)?void 0:n.get(p))||{};for(const t in i.__attributes)r[t]=i.__attributes[t];s[p]={enabled:i._enabled,attributes:r}}for(const e in t.script._scriptsIndex)e.awaiting&&o.splice(e.ind,0,e);const i={enabled:t.script.enabled,order:o,scripts:s};return this.addComponent(e,i)}_resetExecutionOrder(){n=0;for(let t=0,e=this._components.length;t<e;t++)this._components.items[t]._executionOrder=n++}_callComponentMethod(t,e,o){for(t.loopIndex=0;t.loopIndex<t.length;t.loopIndex++)t.items[t.loopIndex][e](o)}_onInitialize(){this.preloading=!1,this._callComponentMethod(this._components,"_onInitializeAttributes"),this._callComponentMethod(this._enabledComponents,"_onInitialize")}_onPostInitialize(){this._callComponentMethod(this._enabledComponents,"_onPostInitialize")}_onUpdate(t){this._callComponentMethod(this._enabledComponents,"_onUpdate",t)}_onPostUpdate(t){this._callComponentMethod(this._enabledComponents,"_onPostUpdate",t)}_addComponentToEnabled(t){this._enabledComponents.insert(t)}_removeComponentFromEnabled(t){this._enabledComponents.remove(t)}_onBeforeRemove(t,e){this._components.items.indexOf(e)>=0&&e._onBeforeRemove(),this._removeComponentFromEnabled(e),this._components.remove(e)}destroy(){super.destroy(),this.app.systems.off("initialize",this._onInitialize,this),this.app.systems.off("postInitialize",this._onPostInitialize,this),this.app.systems.off("update",this._onUpdate,this),this.app.systems.off("postUpdate",this._onPostUpdate,this)}}export{i as ScriptComponentSystem};