191 lines
8.1 KiB
C#
191 lines
8.1 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
|
|
public class ScoreManager : MonoBehaviour
|
|
{
|
|
public static ScoreManager Instance { get; private set; }
|
|
|
|
public int totalScore = 0;
|
|
|
|
// per-track pm score sums (red, green, yellow, purple, blue)
|
|
public int red_pmScore_sum = 0;
|
|
public int green_pmScore_sum = 0;
|
|
public int yellow_pmScore_sum = 0;
|
|
public int purple_pmScore_sum = 0;
|
|
public int blue_pmScore_sum = 0;
|
|
|
|
// aggregate of the five track pm sums
|
|
public int allSum_pmScore = 0;
|
|
|
|
private int[] pmScoreSums = new int[5];
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null) Instance = this;
|
|
else Destroy(gameObject);
|
|
|
|
for (int i = 0; i < pmScoreSums.Length; i++) pmScoreSums[i] = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a per-note pm score delta to the specified track (0..4) and update named fields + aggregate.
|
|
/// </summary>
|
|
public void AddPmScoreForTrack(int trackIndex, int delta)
|
|
{
|
|
if (trackIndex < 0 || trackIndex >= pmScoreSums.Length) return;
|
|
if (delta == 0) return;
|
|
pmScoreSums[trackIndex] += delta;
|
|
// clamp at int.MaxValue-1 to avoid overflow
|
|
if (pmScoreSums[trackIndex] < 0) pmScoreSums[trackIndex] = 0;
|
|
if (pmScoreSums[trackIndex] > int.MaxValue - 1) pmScoreSums[trackIndex] = int.MaxValue - 1;
|
|
|
|
// update named fields for easy access
|
|
red_pmScore_sum = pmScoreSums[0];
|
|
green_pmScore_sum = pmScoreSums[1];
|
|
yellow_pmScore_sum = pmScoreSums[2];
|
|
purple_pmScore_sum = pmScoreSums[3];
|
|
blue_pmScore_sum = pmScoreSums[4];
|
|
|
|
// recompute aggregate
|
|
long agg = 0;
|
|
for (int i = 0; i < pmScoreSums.Length; i++) agg += pmScoreSums[i];
|
|
allSum_pmScore = (int)Mathf.Min((float)agg, (float)int.MaxValue - 1f);
|
|
|
|
Debug.Log($"[ScoreManager] Added {delta} to track {trackIndex} pm sum. New per-track sums: R{red_pmScore_sum} G{green_pmScore_sum} Y{yellow_pmScore_sum} P{purple_pmScore_sum} B{blue_pmScore_sum} -> allSum={allSum_pmScore}");
|
|
|
|
// Update UI in teamUIController if available (per-track pm sums + aggregate)
|
|
var ui = teamUIController.Instance;
|
|
if (ui != null)
|
|
{
|
|
// Legacy Text fields on teamUIController
|
|
try
|
|
{
|
|
if (ui.red_pmScore_sum != null) ui.red_pmScore_sum.text = red_pmScore_sum.ToString();
|
|
if (ui.green_pmScore_sum != null) ui.green_pmScore_sum.text = green_pmScore_sum.ToString();
|
|
if (ui.yellow_pmScore_sum != null) ui.yellow_pmScore_sum.text = yellow_pmScore_sum.ToString();
|
|
if (ui.purple_pmScore_sum != null) ui.purple_pmScore_sum.text = purple_pmScore_sum.ToString();
|
|
if (ui.blue_pmScore_sum != null) ui.blue_pmScore_sum.text = blue_pmScore_sum.ToString();
|
|
if (ui.allSum_pmScore != null) ui.allSum_pmScore.text = allSum_pmScore.ToString();
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogWarning($"[ScoreManager] Failed to write pm sums to teamUIController fields: {ex}");
|
|
}
|
|
}
|
|
|
|
// Optionally update any UI for pm sums here if teamUIController exposes fields (not implemented by default)
|
|
}
|
|
|
|
public void RecalculateTotal()
|
|
{
|
|
int sum = 0;
|
|
int[] currents = new int[5];
|
|
int[] maxes = new int[5];
|
|
for (int i = 1; i <= 5; i++)
|
|
{
|
|
var go = GameObject.Find($"ally_0{i}");
|
|
if (go == null) { currents[i - 1] = 0; maxes[i - 1] = 0; continue; }
|
|
var ally = go.GetComponent<AllyCombatant>();
|
|
if (ally != null)
|
|
{
|
|
currents[i - 1] = ally.currentScore;
|
|
maxes[i - 1] = ally.maxTrackScore;
|
|
}
|
|
else
|
|
{
|
|
currents[i - 1] = 0;
|
|
maxes[i - 1] = 0;
|
|
}
|
|
|
|
sum += currents[i - 1];
|
|
}
|
|
totalScore = sum;
|
|
// update UI if available
|
|
if (teamUIController.Instance != null)
|
|
{
|
|
var ui = teamUIController.Instance;
|
|
|
|
// helper lambda to set text on TMP or legacy Text under a parent fallback
|
|
void SetScoreText(TextMeshProUGUI assignedTmp, GameObject parent, int cur, int max, string slotName)
|
|
{
|
|
string value = cur + "/" + max;
|
|
if (assignedTmp != null)
|
|
{
|
|
assignedTmp.text = value;
|
|
Debug.Log($"[ScoreManager] Wrote to assigned TMP for {slotName}: '{value}' -> {assignedTmp.gameObject.name}");
|
|
return;
|
|
}
|
|
// try find TMP in parent
|
|
if (parent != null)
|
|
{
|
|
var tmp = parent.GetComponentInChildren<TextMeshProUGUI>(true);
|
|
if (tmp != null)
|
|
{
|
|
tmp.text = value;
|
|
Debug.Log($"[ScoreManager] Wrote to TMP in parent {parent.name} for {slotName}: '{value}' -> {tmp.gameObject.name}");
|
|
return;
|
|
}
|
|
var legacy = parent.GetComponentInChildren<Text>(true);
|
|
if (legacy != null)
|
|
{
|
|
legacy.text = value;
|
|
Debug.Log($"[ScoreManager] Wrote to legacy Text in parent {parent.name} for {slotName}: '{value}' -> {legacy.gameObject.name}");
|
|
return;
|
|
}
|
|
}
|
|
Debug.LogWarning($"[ScoreManager] Could not find UI to write score for {slotName}. assignedTmp is null and parent '{parent?.name}' has no TMP/Text children.");
|
|
}
|
|
|
|
// Update each slot: use the explicit TMP fields if set; otherwise try parent object fields
|
|
SetScoreText(ui.teammate01_current_scoreText, ui.objectFather_ally01, currents[0], maxes[0], "teammate01");
|
|
SetScoreText(ui.teammate02_current_scoreText, ui.objectFather_ally02, currents[1], maxes[1], "teammate02");
|
|
SetScoreText(ui.teammate03_current_scoreText, ui.objectFather_ally03, currents[2], maxes[2], "teammate03");
|
|
SetScoreText(ui.teammate04_current_scoreText, ui.objectFather_ally04, currents[3], maxes[3], "teammate04");
|
|
SetScoreText(ui.teammate05_current_scoreText, ui.objectFather_ally05, currents[4], maxes[4], "teammate05");
|
|
|
|
// update total score (try TMP first, then legacy Text)
|
|
if (ui.currentTotalScore != null)
|
|
{
|
|
var go = ui.currentTotalScore.gameObject;
|
|
if (go != null)
|
|
{
|
|
var tmpComp = go.GetComponent<TextMeshProUGUI>();
|
|
if (tmpComp != null)
|
|
{
|
|
tmpComp.text = totalScore.ToString();
|
|
Debug.Log($"[ScoreManager] Wrote total to TMP: {totalScore} -> {tmpComp.gameObject.name}");
|
|
}
|
|
else
|
|
{
|
|
var legacyComp = go.GetComponent<Text>();
|
|
if (legacyComp != null)
|
|
{
|
|
legacyComp.text = totalScore.ToString();
|
|
Debug.Log($"[ScoreManager] Wrote total to legacy Text: {totalScore} -> {legacyComp.gameObject.name}");
|
|
}
|
|
else
|
|
{
|
|
ui.currentTotalScore.text = totalScore.ToString(); // fallback
|
|
Debug.Log($"[ScoreManager] Wrote total to currentTotalScore field fallback: {totalScore}");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ui.currentTotalScore.text = totalScore.ToString();
|
|
Debug.Log($"[ScoreManager] Wrote total to currentTotalScore fallback (no go): {totalScore}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("[ScoreManager] teamUIController.currentTotalScore is null; cannot display total score.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("[ScoreManager] teamUIController.Instance is null; cannot update UI.");
|
|
}
|
|
}
|
|
}
|