feat(#139): add LifeTrackerWidget, CommanderDamageRow, PoisonCounter

This commit is contained in:
KoalaTrapLord committed 2026-03-28 19:54:56 -04:00
1 parent 98b81ceab2
commit 837bb3d6f9
3 files changed
+196

No files matched your search

@@ -0,0 +1,69 @@
using UnityEngine;
using TMPro;
namespace CommanderAILab.UI
{
/// <summary>
/// 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.
/// </summary>
public class CommanderDamageRow : MonoBehaviour
{
/// <summary>The seat this row belongs to (receives damage).</summary>
[SerializeField] private int seatIndex;
/// <summary>
/// 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).
/// </summary>
[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);
}
}
}
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace CommanderAILab.UI
{
/// <summary>
/// Per-seat life total widget with +/- buttons, danger flash, and history log.
/// Calls GameStateManager.EliminatePlayer when life reaches 0.
/// </summary>
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<LifeHistoryEntry> _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<LifeHistoryEntry> 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;
}
}
}
@@ -0,0 +1,49 @@
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace CommanderAILab.UI
{
/// <summary>
/// Tracks poison counters for a seat (0-10).
/// Activates pip toggles and triggers elimination at 10.
/// </summary>
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;
}
}
}