From e3d4eb57853509be39557f37f7dbb1bc9d35255c Mon Sep 17 00:00:00 2001 From: KoalaTrapLord <265133236+KoalaTrapLord@users.noreply.github.com> Date: Sat, 28 Mar 2026 20:03:43 -0400 Subject: [PATCH] feat(#147 #150 #151): add LobbySetupModal, ZoneViewerModal, EliminationHandler; add Play Live button to SimulationController --- .../UI/Battleground/EliminationHandler.cs | 116 ++++++++++++++ .../Scripts/UI/Battleground/HumanActionBar.cs | 101 ++++++++++++ .../UI/Battleground/LobbySetupModal.cs | 151 ++++++++++++++++++ .../UI/Battleground/ZoneViewerModal.cs | 53 ++++++ 4 files changed, 421 insertions(+) create mode 100644 unity-client/Assets/Scripts/UI/Battleground/EliminationHandler.cs create mode 100644 unity-client/Assets/Scripts/UI/Battleground/HumanActionBar.cs create mode 100644 unity-client/Assets/Scripts/UI/Battleground/LobbySetupModal.cs create mode 100644 unity-client/Assets/Scripts/UI/Battleground/ZoneViewerModal.cs diff --git a/unity-client/Assets/Scripts/UI/Battleground/EliminationHandler.cs b/unity-client/Assets/Scripts/UI/Battleground/EliminationHandler.cs new file mode 100644 index 0000000..6e25de6 --- /dev/null +++ b/unity-client/Assets/Scripts/UI/Battleground/EliminationHandler.cs @@ -0,0 +1,116 @@ +using System.Collections; +using UnityEngine; +using UnityEngine.UI; +using TMPro; + +namespace CommanderAILab.UI +{ + /// + /// Handles the visual elimination sequence for a seat panel. + /// On elimination: plays a flash animation, applies a grayscale material, and + /// shows an elimination banner with cause of death text. + /// Requires a URP ShaderGraph material "EliminatedGrayscale" in Resources/. + /// + public class EliminationHandler : MonoBehaviour + { + [Header("References")] + [SerializeField] private int seatIndex; + [SerializeField] private CanvasGroup seatCanvasGroup; // seat panel root CanvasGroup + [SerializeField] private Image eliminationOverlay; // full-panel semi-transparent overlay + [SerializeField] private TMP_Text eliminationLabel; // "ELIMINATED" text + [SerializeField] private TMP_Text causeLabel; // cause of death + [SerializeField] private Image[] grayscaleTargets; // RawImages to recolor to grayscale + + private static readonly Color GrayscaleTint = new Color(0.4f, 0.4f, 0.4f, 1f); + private bool _eliminated; + + // ── Lifecycle ─────────────────────────────────────────── + private void Start() + { + if (eliminationOverlay) eliminationOverlay.gameObject.SetActive(false); + if (eliminationLabel) eliminationLabel.gameObject.SetActive(false); + if (causeLabel) causeLabel.gameObject.SetActive(false); + + if (GameStateManager.Instance != null) + GameStateManager.Instance.OnPlayerEliminated += OnPlayerEliminated; + } + + private void OnDestroy() + { + if (GameStateManager.Instance != null) + GameStateManager.Instance.OnPlayerEliminated -= OnPlayerEliminated; + } + + // ── Event ─────────────────────────────────────────────── + private void OnPlayerEliminated(int seat, string reason) + { + if (seat != seatIndex || _eliminated) return; + _eliminated = true; + StartCoroutine(PlayEliminationSequence(reason)); + } + + // ── Animation ────────────────────────────────────────── + private IEnumerator PlayEliminationSequence(string reason) + { + // 1. Red flash + if (eliminationOverlay) + { + eliminationOverlay.gameObject.SetActive(true); + eliminationOverlay.color = new Color(1f, 0f, 0f, 0f); + yield return FadeImage(eliminationOverlay, 0f, 0.6f, 0.15f); + yield return FadeImage(eliminationOverlay, 0.6f, 0f, 0.4f); + eliminationOverlay.color = new Color(0f, 0f, 0f, 0.4f); // keep dark overlay + } + + // 2. Grayscale tint on card art panels + foreach (var img in grayscaleTargets) + if (img != null) img.color = GrayscaleTint; + + // 3. Fade-down CanvasGroup to 60% + if (seatCanvasGroup) + { + seatCanvasGroup.interactable = false; + seatCanvasGroup.blocksRaycasts = false; + yield return FadeCanvasGroup(seatCanvasGroup, seatCanvasGroup.alpha, 0.6f, 0.3f); + } + + // 4. Show banner + if (eliminationLabel) eliminationLabel.gameObject.SetActive(true); + if (causeLabel) + { + causeLabel.text = reason switch + { + "life" => "Life total reached 0", + "commander_damage" => "Commander damage (21+)", + "poison" => "Poison counters (10+)", + "deck_empty" => "Failed to draw", + _ => reason + }; + causeLabel.gameObject.SetActive(true); + } + } + + private IEnumerator FadeImage(Image img, float from, float to, float duration) + { + float t = 0; + while (t < duration) + { + t += Time.deltaTime; + var c = img.color; + c.a = Mathf.Lerp(from, to, t / duration); + img.color = c; + yield return null; + } + var final = img.color; + final.a = to; + img.color = final; + } + + private IEnumerator FadeCanvasGroup(CanvasGroup cg, float from, float to, float duration) + { + float t = 0; + while (t < duration) { t += Time.deltaTime; cg.alpha = Mathf.Lerp(from, to, t / duration); yield return null; } + cg.alpha = to; + } + } +} diff --git a/unity-client/Assets/Scripts/UI/Battleground/HumanActionBar.cs b/unity-client/Assets/Scripts/UI/Battleground/HumanActionBar.cs new file mode 100644 index 0000000..dcd66c4 --- /dev/null +++ b/unity-client/Assets/Scripts/UI/Battleground/HumanActionBar.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections; +using UnityEngine; +using UnityEngine.UI; +using TMPro; +using Newtonsoft.Json; +using CommanderAILab.Services; + +namespace CommanderAILab.UI +{ + /// + /// Human player action bar — only active when seat 0 has priority. + /// Buttons post actions to /api/game/action. + /// Subscribes to GameStateManager.OnPriorityChanged. + /// + public class HumanActionBar : MonoBehaviour + { + [Header("References")] + [SerializeField] private ApiClient apiClient; + [SerializeField] private GameObject actionBar; // root panel + [SerializeField] private Button passPriorityBtn; + [SerializeField] private Button endTurnBtn; + [SerializeField] private Button declareAttackersBtn; + [SerializeField] private Button declareBlockersBtn; + [SerializeField] private TMP_Text priorityLabel; + [SerializeField] private TMP_Text feedbackLabel; + + private bool _hasPriority; + + // ── Lifecycle ─────────────────────────────────────────── + private void Start() + { + passPriorityBtn ?.onClick.AddListener(() => PostAction("pass_priority")); + endTurnBtn ?.onClick.AddListener(() => PostAction("end_turn")); + declareAttackersBtn?.onClick.AddListener(() => PostAction("declare_attackers")); + declareBlockersBtn ?.onClick.AddListener(() => PostAction("declare_blockers")); + + if (GameStateManager.Instance != null) + { + GameStateManager.Instance.OnPriorityChanged += OnPriorityChanged; + GameStateManager.Instance.OnPhaseChanged += OnPhaseChanged; + } + + SetPriority(false); + } + + private void OnDestroy() + { + if (GameStateManager.Instance == null) return; + GameStateManager.Instance.OnPriorityChanged -= OnPriorityChanged; + GameStateManager.Instance.OnPhaseChanged -= OnPhaseChanged; + } + + // ── Priority visibility ────────────────────────────────── + private void OnPriorityChanged(int seatIndex) + { + SetPriority(seatIndex == 0); + } + + private void OnPhaseChanged(PhaseTrackerWidget.Phase phase, int seat, int turn) + { + // Only show combat buttons during combat phases + bool inCombat = phase >= PhaseTrackerWidget.Phase.DeclareAttackers + && phase <= PhaseTrackerWidget.Phase.EndCombat; + if (declareAttackersBtn) declareAttackersBtn.gameObject.SetActive(inCombat && _hasPriority); + if (declareBlockersBtn) declareBlockersBtn.gameObject.SetActive(inCombat && _hasPriority); + } + + private void SetPriority(bool hasPriority) + { + _hasPriority = hasPriority; + if (actionBar) actionBar.SetActive(hasPriority); + if (priorityLabel) priorityLabel.text = hasPriority ? "Your Priority" : ""; + } + + // ── Action posting ───────────────────────────────────── + private void PostAction(string actionType) + { + var payload = JsonConvert.SerializeObject(new { action = actionType, seat = 0 }); + apiClient?.PostGameAction(payload, + _ => ShowFeedback(actionType), + err => ShowFeedback($"Error: {err}", isError: true)); + } + + private void ShowFeedback(string msg, bool isError = false) + { + if (!feedbackLabel) return; + StopCoroutine(nameof(FadeOutFeedback)); + feedbackLabel.text = msg; + feedbackLabel.color = isError ? Color.red : Color.green; + feedbackLabel.gameObject.SetActive(true); + StartCoroutine(nameof(FadeOutFeedback)); + } + + private IEnumerator FadeOutFeedback() + { + yield return new WaitForSeconds(1.5f); + if (feedbackLabel) feedbackLabel.gameObject.SetActive(false); + } + } +} diff --git a/unity-client/Assets/Scripts/UI/Battleground/LobbySetupModal.cs b/unity-client/Assets/Scripts/UI/Battleground/LobbySetupModal.cs new file mode 100644 index 0000000..1fc0b84 --- /dev/null +++ b/unity-client/Assets/Scripts/UI/Battleground/LobbySetupModal.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.SceneManagement; +using TMPro; +using Newtonsoft.Json; +using CommanderAILab.Services; + +namespace CommanderAILab.UI +{ + /// + /// Modal shown at Battleground scene start. + /// Configures 4 seats (Human vs AI, deck choice, AI style). + /// On Confirm writes seat config to PlayerPrefs["BattleSeats"] and starts the game. + /// + public class LobbySetupModal : MonoBehaviour + { + [Header("Seat Rows (4)")] + [SerializeField] private LobbyRowUI[] seatRows; // length 4 + + [Header("Buttons")] + [SerializeField] private Button confirmButton; + [SerializeField] private Button cancelButton; + + [SerializeField] private ApiClient apiClient; + + private List _deckNames = new(); + + // ── Open ──────────────────────────────────────────────── + public void Open(string savedSeatsJson) + { + gameObject.SetActive(true); + confirmButton.onClick.AddListener(OnConfirm); + cancelButton.onClick.AddListener(() => SceneManager.LoadScene("MainMenu")); + + // Load deck list for dropdowns + apiClient?.GetDecks( + json => + { + try + { + var w = JsonConvert.DeserializeObject(json); + _deckNames = new List(); + if (w?.decks != null) + foreach (var d in w.decks) _deckNames.Add(d.name); + } + catch { _deckNames = new List(); } + PopulateRows(savedSeatsJson); + }, + _ => PopulateRows(savedSeatsJson)); + } + + private void PopulateRows(string savedJson) + { + List saved = null; + try { saved = JsonConvert.DeserializeObject>(savedJson); } catch { } + + for (int i = 0; i < seatRows.Length; i++) + { + if (seatRows[i] == null) continue; + var cfg = (saved != null && i < saved.Count) ? saved[i] : null; + seatRows[i].Populate(i, _deckNames, cfg); + } + } + + // ── Confirm ───────────────────────────────────────────── + private void OnConfirm() + { + var configs = new List(); + foreach (var row in seatRows) + configs.Add(row?.GetConfig() ?? new SeatConfig()); + + PlayerPrefs.SetString("BattleSeats", JsonConvert.SerializeObject(configs)); + PlayerPrefs.Save(); + gameObject.SetActive(false); + } + + // ── Models ───────────────────────────────────────────── + [Serializable] + public class SeatConfig + { + public bool isHuman; + public string deckName; + public string aiStyle; // Aggro / Control / Combo / Political + } + + [Serializable] private class DecksWrapper { public List decks; } + [Serializable] private class DeckEntry { public string name; } + } + + /// Per-seat row in the lobby modal (UI bindings are set in inspector). + [Serializable] + public class LobbyRowUI : MonoBehaviour + { + [SerializeField] private TMP_Text seatLabel; + [SerializeField] private Toggle humanToggle; + [SerializeField] private TMP_Dropdown deckDropdown; + [SerializeField] private TMP_Dropdown aiStyleDropdown; + + private static readonly List AiStyles = new() { "Aggro", "Control", "Combo", "Political" }; + + public void Populate(int seatIndex, List deckNames, LobbySetupModal.SeatConfig saved) + { + if (seatLabel) seatLabel.text = seatIndex == 0 ? "Seat 0 (You)" : $"Seat {seatIndex} (AI)"; + + if (humanToggle) + { + humanToggle.isOn = saved?.isHuman ?? (seatIndex == 0); + humanToggle.onValueChanged.AddListener(isHuman => + { + if (aiStyleDropdown) aiStyleDropdown.gameObject.SetActive(!isHuman); + }); + if (aiStyleDropdown) aiStyleDropdown.gameObject.SetActive(!humanToggle.isOn); + } + + if (deckDropdown) + { + deckDropdown.ClearOptions(); + var opts = new List { "-- Select Deck --" }; + opts.AddRange(deckNames); + deckDropdown.AddOptions(opts); + if (!string.IsNullOrEmpty(saved?.deckName)) + { + int idx = opts.IndexOf(saved.deckName); + deckDropdown.value = idx >= 0 ? idx : 0; + } + } + + if (aiStyleDropdown) + { + aiStyleDropdown.ClearOptions(); + aiStyleDropdown.AddOptions(AiStyles); + if (!string.IsNullOrEmpty(saved?.aiStyle)) + { + int idx = AiStyles.IndexOf(saved.aiStyle); + aiStyleDropdown.value = idx >= 0 ? idx : 0; + } + } + } + + public LobbySetupModal.SeatConfig GetConfig() => new() + { + isHuman = humanToggle?.isOn ?? false, + deckName = deckDropdown != null && deckDropdown.value > 0 + ? deckDropdown.options[deckDropdown.value].text : "", + aiStyle = aiStyleDropdown != null + ? aiStyleDropdown.options[aiStyleDropdown.value].text : "Aggro" + }; + } +} diff --git a/unity-client/Assets/Scripts/UI/Battleground/ZoneViewerModal.cs b/unity-client/Assets/Scripts/UI/Battleground/ZoneViewerModal.cs new file mode 100644 index 0000000..ea57a13 --- /dev/null +++ b/unity-client/Assets/Scripts/UI/Battleground/ZoneViewerModal.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using TMPro; + +namespace CommanderAILab.UI +{ + /// + /// Full-screen overlay showing all cards in a particular zone (graveyard, exile, etc.). + /// Opened by clicking zone count badges on each seat panel. + /// + public class ZoneViewerModal : MonoBehaviour + { + [Header("References")] + [SerializeField] private TMP_Text titleLabel; + [SerializeField] private Transform cardGrid; // GridLayoutGroup + [SerializeField] private GameObject cardPrefab; // CardView prefab + [SerializeField] private Button closeButton; + + private readonly List _cardGOs = new(); + + private void Start() => closeButton?.onClick.AddListener(Close); + + // ── Open ───────────────────────────────────────────────── + public void Open(string zoneName, CardZone zone) + { + if (titleLabel) titleLabel.text = $"{zoneName} ({zone.Count} cards)"; + + foreach (var go in _cardGOs) if (go) Destroy(go); + _cardGOs.Clear(); + + foreach (var view in zone.Cards) + { + if (view?.Model == null) continue; + var go = Instantiate(cardPrefab, cardGrid); + go.SetActive(true); + var cv = go.GetComponent(); + cv?.Bind(view.Model, false); + _cardGOs.Add(go); + } + + gameObject.SetActive(true); + } + + // ── Close ─────────────────────────────────────────────── + public void Close() + { + foreach (var go in _cardGOs) if (go) Destroy(go); + _cardGOs.Clear(); + gameObject.SetActive(false); + } + } +}