diff --git a/assets/Scripts/Game/Game.ts b/assets/Scripts/Game/Game.ts index e622f42..f1c309c 100644 --- a/assets/Scripts/Game/Game.ts +++ b/assets/Scripts/Game/Game.ts @@ -78,12 +78,52 @@ export class Game extends Component { Game.instance = this; } - public async playGame( - userData: UserData, - settings: GameSettings, - translationData: TranslationData, - testValues?: TestValues - ): Promise { + public async play(userData: UserData, settings: GameSettings, translationData: TranslationData, testValues?: TestValues): Promise { + await this.setup(userData, settings, translationData, testValues); + + this.gamePauser.resume(); + this.blackScreen.active = false; + AppRoot.Instance.ScreenFader.playClose(); + + while (!this.gameResult.hasExitManually && this.player.Health.IsAlive) await delay(100); + + this.gamePauser.pause(); + Game.instance = null; + this.gameResult.score = this.timeAlive; + + if (!this.gameResult.hasExitManually) { + await delay(2000); + } + + return this.gameResult; + } + + public exitGame(): void { + this.gameResult.hasExitManually = true; + } + + public update(deltaTime: number): void { + if (this.gamePauser.IsPaused) return; + + this.player.gameTick(deltaTime); + this.playerCollisionSystem.gameTick(deltaTime); + this.enemyManager.gameTick(deltaTime); + this.haloProjectileLauncher.gameTick(deltaTime); + this.horizontalProjectileLauncher.gameTick(deltaTime); + this.diagonalProjectileLauncher.gameTick(deltaTime); + this.enemyAxeProjectileLauncher.gameTick(deltaTime); + this.enemyMagicOrbProjectileLauncher.gameTick(deltaTime); + this.itemAttractor.gameTick(deltaTime); + this.background.gameTick(); + + this.timeAlive += deltaTime; + this.gameUI.updateTimeAlive(this.timeAlive); + + AppRoot.Instance.MainCamera.node.setWorldPosition(this.player.node.worldPosition); + this.gameUI.node.setWorldPosition(this.player.node.worldPosition); + } + + private async setup(userData: UserData, settings: GameSettings, translationData: TranslationData, testValues: TestValues): Promise { await requireAppRootAsync(); this.gameCanvas.cameraComponent = AppRoot.Instance.MainCamera; @@ -175,45 +215,6 @@ export class Game extends Component { this.diagonalProjectileLauncher, this.haloProjectileLauncher ); - this.gamePauser.resume(); - this.blackScreen.active = false; - AppRoot.Instance.ScreenFader.playClose(); - - while (!this.gameResult.hasExitManually && this.player.Health.IsAlive) await delay(100); - this.gamePauser.pause(); - Game.instance = null; - this.gameResult.score = this.timeAlive; - - if (!this.gameResult.hasExitManually) { - await delay(2000); - } - - return this.gameResult; - } - - public exitGame(): void { - this.gameResult.hasExitManually = true; - } - - public update(deltaTime: number): void { - if (this.gamePauser.IsPaused) return; - - this.player.gameTick(deltaTime); - this.playerCollisionSystem.gameTick(deltaTime); - this.enemyManager.gameTick(deltaTime); - this.haloProjectileLauncher.gameTick(deltaTime); - this.horizontalProjectileLauncher.gameTick(deltaTime); - this.diagonalProjectileLauncher.gameTick(deltaTime); - this.enemyAxeProjectileLauncher.gameTick(deltaTime); - this.enemyMagicOrbProjectileLauncher.gameTick(deltaTime); - this.itemAttractor.gameTick(deltaTime); - this.background.gameTick(); - - this.timeAlive += deltaTime; - this.gameUI.updateTimeAlive(this.timeAlive); - - AppRoot.Instance.MainCamera.node.setWorldPosition(this.player.node.worldPosition); - this.gameUI.node.setWorldPosition(this.player.node.worldPosition); } private createPlayerData(settings: PlayerSettings, metaUpgrades: MetaUpgrades): PlayerData { diff --git a/assets/Scripts/Game/TestGameRunner.ts b/assets/Scripts/Game/TestGameRunner.ts index 8ed7560..6597378 100644 --- a/assets/Scripts/Game/TestGameRunner.ts +++ b/assets/Scripts/Game/TestGameRunner.ts @@ -36,7 +36,7 @@ export class TestGameRunner extends Component { testUserData.game.metaUpgrades.goldGathererLevel = this.goldGathererLevel; const settings = this.getTimeModifiedSettings(AppRoot.Instance.Settings); - Game.Instance.playGame(testUserData, settings, AppRoot.Instance.TranslationData, { startTime: this.startTime, startXP: this.startXP }); + Game.Instance.play(testUserData, settings, AppRoot.Instance.TranslationData, { startTime: this.startTime, startXP: this.startXP }); } private getTimeModifiedSettings(settings: GameSettings): GameSettings { diff --git a/assets/Scripts/Menu/GameRunner.ts b/assets/Scripts/Menu/GameRunner.ts index 9a96305..c0db0e3 100644 --- a/assets/Scripts/Menu/GameRunner.ts +++ b/assets/Scripts/Menu/GameRunner.ts @@ -25,7 +25,7 @@ export class GameRunner { director.loadScene("Game"); const userData: UserData = AppRoot.Instance.LiveUserData; while (Game.Instance == null) await delay(10); - const result: GameResult = await Game.Instance.playGame(userData, AppRoot.Instance.Settings, AppRoot.Instance.TranslationData); + const result: GameResult = await Game.Instance.play(userData, AppRoot.Instance.Settings, AppRoot.Instance.TranslationData); userData.game.goldCoins += result.goldCoins; if (userData.game.highscore < result.score) {