generated from yandeu/phaser-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add loading screen using custom event emitter
- Loading branch information
Showing
21 changed files
with
229 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export class GameExtended extends Phaser.Game { | ||
settings; | ||
outEmitter; | ||
constructor(GameConfig?: Phaser.Types.Core.GameConfig, settings = {}, outEmitter = null) { | ||
super(GameConfig); | ||
this.settings = settings; | ||
this.outEmitter = outEmitter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Phaser from "phaser"; | ||
import { createNanoEvents } from "nanoevents"; | ||
import type { Emitter } from "nanoevents"; | ||
|
||
import { MainScene } from "~/scenes"; | ||
import type { Spaceship } from "~/objects"; | ||
import { GameExtended } from "."; | ||
import { gameConfig } from "."; | ||
|
||
interface Events { | ||
loading: (report: { name: string; progress: number }) => void; | ||
} | ||
|
||
export class GameManager { | ||
config: Phaser.Types.Core.GameConfig; | ||
game: GameExtended; | ||
emitter: Emitter<Events>; | ||
|
||
constructor(config) { | ||
this.config = config; | ||
this.emitter = createNanoEvents(); | ||
} | ||
|
||
on = (event, callback) => { | ||
return this.emitter.on(event, callback); | ||
}; | ||
|
||
init = async (settings) => { | ||
console.log("Booting"); | ||
const whenIsBooted = new Promise((resolve) => { | ||
this.game = new GameExtended( | ||
{ | ||
...this.config, | ||
callbacks: { postBoot: () => resolve(true) }, | ||
}, | ||
settings, | ||
this.emitter | ||
); | ||
}); | ||
await whenIsBooted; | ||
console.log("Booted"); | ||
console.log("Creating"); | ||
|
||
const whenSceneCreated = new Promise((resolve) => { | ||
const MainScene = this.game.scene.keys.MainScene as MainScene; | ||
MainScene.events.on("create", resolve); | ||
}); | ||
await whenSceneCreated; | ||
|
||
console.log("Created"); | ||
|
||
return this; | ||
}; | ||
initMultiplayer = async (settings) => {}; | ||
|
||
// TODO use this when ui modals are opened | ||
lockInput = () => { | ||
this.game.input.keyboard.enabled = false; | ||
}; | ||
unlockInput = () => { | ||
this.game.input.keyboard.enabled = false; | ||
}; | ||
|
||
get scene(): MainScene | null { | ||
const mainScene = this.game?.scene?.keys?.MainScene as MainScene; | ||
return mainScene ?? null; | ||
} | ||
get player(): Spaceship | null { | ||
return this.scene?.player ?? null; | ||
} | ||
destroy = () => { | ||
this.game.destroy(false); | ||
}; | ||
} | ||
|
||
export const gameManager = new GameManager(gameConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./gameConfig"; | ||
export * from "./gameExtended"; | ||
export * from "./gameManager"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { game, gameConfig } from "./core/game"; | ||
|
||
export { game, gameConfig }; | ||
export * from "./core"; | ||
export * from "./managers"; | ||
export * from "./objects"; | ||
export * from "./scenes"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.