From 837bb3d6f9d7703a4d54273d445d249653e0dad4 Mon Sep 17 00:00:00 2001 From: KoalaTrapLord <265133236+KoalaTrapLord@users.noreply.github.com> Date: Sat, 28 Mar 2026 19:54:56 -0400 Subject: [PATCH] feat(#139): add LifeTrackerWidget, CommanderDamageRow, PoisonCounter --- .../UI/Battleground/CommanderDamageRow.cs | 69 ++++++++++++++++ .../UI/Battleground/LifeTrackerWidget.cs | 78 +++++++++++++++++++ .../Scripts/UI/Battleground/PoisonCounter.cs | 49 ++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 unity-client/Assets/Scripts/UI/Battleground/CommanderDamageRow.cs create mode 100644 unity-client/Assets/Scripts/UI/Battleground/LifeTrackerWidget.cs create mode 100644 unity-client/Assets/Scripts/UI/Battleground/PoisonCounter.cs diff --git a/unity-client/Assets/Scripts/UI/Battleground/CommanderDamageRow.cs b/unity-client/Assets/Scripts/UI/Battleground/CommanderDamageRow.cs new file mode 100644 index 0000000..f3dab31 --- /dev/null +++ b/unity-client/Assets/Scripts/UI/Battleground/CommanderDamageRow.cs @@ -0,0 +1,69 @@ +using UnityEngine; +using TMPro; + +namespace CommanderAILab.UI +{ + /// + /// Displays cumulative commander damage received from each opposing seat. + /// Triggers elimination when any source reaches 21. + /// Attach one to each seat panel; set SeatIndex in inspector. + /// + public class CommanderDamageRow : MonoBehaviour + { + /// The seat this row belongs to (receives damage). + [SerializeField] private int seatIndex; + + /// + /// Labels for damage received from each seat. + /// fromSeatLabels[i] corresponds to global seat i. + /// The label for this seat's own index will be hidden (can't deal commander damage to yourself). + /// + [SerializeField] private TMP_Text[] fromSeatLabels; // length 4 + + private readonly int[] _damage = new int[4]; + + private void Start() + { + // Hide this seat's own label + if (fromSeatLabels != null && seatIndex < fromSeatLabels.Length + && fromSeatLabels[seatIndex] != null) + fromSeatLabels[seatIndex].gameObject.SetActive(false); + + RefreshAllLabels(); + } + + public void AddDamage(int fromSeat, int amount) + { + if (fromSeat < 0 || fromSeat >= 4 || fromSeat == seatIndex) return; + _damage[fromSeat] = Mathf.Max(0, _damage[fromSeat] + amount); + RefreshLabel(fromSeat); + + if (_damage[fromSeat] >= 21) + GameStateManager.Instance?.EliminatePlayer(seatIndex, "commander_damage"); + } + + public void SetDamage(int fromSeat, int value) + { + if (fromSeat < 0 || fromSeat >= 4 || fromSeat == seatIndex) return; + _damage[fromSeat] = Mathf.Max(0, value); + RefreshLabel(fromSeat); + } + + public int GetDamage(int fromSeat) => + (fromSeat >= 0 && fromSeat < 4) ? _damage[fromSeat] : 0; + + private void RefreshLabel(int fromSeat) + { + if (fromSeatLabels == null || fromSeat >= fromSeatLabels.Length) return; + var lbl = fromSeatLabels[fromSeat]; + if (lbl == null) return; + lbl.text = _damage[fromSeat] > 0 ? _damage[fromSeat].ToString() : "0"; + lbl.color = _damage[fromSeat] >= 21 ? Color.red : Color.white; + } + + private void RefreshAllLabels() + { + for (int i = 0; i < 4; i++) RefreshLabel(i); + } + } +} diff --git a/unity-client/Assets/Scripts/UI/Battleground/LifeTrackerWidget.cs b/unity-client/Assets/Scripts/UI/Battleground/LifeTrackerWidget.cs new file mode 100644 index 0000000..d77bbef --- /dev/null +++ b/unity-client/Assets/Scripts/UI/Battleground/LifeTrackerWidget.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using TMPro; + +namespace CommanderAILab.UI +{ + /// + /// Per-seat life total widget with +/- buttons, danger flash, and history log. + /// Calls GameStateManager.EliminatePlayer when life reaches 0. + /// + public class LifeTrackerWidget : MonoBehaviour + { + [Header("References")] + [SerializeField] private TMP_Text lifeLabel; + [SerializeField] private Button plusOneBtn; + [SerializeField] private Button minusOneBtn; + [SerializeField] private Button plusFiveBtn; + [SerializeField] private Button minusFiveBtn; + [SerializeField] private GameObject dangerFlash; // red glow overlay, shown at <=5 life + + [Header("Config")] + [SerializeField] private int startingLife = 40; + public int SeatIndex { get; set; } + + private int _life; + private readonly List _history = new(); + + // ── Lifecycle ─────────────────────────────────────────── + private void Start() + { + SetLife(startingLife); + plusOneBtn .onClick.AddListener(() => AdjustLife(+1, "manual")); + minusOneBtn.onClick.AddListener(() => AdjustLife(-1, "manual")); + plusFiveBtn .onClick.AddListener(() => AdjustLife(+5, "manual")); + minusFiveBtn.onClick.AddListener(() => AdjustLife(-5, "manual")); + } + + // ── Public API ───────────────────────────────────────── + public int Life => _life; + + public void AdjustLife(int delta, string source) + { + _life = Mathf.Max(0, _life + delta); + RefreshDisplay(); + + int turn = GameStateManager.Instance != null ? GameStateManager.Instance.TurnNumber : 0; + _history.Add(new LifeHistoryEntry { delta = delta, source = source, turn = turn }); + + if (_life == 0) + GameStateManager.Instance?.EliminatePlayer(SeatIndex, "life"); + } + + public void SetLife(int value) + { + _life = Mathf.Max(0, value); + RefreshDisplay(); + } + + public List GetHistory() => _history; + + // ── Internal ─────────────────────────────────────────── + private void RefreshDisplay() + { + if (lifeLabel) lifeLabel.text = _life.ToString(); + if (dangerFlash) dangerFlash.SetActive(_life <= 5 && _life > 0); + } + + [Serializable] + public class LifeHistoryEntry + { + public int delta; + public string source; + public int turn; + } + } +} diff --git a/unity-client/Assets/Scripts/UI/Battleground/PoisonCounter.cs b/unity-client/Assets/Scripts/UI/Battleground/PoisonCounter.cs new file mode 100644 index 0000000..d505dc9 --- /dev/null +++ b/unity-client/Assets/Scripts/UI/Battleground/PoisonCounter.cs @@ -0,0 +1,49 @@ +using UnityEngine; +using UnityEngine.UI; +using TMPro; + +namespace CommanderAILab.UI +{ + /// + /// Tracks poison counters for a seat (0-10). + /// Activates pip toggles and triggers elimination at 10. + /// + public class PoisonCounter : MonoBehaviour + { + [SerializeField] private int seatIndex; + [SerializeField] private Toggle[] pips; // 10 Toggle GameObjects + [SerializeField] private TMP_Text countLabel; + [SerializeField] private Button addPoisonBtn; + [SerializeField] private Button removePoisonBtn; + + private int _count = 0; + + private void Start() + { + addPoisonBtn?.onClick.AddListener(() => AddPoison(1)); + removePoisonBtn?.onClick.AddListener(() => AddPoison(-1)); + Refresh(); + } + + public void AddPoison(int delta) + { + _count = Mathf.Clamp(_count + delta, 0, 10); + Refresh(); + if (_count >= 10) + GameStateManager.Instance?.EliminatePlayer(seatIndex, "poison"); + } + + public void SetPoison(int value) + { + _count = Mathf.Clamp(value, 0, 10); + Refresh(); + } + + private void Refresh() + { + if (countLabel) countLabel.text = _count.ToString(); + for (int i = 0; i < pips.Length; i++) + if (pips[i] != null) pips[i].isOn = i < _count; + } + } +}