-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscenestack.js
2 lines (2 loc) · 4.25 KB
/
scenestack.js
1
2
var gdjs;(function(o){const r=new o.Logger("Scene stack"),c=new o.Logger("Multiplayer - Debug");class h{constructor(t){this._stack=[];this._wasFirstSceneLoaded=!1;this._isNextLayoutLoading=!1;this._sceneStackSyncDataToApply=null;this._wasDisposed=!1;if(!t)throw"SceneStack must be constructed with a gdjs.RuntimeGame.";this._runtimeGame=t}onGameResolutionResized(){for(let t=0;t<this._stack.length;++t)this._stack[t].onGameResolutionResized()}step(t){if(this._throwIfDisposed(),this._isNextLayoutLoading||this._stack.length===0)return!1;if(this.applyUpdateFromNetworkSyncDataIfAny())return c.info("Scene stack has been updated from network sync data, skipping step."),!0;const e=this._stack[this._stack.length-1];if(e.renderAndStep(t)){const n=e.getRequestedChange();if(n===o.SceneChangeRequest.STOP_GAME)return this._runtimeGame.getRenderer().stopGame(),!0;n===o.SceneChangeRequest.POP_SCENE?this.pop():n===o.SceneChangeRequest.PUSH_SCENE?this.push(e.getRequestedScene()):n===o.SceneChangeRequest.REPLACE_SCENE?this.replace(e.getRequestedScene()):n===o.SceneChangeRequest.CLEAR_SCENES?this.replace(e.getRequestedScene(),!0):r.error("Unrecognized change in scene stack: "+n)}return!0}renderWithoutStep(){return this._throwIfDisposed(),this._stack.length===0?!1:(this._stack[this._stack.length-1].render(),!0)}pop(t=1){this._throwIfDisposed();let s=!1;for(let e=0;e<t&&!(this._stack.length<=1);++e){s=!0;const n=this._stack.pop();if(!n)return;n.unloadScene()}if(s){const e=this._stack[this._stack.length-1];e&&e.onResume()}}push(t,s){this._throwIfDisposed();const e=this._stack[this._stack.length-1];return e&&e.onPause(),this._runtimeGame.areSceneAssetsReady(t)?this._loadNewScene(t,s):(this._isNextLayoutLoading=!0,this._runtimeGame.loadSceneAssets(t).then(()=>{this._loadNewScene(t),this._isNextLayoutLoading=!1}),null)}_loadNewScene(t,s){this._throwIfDisposed();const e=new o.RuntimeScene(this._runtimeGame);if(e.loadFromScene(this._runtimeGame.getSceneAndExtensionsData(t)),this._wasFirstSceneLoaded=!0,s){const n=this._runtimeGame.getExternalLayoutData(s);n&&e.createObjectsFrom(n.instances,0,0,0,!0)}return this._stack.push(e),e}replace(t,s){if(this._throwIfDisposed(),s)for(;this._stack.length!==0;){let e=this._stack.pop();e&&e.unloadScene()}else if(this._stack.length!==0){let e=this._stack.pop();e&&e.unloadScene()}return this.push(t)}getCurrentScene(){return this._throwIfDisposed(),this._stack.length===0?null:this._stack[this._stack.length-1]}wasFirstSceneLoaded(){return this._wasFirstSceneLoaded}getAllSceneNames(){return this._throwIfDisposed(),this._stack.map(t=>t.getName())}getNetworkSyncData(t){const s=t.playerNumber,e=t.isHost;if(s!==void 0&&!e)return null;const n=[];for(let a=0;a<this._stack.length;++a){const i=this._stack[a];n.push({name:i.getName(),networkId:i.getOrCreateNetworkId()})}return n}updateFromNetworkSyncData(t){this._sceneStackSyncDataToApply=t}applyUpdateFromNetworkSyncDataIfAny(){this._throwIfDisposed();const t=this._sceneStackSyncDataToApply;let s=!1;if(!t)return s;this._sceneStackSyncDataToApply=null;for(let e=0;e<t.length;++e){const n=t[e],a=this._stack[e];if(!a){c.info(`Scene at position ${e} with name ${n.name} is missing from the stack, adding it.`);const i=this.push(n.name);i&&(i.networkId=n.networkId),s=!0;continue}if(a.getName()!==n.name){c.info(`Scene at position ${e} and name ${a.getName()} is not the same as the expected ${n.name}, replacing.`);const i=this.replace(n.name,!0);i&&(i.networkId=n.networkId),s=!0;continue}if(!a.networkId&&n.networkId&&n.name===a.getName()){c.info(`Scene at position ${e} and name ${a.getName()} has no networkId, let's assume it's the right one and reconcile it with the id ${n.networkId}.`),a.networkId=n.networkId;continue}if(a.networkId!==n.networkId){c.info(`Scene at position ${e} and name ${a.getName()} has a different networkId ${a.networkId} than the expected ${n.networkId}, replacing.`);const i=this.replace(n.name,!1);i&&(i.networkId=n.networkId),s=!0;continue}}if(this._stack.length>t.length){const e=this._stack.length-t.length;this.pop(e),s=!0}return s}dispose(){for(const t of this._stack)t.unloadScene();this._stack.length=0,this._wasDisposed=!0}_throwIfDisposed(){if(this._wasDisposed)throw"The scene stack has been disposed and should not be used anymore."}}o.SceneStack=h})(gdjs||(gdjs={}));
//# sourceMappingURL=scenestack.js.map