From 5a5c379f1f6e73e09263e613d6e81ffaac559cf5 Mon Sep 17 00:00:00 2001 From: Michal Pikulski Date: Fri, 19 Dec 2025 16:26:46 +0100 Subject: [PATCH] Add score to FortFight, clean up the resets --- Assets/Scenes/MiniGames/FortFight.unity | 8 +- .../FortFight/Core/FortFightGameManager.cs | 125 +++++++++++++++++- .../Minigames/FortFight/UI/GameOverUI.cs | 11 +- 3 files changed, 131 insertions(+), 13 deletions(-) diff --git a/Assets/Scenes/MiniGames/FortFight.unity b/Assets/Scenes/MiniGames/FortFight.unity index 36be810c..7ba2d7b0 100644 --- a/Assets/Scenes/MiniGames/FortFight.unity +++ b/Assets/Scenes/MiniGames/FortFight.unity @@ -5529,7 +5529,7 @@ GameObject: - component: {fileID: 1156219949} m_Layer: 5 m_Name: MainCanvas - m_TagString: Untagged + m_TagString: MainCanvas m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -5613,7 +5613,7 @@ RectTransform: - {fileID: 1585033672} - {fileID: 805585728} m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: -0.079} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} m_AnchoredPosition: {x: 0, y: 0} @@ -8511,7 +8511,7 @@ Camera: far clip plane: 1000 field of view: 60 orthographic: 1 - orthographic size: 20 + orthographic size: 35 m_Depth: -1 m_CullingMask: serializedVersion: 2 @@ -8536,7 +8536,7 @@ Transform: m_GameObject: {fileID: 1810521056} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalPosition: {x: 0, y: 13.5999975, z: -10} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] diff --git a/Assets/Scripts/Minigames/FortFight/Core/FortFightGameManager.cs b/Assets/Scripts/Minigames/FortFight/Core/FortFightGameManager.cs index e880e906..31c49a03 100644 --- a/Assets/Scripts/Minigames/FortFight/Core/FortFightGameManager.cs +++ b/Assets/Scripts/Minigames/FortFight/Core/FortFightGameManager.cs @@ -62,6 +62,7 @@ namespace Minigames.FortFight.Core private PlayerData playerTwo; private bool isGameActive = false; private float gameStartTime = 0f; + private float completionTime = 0f; // Store completion time when game ends public FortFightGameMode CurrentGameMode => currentGameMode; public bool IsGameActive => isGameActive; @@ -73,6 +74,11 @@ namespace Minigames.FortFight.Core { get { + if (!isGameActive && completionTime > 0f) + { + // Game ended, return the stored completion time + return completionTime; + } if (!isGameActive) return 0f; return Time.time - gameStartTime; } @@ -333,6 +339,8 @@ namespace Minigames.FortFight.Core return; } + // Store completion time before setting isGameActive to false + completionTime = Time.time - gameStartTime; isGameActive = false; // Stop turn manager @@ -341,12 +349,123 @@ namespace Minigames.FortFight.Core TurnManager.Instance.SetGameOver(); } - // Manage UI transitions - ShowGameOver(); + // Start the end game sequence (show boosters, then game over screen) + StartCoroutine(EndGameSequence()); OnGameEnded?.Invoke(); - Logging.Debug("[FortFightGameManager] Game ended"); + Logging.Debug($"[FortFightGameManager] Game ended - Duration: {completionTime:F2}s"); + } + + /// + /// End game sequence: award boosters, wait for completion, then show game over screen + /// + private System.Collections.IEnumerator EndGameSequence() + { + // Determine if player won + bool playerWon = DidPlayerWin(); + + // Calculate booster reward based on completion time and win/loss + int boosterReward = CalculateBoosterReward(completionTime, playerWon); + + string resultText = playerWon ? "WON" : "LOST"; + Logging.Debug($"[FortFightGameManager] Game completed in {completionTime:F2}s - Player {resultText}, awarding {boosterReward} booster pack(s)"); + + // Show booster pack reward UI and wait for completion + if (boosterReward > 0) + { + Logging.Debug("[FortFightGameManager] Starting booster giver sequence via GameManager"); + + var task = GameManager.Instance.GiveBoosterPacksAsync(boosterReward); + + // Wait for the task to complete + while (!task.IsCompleted) + { + yield return null; + } + + // Check for exceptions + if (task.IsFaulted) + { + Logging.Warning($"[FortFightGameManager] Booster pack reward failed: {task.Exception?.GetBaseException().Message}"); + } + else + { + Logging.Debug("[FortFightGameManager] Booster giver sequence finished, proceeding to game over screen"); + } + } + else + { + Logging.Debug("[FortFightGameManager] No boosters to award, proceeding directly to game over screen"); + } + + // Now show game over screen + ShowGameOver(); + } + + /// + /// Determine if the player won the game + /// + private bool DidPlayerWin() + { + var fortManager = FortManager.Instance; + if (fortManager == null) + { + Logging.Warning("[FortFightGameManager] Cannot determine winner - FortManager not found"); + return false; + } + + bool playerDefeated = fortManager.PlayerFort?.IsDefeated ?? false; + bool enemyDefeated = fortManager.EnemyFort?.IsDefeated ?? false; + + // Player wins if enemy fort is defeated and player fort is not + return enemyDefeated && !playerDefeated; + } + + /// + /// Calculate booster pack reward based on completion time and game result. + /// + /// If player WON: + /// < 1min: 5 Booster Packs + /// < 3min: 4 Booster Packs + /// < 5min: 3 Booster Packs + /// < 7min: 2 Booster Packs + /// >= 7min: 1 Booster Pack + /// + /// If player LOST: + /// 1 Booster Pack (participation reward) + /// + private int CalculateBoosterReward(float timeInSeconds, bool playerWon) + { + // If player lost, give participation reward + if (!playerWon) + { + return 1; + } + + // If player won, calculate reward based on completion time + float minutes = timeInSeconds / 60f; + + if (minutes < 1f) + { + return 5; + } + else if (minutes < 3f) + { + return 4; + } + else if (minutes < 5f) + { + return 3; + } + else if (minutes < 7f) + { + return 2; + } + else + { + return 1; + } } /// diff --git a/Assets/Scripts/Minigames/FortFight/UI/GameOverUI.cs b/Assets/Scripts/Minigames/FortFight/UI/GameOverUI.cs index 2f4b3087..278198f5 100644 --- a/Assets/Scripts/Minigames/FortFight/UI/GameOverUI.cs +++ b/Assets/Scripts/Minigames/FortFight/UI/GameOverUI.cs @@ -1,4 +1,5 @@ -using Core; +using System; +using Core; using Core.Lifecycle; using TMPro; using UnityEngine; @@ -220,12 +221,10 @@ namespace Minigames.FortFight.UI /// /// Restart the game by reloading the current scene /// - private void RestartGame() + private async void RestartGame() { - // Use Unity's SceneManager to reload current scene - string currentScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; - Logging.Debug($"[GameOverUI] Reloading scene: {currentScene}"); - UnityEngine.SceneManagement.SceneManager.LoadScene(currentScene); + var progress = new Progress(p => Logging.Debug($"Loading progress: {p * 100:F0}%")); + await SceneManagerService.Instance.ReloadCurrentScene(progress); } #endregion