-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtimescene.js
2 lines (2 loc) · 13.6 KB
/
runtimescene.js
1
2
var gdjs;(function(r){const h=new r.Logger("RuntimeScene"),d=new r.Logger("RuntimeScene (setup warnings)");class u{constructor(e){this._eventsFunction=null;this._lastId=0;this._name="";this._gameStopRequested=!1;this._requestedScene="";this._isLoaded=!1;this._isJustResumed=!1;this._backgroundColor=0;this._allInstancesList=[];this._layersCameraCoordinates={};this._instancesRemoved=[];this._profiler=null;this._debugDrawEnabled=!1;this._debugDrawShowHiddenInstances=!1;this._debugDrawShowPointsNames=!1;this._debugDrawShowCustomPoints=!1;this._onProfilerStopped=null;this._instances=new Hashtable,this._instancesCache=new Hashtable,this._objects=new Hashtable,this._objectsCtor=new Hashtable,this._layers=new Hashtable,this._initialBehaviorSharedData=new Hashtable,this._renderer=new r.RuntimeSceneRenderer(this,e?e.getRenderer():null),this._variables=new r.VariablesContainer,this._runtimeGame=e,this._timeManager=new r.TimeManager,this._requestedChange=c.CONTINUE,this._onceTriggers=new r.OnceTriggers,this.onGameResolutionResized()}enableDebugDraw(e,s,t,i){this._debugDrawEnabled&&!e&&this.getRenderer().clearDebugDraw(),this._debugDrawEnabled=e,this._debugDrawShowHiddenInstances=s,this._debugDrawShowPointsNames=t,this._debugDrawShowCustomPoints=i}onGameResolutionResized(){for(const e in this._layers.items)this._layers.items.hasOwnProperty(e)&&this._layers.items[e].onGameResolutionResized();this._renderer.onGameResolutionResized()}loadFromScene(e){if(!e){h.error("loadFromScene was called without a scene");return}this._isLoaded&&this.unloadScene(),this._runtimeGame&&this._runtimeGame.getRenderer().setWindowTitle(e.title),this._name=e.name,this.setBackgroundColor(e.r,e.v,e.b);for(let t=0,i=e.layers.length;t<i;++t)this.addLayer(e.layers[t]);this._variables=new r.VariablesContainer(e.variables);for(let t=0,i=e.behaviorsSharedData.length;t<i;++t){const n=e.behaviorsSharedData[t];this.setInitialSharedDataForBehavior(n.name,n)}const s=this.getGame().getInitialObjectsData();for(let t=0,i=s.length;t<i;++t)this.registerObject(s[t]);for(let t=0,i=e.objects.length;t<i;++t)this.registerObject(e.objects[t]);if(this.createObjectsFrom(e.instances,0,0,!0),this._setLayerDefaultZOrders(),this.setEventsGeneratedCodeFunction(e),this._onceTriggers=new r.OnceTriggers,this._runtimeGame&&!this._runtimeGame.wasFirstSceneLoaded())for(let t=0;t<r.callbacksFirstRuntimeSceneLoaded.length;++t)r.callbacksFirstRuntimeSceneLoaded[t](this);for(let t=0;t<r.callbacksRuntimeSceneLoaded.length;++t)r.callbacksRuntimeSceneLoaded[t](this);e.stopSoundsOnStartup&&this._runtimeGame&&this._runtimeGame.getSoundManager().clearAll(),this._isLoaded=!0,this._timeManager.reset()}isObjectRegistered(e){return this._objects.containsKey(e)&&this._instances.containsKey(e)&&this._objectsCtor.containsKey(e)}registerObject(e){this._objects.put(e.name,e),this._instances.put(e.name,[]);const s=r.getObjectConstructor(e.type);this._objectsCtor.put(e.name,s),s.supportsReinitialization&&this._instancesCache.put(e.name,[])}updateObject(e){this.isObjectRegistered(e.name)||h.warn("Tried to call updateObject for an object that was not registered ("+e.name+"). Call registerObject first."),this._objects.put(e.name,e)}unregisterObject(e){const s=this._instances.get(e);if(s){const t=s.slice();for(let i=0;i<t.length;i++)this.markObjectForDeletion(t[i]);this._cacheOrClearRemovedInstances()}this._objects.remove(e),this._instances.remove(e),this._instancesCache.remove(e),this._objectsCtor.remove(e)}onPause(){this._constructListOfAllInstances();for(let e=0,s=this._allInstancesList.length;e<s;++e)this._allInstancesList[e].onScenePaused(this);for(let e=0;e<r.callbacksRuntimeScenePaused.length;++e)r.callbacksRuntimeScenePaused[e](this)}onResume(){this._isJustResumed=!0,this._constructListOfAllInstances();for(let e=0,s=this._allInstancesList.length;e<s;++e)this._allInstancesList[e].onSceneResumed(this);for(let e=0;e<r.callbacksRuntimeSceneResumed.length;++e)r.callbacksRuntimeSceneResumed[e](this)}unloadScene(){if(!!this._isLoaded){this._profiler&&this.stopProfiler();for(let e=0;e<r.callbacksRuntimeSceneUnloading.length;++e)r.callbacksRuntimeSceneUnloading[e](this);this._constructListOfAllInstances();for(let e=0,s=this._allInstancesList.length;e<s;++e)this._allInstancesList[e].onDestroyFromScene(this);this._renderer&&this._renderer.onSceneUnloaded();for(let e=0;e<r.callbacksRuntimeSceneUnloaded.length;++e)r.callbacksRuntimeSceneUnloaded[e](this);this._layers=new Hashtable,this._variables=new r.VariablesContainer,this._initialBehaviorSharedData=new Hashtable,this._objects=new Hashtable,this._instances=new Hashtable,this._instancesCache=new Hashtable,this._eventsFunction=null,this._objectsCtor=new Hashtable,this._allInstancesList=[],this._instancesRemoved=[],this._lastId=0,this._onceTriggers=null,this._isLoaded=!1,this.onGameResolutionResized()}}createObjectsFrom(e,s,t,i){for(let n=0,a=e.length;n<a;++n){const o=e[n],_=o.name,l=this.createObject(_);l!==null&&(i&&(l.persistentUuid=o.persistentUuid||null),l.setPosition(o.x+s,o.y+t),l.setZOrder(o.zOrder),l.setAngle(o.angle),l.setLayer(o.layer),l.getVariables().initFrom(o.initialVariables,!0),l.extraInitializationFromInitialInstance(o))}}_setLayerDefaultZOrders(){if(this._runtimeGame.getGameData().properties.useDeprecatedZeroAsDefaultZOrder)return;const e={},s=this.getAdhocListOfAllInstances();for(let t=0,i=s.length;t<i;++t){const n=s[t];let a=n.getLayer();const o=n.getZOrder();(e[a]===void 0||e[a]<o)&&(e[a]=o)}for(let t in e)this.getLayer(t).setDefaultZOrder(e[t]+1)}setEventsGeneratedCodeFunction(e){const s=r[e.mangledName+"Code"];s&&s.func?this._eventsFunction=s.func:(d.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=c.CONTINUE,this._timeManager.update(e,this._runtimeGame.getMinimalFramerate()),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 s=0;s<r.callbacksRuntimeScenePreEvents.length;++s)r.callbacksRuntimeScenePreEvents[s](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._updateObjectsPostEvents(),this._profiler&&this._profiler.end("objects (post-events)"),this._profiler&&this._profiler.begin("callbacks and extensions (post-events)");for(let s=0;s<r.callbacksRuntimeScenePostEvents.length;++s)r.callbacksRuntimeScenePostEvents[s](this);return this._profiler&&this._profiler.end("callbacks and extensions (post-events)"),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.getRenderer().renderDebugDraw(this._allInstancesList,this._debugDrawShowHiddenInstances,this._debugDrawShowPointsNames,this._debugDrawShowCustomPoints),this._isJustResumed=!1,this.render(),this._profiler&&this._profiler.end("render"),this._profiler&&this._profiler.endFrame(),!!this.getRequestedChange()}render(){this._renderer.render()}_updateLayersCameraCoordinates(e){this._layersCameraCoordinates=this._layersCameraCoordinates||{};for(const s in this._layers.items)if(this._layers.items.hasOwnProperty(s)){const t=this._layers.items[s];this._layersCameraCoordinates[s]=this._layersCameraCoordinates[s]||[0,0,0,0],this._layersCameraCoordinates[s][0]=t.getCameraX()-t.getCameraWidth()/2*e,this._layersCameraCoordinates[s][1]=t.getCameraY()-t.getCameraHeight()/2*e,this._layersCameraCoordinates[s][2]=t.getCameraX()+t.getCameraWidth()/2*e,this._layersCameraCoordinates[s][3]=t.getCameraY()+t.getCameraHeight()/2*e}}_updateLayersPreRender(){for(const e in this._layers.items)this._layers.items.hasOwnProperty(e)&&this._layers.items[e].updatePreRender(this)}_updateObjectsPreRender(){if(this._timeManager.isFirstFrame()){this._constructListOfAllInstances();for(let e=0,s=this._allInstancesList.length;e<s;++e){const t=this._allInstancesList[e],i=t.getRendererObject();i&&(i.visible=!t.isHidden(),i.visible&&this._runtimeGame.getEffectsManager().updatePreRender(t.getRendererEffects(),t)),t.updatePreRender(this)}return}else{this._updateLayersCameraCoordinates(2),this._constructListOfAllInstances();for(let e=0,s=this._allInstancesList.length;e<s;++e){const t=this._allInstancesList[e],i=t.getRendererObject();if(i){if(t.isHidden())i.visible=!1;else{const n=this._layersCameraCoordinates[t.getLayer()];if(!n)continue;const a=t.getVisibilityAABB();i.visible=!a||!(a.min[0]>n[2]||a.min[1]>n[3]||a.max[0]<n[0]||a.max[1]<n[1])}i.visible&&(this._runtimeGame.getEffectsManager().updatePreRender(t.getRendererEffects(),t),t.updatePreRender(this))}else t.updatePreRender(this)}}}_cacheOrClearRemovedInstances(){for(let e=0,s=this._instancesRemoved.length;e<s;++e){const t=this._instancesCache.get(this._instancesRemoved[e].getName());t&&t.length<128&&t.push(this._instancesRemoved[e])}this._instancesRemoved.length=0}_constructListOfAllInstances(){let e=0;for(const s in this._instances.items)if(this._instances.items.hasOwnProperty(s)){const t=this._instances.items[s],i=e;e+=t.length;for(let n=0,a=t.length;n<a;++n)i+n<this._allInstancesList.length?this._allInstancesList[i+n]=t[n]:this._allInstancesList.push(t[n])}this._allInstancesList.length=e}_updateObjectsPreEvents(){this._constructListOfAllInstances();for(let e=0,s=this._allInstancesList.length;e<s;++e){const t=this._allInstancesList[e],i=t.getElapsedTime(this);if(t.hasNoForces())t.update(this);else{const n=t.getAverageForce(),a=i/1e3;t.setX(t.getX()+n.getX()*a),t.setY(t.getY()+n.getY()*a),t.update(this),t.updateForces(a)}t.updateTimers(i),this._allInstancesList[e].stepBehaviorsPreEvents(this)}this._cacheOrClearRemovedInstances()}_updateObjectsPostEvents(){this._cacheOrClearRemovedInstances(),this._constructListOfAllInstances();for(let e=0,s=this._allInstancesList.length;e<s;++e)this._allInstancesList[e].stepBehaviorsPostEvents(this);this._cacheOrClearRemovedInstances()}setBackgroundColor(e,s,t){this._backgroundColor=parseInt(r.rgbToHex(e,s,t),16)}getBackgroundColor(){return this._backgroundColor}getName(){return this._name}updateObjectsForces(){for(const e in this._instances.items)if(this._instances.items.hasOwnProperty(e)){const s=this._instances.items[e];for(let t=0,i=s.length;t<i;++t){const n=s[t];if(!n.hasNoForces()){const a=n.getAverageForce(),o=n.getElapsedTime(this)/1e3;n.setX(n.getX()+a.getX()*o),n.setY(n.getY()+a.getY()*o),n.updateForces(o)}}}}addObject(e){this._instances.containsKey(e.name)||this._instances.put(e.name,[]),this._instances.get(e.name).push(e)}getObjects(e){return this._instances.containsKey(e)||(h.info('RuntimeScene.getObjects: No instances called "'+e+'"! Adding it.'),this._instances.put(e,[])),this._instances.get(e)}createObject(e){if(!this._objectsCtor.containsKey(e)||!this._objects.containsKey(e))return null;const s=this._instancesCache.get(e),t=this._objectsCtor.get(e);let i;return!s||s.length===0?i=new t(this,this._objects.get(e)):(i=s.pop(),i.reinitialize(this._objects.get(e))),this.addObject(i),i}markObjectForDeletion(e){if(this._instancesRemoved.indexOf(e)===-1&&this._instancesRemoved.push(e),this._instances.containsKey(e.getName())){const s=e.id,t=this._instances.get(e.getName());for(let i=0,n=t.length;i<n;++i)if(t[i].id==s){t.splice(i,1);break}}e.onDestroyFromScene(this);for(let s=0;s<r.callbacksObjectDeletedFromScene.length;++s)r.callbacksObjectDeletedFromScene[s](this,e)}createNewUniqueId(){return this._lastId++,this._lastId}getRenderer(){return this._renderer}getGame(){return this._runtimeGame}getVariables(){return this._variables}getInitialSharedDataForBehavior(e){const s=this._initialBehaviorSharedData.get(e);return s||(h.error("Can't find shared data for behavior with name: "+e),null)}setInitialSharedDataForBehavior(e,s){this._initialBehaviorSharedData.put(e,s)}getLayer(e){return this._layers.containsKey(e)?this._layers.get(e):this._layers.get("")}hasLayer(e){return this._layers.containsKey(e)}addLayer(e){this._layers.put(e.name,new r.Layer(e,this))}removeLayer(e){const s=this.getAdhocListOfAllInstances();for(let t=0;t<s.length;++t){const i=s[t];i.getLayer()===e&&i.setLayer("")}this._layers.remove(e)}setLayerIndex(e,s){const t=this._layers.get(e);!t||this._renderer.setLayerIndex(t,s)}getAllLayerNames(e){this._layers.keys(e)}getTimeManager(){return this._timeManager}getSoundManager(){return this._runtimeGame.getSoundManager()}getRequestedChange(){return this._requestedChange}getRequestedScene(){return this._requestedScene}requestChange(e,s){this._requestedChange=e,s&&(this._requestedScene=s)}getProfiler(){return this._profiler}startProfiler(e){this._profiler||(this._profiler=new r.Profiler,this._onProfilerStopped=e)}stopProfiler(){if(!this._profiler)return;const e=this._profiler,s=this._onProfilerStopped;this._profiler=null,this._onProfilerStopped=null,s&&s(e)}getOnceTriggers(){return this._onceTriggers}getAdhocListOfAllInstances(){return this._constructListOfAllInstances(),this._allInstancesList}sceneJustResumed(){return this._isJustResumed}}r.RuntimeScene=u;let c;(function(a){a[a.CONTINUE=0]="CONTINUE",a[a.PUSH_SCENE=1]="PUSH_SCENE",a[a.POP_SCENE=2]="POP_SCENE",a[a.REPLACE_SCENE=3]="REPLACE_SCENE",a[a.CLEAR_SCENES=4]="CLEAR_SCENES",a[a.STOP_GAME=5]="STOP_GAME"})(c=r.SceneChangeRequest||(r.SceneChangeRequest={}))})(gdjs||(gdjs={}));
//# sourceMappingURL=runtimescene.js.map