Add score to FortFight, clean up the resets

This commit is contained in:
Michal Pikulski
2025-12-19 16:26:46 +01:00
parent a9df9487f1
commit 5a5c379f1f
3 changed files with 131 additions and 13 deletions

View File

@@ -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: []

View File

@@ -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");
}
/// <summary>
/// End game sequence: award boosters, wait for completion, then show game over screen
/// </summary>
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();
}
/// <summary>
/// Determine if the player won the game
/// </summary>
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;
}
/// <summary>
/// Calculate booster pack reward based on completion time and game result.
///
/// If player WON:
/// &lt; 1min: 5 Booster Packs
/// &lt; 3min: 4 Booster Packs
/// &lt; 5min: 3 Booster Packs
/// &lt; 7min: 2 Booster Packs
/// &gt;= 7min: 1 Booster Pack
///
/// If player LOST:
/// 1 Booster Pack (participation reward)
/// </summary>
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;
}
}
/// <summary>

View File

@@ -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
/// <summary>
/// Restart the game by reloading the current scene
/// </summary>
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<float>(p => Logging.Debug($"Loading progress: {p * 100:F0}%"));
await SceneManagerService.Instance.ReloadCurrentScene(progress);
}
#endregion