12p4$5@+22
Hi there,
Apologies if a question like this has already been asked, (could not find a suitable answer)
I’m trying to restrusture my single file app.js to a more structured approach, and with the ability to separate all my different screens & functionalities, in this case:
- Start Screen
- Lobby Screen
- Game Screen
After alot of research, I could not really find many examples of how to achieve that so I came up with my own solution: https://github.com/oriongunning/orbiter-colyseus/tree/main/src
StartScene.js
export class StartScene {
public _scene: Scene;
public _button: Button;
constructor(engine) {
this.create(engine);
}
public async create(engine) {
// create scene
let scene = new Scene(engine);
// setup events
this._button.onPointerDownObservable.add(() => {
scene.detachControl(); //observables disabled
this._newState = State.LOBBY;
});
this._scene = scene;
}
}
App.js
this._engine.runRenderLoop(() => {
switch (this._state) {
///////////////////////////////////////
case State.START:
this.clearScene();
scene = new StartScene(this._engine);
this._scene = scene._scene;
this._state = State.NULL;
break;
///////////////////////////////////////
case State.LOBBY:
this.clearScene();
scene = new LobbyScene(this._engine, this._client);
this._scene = scene._scene;
this._state = State.NULL;
break;
default: break;
}
// monitor state
this._state = scene._newState;
// render when scene is ready
this._process();
});
}
private async _process(): Promise<void> {
if(this._scene){
await this._scene.whenReadyAsync();
this._engine.hideLoadingUI(); //when the scene is ready, hide loading
this._scene.render();
}
}
private clearScene() {
if(this._scene){
this._scene.dispose();
this._scene.detachControl();
this._engine.displayLoadingUI();
}
}
What I’m wondering if am I doing any obvious mistakes? or improvements?
Before I dive deeper, I’d like to know I have a good base,
Cheers,
Orion
