From 9eab8522a2d032abe7d4ab00a6e3e09e32690a37 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 13 Dec 2022 18:25:19 +0100 Subject: [PATCH] Linking highscore to game results --- assets/Scripts/Game/Data/UserData.ts | 1 + assets/Scripts/Game/Game.ts | 9 +++++++-- assets/Scripts/Menu/GameRunner.ts | 10 +++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/assets/Scripts/Game/Data/UserData.ts b/assets/Scripts/Game/Data/UserData.ts index 93ca860..fac3a67 100644 --- a/assets/Scripts/Game/Data/UserData.ts +++ b/assets/Scripts/Game/Data/UserData.ts @@ -7,6 +7,7 @@ export class UserData { export class GameData { public goldCoins = 0; public metaUpgrades = new MetaUpgradesData(); + public highscore = 0; } export class MetaUpgradesData { diff --git a/assets/Scripts/Game/Game.ts b/assets/Scripts/Game/Game.ts index cee5992..3224abf 100644 --- a/assets/Scripts/Game/Game.ts +++ b/assets/Scripts/Game/Game.ts @@ -59,7 +59,7 @@ export class Game extends Component { this.gamePauser.pause(); } - public async playGame(userData: UserData): Promise { + public async playGame(userData: UserData): Promise { const translationData = this.translationAsset.json; const settings = this.settingsAsset.json; const metaUpgrades = new MetaUpgrades(userData.game.metaUpgrades, settings.metaUpgrades); @@ -123,7 +123,7 @@ export class Game extends Component { await delay(1000000); this.gamePauser.pause(); Game.instance = null; - return 1; + return { goldCoins: 1, score: Math.floor(this.timeAlive) }; } public update(deltaTime: number): void { @@ -159,3 +159,8 @@ export class Game extends Component { return playerData; } } + +export class GameResult { + public goldCoins = 0; + public score = 0; +} diff --git a/assets/Scripts/Menu/GameRunner.ts b/assets/Scripts/Menu/GameRunner.ts index f469de1..883aa95 100644 --- a/assets/Scripts/Menu/GameRunner.ts +++ b/assets/Scripts/Menu/GameRunner.ts @@ -1,7 +1,7 @@ import { director } from "cc"; import { AppRoot } from "../AppRoot/AppRoot"; import { UserData } from "../Game/Data/UserData"; -import { Game } from "../Game/Game"; +import { Game, GameResult } from "../Game/Game"; import { delay } from "../Services/Utils/AsyncUtils"; export class GameRunner { @@ -25,8 +25,12 @@ export class GameRunner { director.loadScene("Game"); const userData: UserData = AppRoot.Instance.SaveSystem.load(); while (Game.Instance == null) await delay(10); - const result: number = await Game.Instance.playGame(); - userData.game.goldCoins += result; + const result: GameResult = await Game.Instance.playGame(userData); + userData.game.goldCoins += result.goldCoins; + + if (userData.game.highscore < result.score) { + userData.game.highscore = result.score; + } AppRoot.Instance.SaveSystem.save(userData); console.log("Gold coins: " + result);