Slash-The-Hordes/assets/Scripts/Menu/GameRunner.ts

40 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2022-12-07 09:47:46 +00:00
import { director } from "cc";
import { AppRoot } from "../AppRoot/AppRoot";
import { UserData } from "../Game/Data/UserData";
2022-12-13 17:25:19 +00:00
import { Game, GameResult } from "../Game/Game";
2022-12-07 09:47:46 +00:00
import { delay } from "../Services/Utils/AsyncUtils";
export class GameRunner {
private static instance: GameRunner = new GameRunner();
2022-12-12 08:15:27 +00:00
private isRunning = false;
2022-12-07 09:47:46 +00:00
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static get Instance(): GameRunner {
return this.instance;
}
2022-12-12 08:15:27 +00:00
public get IsRunning(): boolean {
return this.isRunning;
}
2022-12-07 09:47:46 +00:00
public async playGame(): Promise<void> {
2022-12-12 08:15:27 +00:00
this.isRunning = true;
2022-12-07 09:47:46 +00:00
director.loadScene("Game");
2022-12-21 13:08:49 +00:00
const userData: UserData = AppRoot.Instance.LiveUserData;
2022-12-07 09:47:46 +00:00
while (Game.Instance == null) await delay(10);
2023-01-05 08:53:16 +00:00
const result: GameResult = await Game.Instance.play(userData, AppRoot.Instance.Settings, AppRoot.Instance.TranslationData);
2022-12-13 17:25:19 +00:00
userData.game.goldCoins += result.goldCoins;
if (userData.game.highscore < result.score) {
userData.game.highscore = result.score;
}
2022-12-21 13:08:49 +00:00
AppRoot.Instance.saveUserData();
2022-12-19 14:50:24 +00:00
director.loadScene("Menu");
2022-12-12 08:15:27 +00:00
this.isRunning = false;
2022-12-07 09:47:46 +00:00
}
}