Files

3 lines
11 KiB
JavaScript

var gdjs;(function(n){const h=new n.Logger("RuntimeScene"),c=new n.Logger("RuntimeScene (setup warnings)");class u extends n.RuntimeInstanceContainer{constructor(e){super(e);this._eventsFunction=null;this._idToCallbackMap=null;this._lastId=0;this._name="";this._gameStopRequested=!1;this._requestedScene="";this._resourcesUnloading="inherit";this._asyncTasksManager=new n.AsyncTasksManager;this._isLoaded=!1;this._isJustResumed=!1;this._backgroundColor=0;this._clearCanvas=!0;this._profiler=null;this._onProfilerStopped=null;this.networkId=null;this._runtimeGame=e,this._variables=new n.VariablesContainer,this._variablesByExtensionName=new Map,this._timeManager=new n.TimeManager,this._requestedChange=d.CONTINUE,this._cachedGameResolutionWidth=e?e.getGameResolutionWidth():0,this._cachedGameResolutionHeight=e?e.getGameResolutionHeight():0,this._renderer=new n.RuntimeSceneRenderer(this,e?e.getRenderer():null),this._debuggerRenderer=new n.DebuggerRenderer(this),this.onGameResolutionResized()}addLayer(e){const t=new n.Layer(e,this);this._layers.put(e.name,t),this._orderedLayers.push(t)}onGameResolutionResized(){const e=this.getViewportOriginX(),t=this.getViewportOriginY();this._cachedGameResolutionWidth=this._runtimeGame?this._runtimeGame.getGameResolutionWidth():0,this._cachedGameResolutionHeight=this._runtimeGame?this._runtimeGame.getGameResolutionHeight():0;for(const r in this._layers.items)this._layers.items.hasOwnProperty(r)&&this._layers.items[r].onGameResolutionResized(e,t);this._renderer.onGameResolutionResized()}loadFromScene(e,t){if(!e){h.error("loadFromScene was called without a scene");return}const{sceneData:r,usedExtensionsWithVariablesData:s}=e;this._isLoaded&&this.unloadScene(),this._runtimeGame&&this._runtimeGame.getRenderer().setWindowTitle(r.title),this._name=r.name,this._resourcesUnloading=r.resourcesUnloading||"inherit",this.setBackgroundColor(r.r,r.v,r.b);for(let i=0,o=r.layers.length;i<o;++i)this.addLayer(r.layers[i]);this._variables=new n.VariablesContainer(r.variables);for(const i of s)this._variablesByExtensionName.set(i.name,new n.VariablesContainer(i.sceneVariables));for(let i=0,o=r.behaviorsSharedData.length;i<o;++i){const l=r.behaviorsSharedData[i];this.setInitialSharedDataForBehavior(l.name,l)}const a=this.getGame().getInitialObjectsData();for(let i=0,o=a.length;i<o;++i)this.registerObject(a[i]);for(let i=0,o=r.objects.length;i<o;++i)this.registerObject(r.objects[i]);if((!t||!t.skipCreatingInstances)&&this.createObjectsFrom(r.instances,0,0,0,!0,{excludedObjectNames:t?.excludedObjectNames}),this._setLayerDefaultZOrders(),this.setEventsGeneratedCodeFunction(r),this._onceTriggers=new n.OnceTriggers,this._runtimeGame&&!this._runtimeGame.wasFirstSceneLoaded())for(let i=0;i<n.callbacksFirstRuntimeSceneLoaded.length;++i)n.callbacksFirstRuntimeSceneLoaded[i](this);for(let i=0;i<n.callbacksRuntimeSceneLoaded.length;++i)n.callbacksRuntimeSceneLoaded[i](this);r.stopSoundsOnStartup&&(!t||!t.skipStoppingSoundsOnStartup)&&this._runtimeGame&&this._runtimeGame.getSoundManager().clearAll(),this._isLoaded=!0,this._timeManager.reset()}getInitialSharedDataForBehavior(e){const t=super.getInitialSharedDataForBehavior(e);return t||h.error("Can't find shared data for behavior with name: "+e),t}onPause(){const e=this.getAdhocListOfAllInstances();for(let t=0,r=e.length;t<r;++t)e[t].onScenePaused(this);for(let t=0;t<n.callbacksRuntimeScenePaused.length;++t)n.callbacksRuntimeScenePaused[t](this)}onResume(){this._isJustResumed=!0;const e=this.getAdhocListOfAllInstances();for(let t=0,r=e.length;t<r;++t)e[t].onSceneResumed(this);for(let t=0;t<n.callbacksRuntimeSceneResumed.length;++t)n.callbacksRuntimeSceneResumed[t](this)}unloadScene(){if(!this._isLoaded)return;this._profiler&&this.stopProfiler();for(let t=0;t<n.callbacksRuntimeSceneUnloading.length;++t)n.callbacksRuntimeSceneUnloading[t](this);const e=this.getAdhocListOfAllInstances();for(let t=0,r=e.length;t<r;++t){const s=e[t];s.onDeletedFromScene(),s.onDestroyed()}this._renderer&&this._renderer.onSceneUnloaded();for(let t=0;t<n.callbacksRuntimeSceneUnloaded.length;++t)n.callbacksRuntimeSceneUnloaded[t](this);this._destroy(),this._isLoaded=!1,this.onGameResolutionResized()}_destroy(){super._destroy(),this._variables=new n.VariablesContainer,this._variablesByExtensionName=new Map,this._initialBehaviorSharedData=new Hashtable,this._eventsFunction=null,this._lastId=0,this.networkId=null}setEventsGeneratedCodeFunction(e){const t=n[e.mangledName+"Code"];t&&t.func?(this._eventsFunction=t.func,this._idToCallbackMap=n[e.mangledName+"Code"].idToCallbackMap):(c.warn("No function found for running logic of scene "+this._name),this._eventsFunction=function(){})}setEventsFunction(e){this._eventsFunction=e}renderAndStep(e){this._profiler&&this._profiler.beginFrame(),this._requestedChange=d.CONTINUE,this._timeManager.update(e,this._runtimeGame.getMinimalFramerate()),this._profiler&&this._profiler.begin("asynchronous actions (wait action, etc...)"),this._asyncTasksManager.processTasks(this),this._profiler&&this._profiler.end("asynchronous actions (wait action, etc...)"),this._profiler&&this._profiler.begin("objects (pre-events)"),this._updateObjectsPreEvents(),this._profiler&&this._profiler.end("objects (pre-events)"),this._profiler&&this._profiler.begin("callbacks and extensions (pre-events)");for(let t=0;t<n.callbacksRuntimeScenePreEvents.length;++t)n.callbacksRuntimeScenePreEvents[t](this);this._profiler&&this._profiler.end("callbacks and extensions (pre-events)"),this._profiler&&this._profiler.begin("events"),this._eventsFunction!==null&&this._eventsFunction(this),this._profiler&&this._profiler.end("events"),this._profiler&&this._profiler.begin("objects (post-events)"),this._stepBehaviorsPostEvents(),this._profiler&&this._profiler.end("objects (post-events)"),this._profiler&&this._profiler.begin("callbacks and extensions (post-events)");for(let t=0;t<n.callbacksRuntimeScenePostEvents.length;++t)n.callbacksRuntimeScenePostEvents[t](this);return this._profiler&&this._profiler.end("callbacks and extensions (post-events)"),this.render(),this._isJustResumed=!1,this._profiler&&this._profiler.end("render"),this._profiler&&this._profiler.endFrame(),!!this.getRequestedChange()}render(){this._profiler&&this._profiler.begin("objects (pre-render, effects update)"),this._updateObjectsPreRender(),this._profiler&&this._profiler.end("objects (pre-render, effects update)"),this._profiler&&this._profiler.begin("layers (effects update)"),this._updateLayersPreRender(),this._profiler&&this._profiler.end("layers (effects update)"),this._profiler&&this._profiler.begin("render"),this._debugDrawEnabled&&this._debuggerRenderer.renderDebugDraw(this.getAdhocListOfAllInstances(),this._debugDrawShowHiddenInstances,this._debugDrawShowPointsNames,this._debugDrawShowCustomPoints),this._renderer.render()}_updateObjectsPreRender(){if(this._timeManager.isFirstFrame()){super._updateObjectsPreRender();return}else{this._updateLayersCameraCoordinates(2);const e=this.getAdhocListOfAllInstances();for(let t=0,r=e.length;t<r;++t){const s=e[t],a=s.getRendererObject();if(a){if(s.isHidden())a.visible=!1;else{const i=this._layersCameraCoordinates[s.getLayer()];if(!i)continue;const o=s.getVisibilityAABB();a.visible=!o||!(o.min[0]>i[2]||o.min[1]>i[3]||o.max[0]<i[0]||o.max[1]<i[1])}a.visible&&(this._runtimeGame.getEffectsManager().updatePreRender(s.getRendererEffects(),s),s.updatePreRender(this))}else s.updatePreRender(this)}}}setBackgroundColor(e,t,r){this._backgroundColor=parseInt(n.rgbToHex(e,t,r),16)}getBackgroundColor(){return this._backgroundColor}setClearCanvas(e){this._clearCanvas=e}getClearCanvas(){return this._clearCanvas}getName(){return this._name}getResourcesUnloading(){return this._resourcesUnloading}createNewUniqueId(){return this._lastId++,this._lastId}getRenderer(){return this._renderer}getDebuggerRenderer(){return this._debuggerRenderer}getGame(){return this._runtimeGame}getScene(){return this}getUnrotatedViewportMinX(){return 0}getUnrotatedViewportMinY(){return 0}getUnrotatedViewportMaxX(){return this._cachedGameResolutionWidth}getUnrotatedViewportMaxY(){return this._cachedGameResolutionHeight}getInitialUnrotatedViewportMinX(){return 0}getInitialUnrotatedViewportMinY(){return 0}getInitialUnrotatedViewportMaxX(){return this.getGame().getOriginalWidth()}getInitialUnrotatedViewportMaxY(){return this.getGame().getOriginalHeight()}getViewportWidth(){return this._cachedGameResolutionWidth}getViewportHeight(){return this._cachedGameResolutionHeight}getViewportOriginX(){return this._cachedGameResolutionWidth/2}getViewportOriginY(){return this._cachedGameResolutionHeight/2}convertCoords(e,t,r){const s=r||[0,0];return s[0]=e,s[1]=t,s}convertInverseCoords(e,t,r){const s=r||[0,0];return s[0]=e,s[1]=t,s}onChildrenLocationChanged(){}getVariables(){return this._variables}getVariablesForExtension(e){return this._variablesByExtensionName.get(e)||null}getTimeManager(){return this._timeManager}getElapsedTime(){return this._timeManager.getElapsedTime()}getSoundManager(){return this._runtimeGame.getSoundManager()}getAsyncTasksManager(){return this._asyncTasksManager}getRequestedChange(){return this._requestedChange}getRequestedScene(){return this._requestedScene}requestChange(e,t){this._requestedChange=e,t&&(this._requestedScene=t)}getProfiler(){return this._profiler}startProfiler(e){this._profiler||(this._profiler=new n.Profiler,this._onProfilerStopped=e)}stopProfiler(){if(!this._profiler)return;const e=this._profiler,t=this._onProfilerStopped;this._profiler=null,this._onProfilerStopped=null,t&&t(e)}sceneJustResumed(){return this._isJustResumed}getNetworkSyncData(e){const t=e.playerNumber,r=this._variables.getNetworkSyncData(e),s={};if(this._variablesByExtensionName.forEach((i,o)=>{const l=i.getNetworkSyncData(e);l&&(s[o]=l)}),t!==void 0&&t!==1&&!this.networkId)return null;const a={var:r,extVar:s,id:this.getOrCreateNetworkId()};if(e.syncSceneVisualProps&&(a.color=this._backgroundColor),e.syncLayers){const i={};for(const o in this._layers.items)i[o]=this._layers.items[o].getNetworkSyncData();a.layers=i}return e.syncSceneTimers&&(a.time=this._timeManager.getNetworkSyncData()),e.syncOnceTriggers&&(a.once=this._onceTriggers.getNetworkSyncData()),n.callbacksRuntimeSceneGetSyncData.forEach(i=>{i(this,a,e)}),e.syncAsyncTasks&&(a.async=this._asyncTasksManager.getNetworkSyncData(e)),a}updateFromNetworkSyncData(e,t){if(e.color!==void 0&&(this._backgroundColor=e.color),e.layers)for(const r in e.layers){const s=e.layers[r];this.hasLayer(r)&&this.getLayer(r).updateFromNetworkSyncData(s)}if(e.var&&this._variables.updateFromNetworkSyncData(e.var,t),e.extVar)for(const r in e.extVar){if(!e.extVar.hasOwnProperty(r))continue;const s=e.extVar[r],a=this._variablesByExtensionName.get(r);a&&a.updateFromNetworkSyncData(s,t)}e.time&&this._timeManager.updateFromNetworkSyncData(e.time),e.once&&this._onceTriggers.updateNetworkSyncData(e.once),n.callbacksRuntimeSceneUpdateFromSyncData.forEach(r=>{r(this,e,t)}),e.async&&this._idToCallbackMap&&this._asyncTasksManager.updateFromNetworkSyncData(e.async,this._idToCallbackMap,this,t)}getOrCreateNetworkId(){if(!this.networkId){const e=n.makeUuid().substring(0,8);this.networkId=e}return this.networkId}}n.RuntimeScene=u;let d;(function(i){i[i.CONTINUE=0]="CONTINUE",i[i.PUSH_SCENE=1]="PUSH_SCENE",i[i.POP_SCENE=2]="POP_SCENE",i[i.REPLACE_SCENE=3]="REPLACE_SCENE",i[i.CLEAR_SCENES=4]="CLEAR_SCENES",i[i.STOP_GAME=5]="STOP_GAME"})(d=n.SceneChangeRequest||(n.SceneChangeRequest={}))})(gdjs||(gdjs={}));
//# sourceMappingURL=runtimescene.js.map