using Core; using Core.Lifecycle; using Minigames.FortFight.Core; using Minigames.FortFight.Data; using UnityEngine; namespace Minigames.FortFight.UI { /// /// Manages ammunition UI panel for a specific player. /// Shows/hides based on turn state and initializes buttons with player context. /// public class AmmunitionPanel : ManagedBehaviour { #region Inspector References [Header("Player Configuration")] [Tooltip("Which player this panel belongs to (0 = Player 1, 1 = Player 2)")] [SerializeField] private int playerIndex = 0; [Header("References")] [Tooltip("This player's slingshot controller")] [SerializeField] private SlingshotController slingshotController; // Note: AmmunitionManager and TurnManager accessed via singletons [Header("UI")] [Tooltip("Array of ammo buttons in this panel")] [SerializeField] private AmmoButton[] ammoButtons; [Tooltip("Root GameObject to show/hide entire panel")] [SerializeField] private GameObject panelRoot; #endregion #region Initialization internal override void OnManagedAwake() { base.OnManagedAwake(); // Validate references if (slingshotController == null) { Logging.Error($"[AmmunitionPanel] Player {playerIndex}: Slingshot controller not assigned!"); } if (ammoButtons == null || ammoButtons.Length == 0) { Logging.Warning($"[AmmunitionPanel] Player {playerIndex}: No ammo buttons assigned!"); } // Use assigned panelRoot or fall back to this GameObject if (panelRoot == null) { panelRoot = gameObject; } } internal override void OnManagedStart() { base.OnManagedStart(); // Initialize ammo buttons with player context InitializeButtons(); // Subscribe to turn events via singleton if (TurnManager.Instance != null) { TurnManager.Instance.OnTurnStarted += HandleTurnStarted; } // Start hidden SetPanelVisibility(false); } internal override void OnManagedDestroy() { base.OnManagedDestroy(); // Unsubscribe from events if (TurnManager.Instance != null) { TurnManager.Instance.OnTurnStarted -= HandleTurnStarted; } } /// /// Initialize all ammo buttons with player-specific configuration /// private void InitializeButtons() { if (AmmunitionManager.Instance == null || slingshotController == null || ammoButtons == null) { return; } // Get available projectile types from settings var availableTypes = AmmunitionManager.Instance.GetAvailableProjectileTypes(); var settings = GameManager.GetSettingsObject(); if (settings == null) { Logging.Error($"[AmmunitionPanel] Player {playerIndex}: Could not get FortFightSettings!"); return; } for (int i = 0; i < ammoButtons.Length && i < availableTypes.Count; i++) { if (ammoButtons[i] != null) { var config = settings.GetProjectileConfig(availableTypes[i]); if (config != null) { ammoButtons[i].Initialize(config, AmmunitionManager.Instance, slingshotController, playerIndex); Logging.Debug($"[AmmunitionPanel] Player {playerIndex}: Initialized button for {config.displayName}"); } } } } #endregion #region Turn Events /// /// Called when any player's turn starts - show/hide panel accordingly /// private void HandleTurnStarted(PlayerData player, TurnState turnState) { // Only show panel when it's this player's turn (not during transitions) bool shouldShow = player.PlayerIndex == playerIndex && (turnState == TurnState.PlayerOneTurn || turnState == TurnState.PlayerTwoTurn); SetPanelVisibility(shouldShow); if (shouldShow) { Logging.Debug($"[AmmunitionPanel] Showing panel for Player {playerIndex}"); } } /// /// Show or hide the entire panel /// private void SetPanelVisibility(bool visible) { if (panelRoot != null) { panelRoot.SetActive(visible); } } #endregion } }