Implement Fort Fight minigame (#75)
Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Reviewed-on: #75
This commit is contained in:
157
Assets/Scripts/Minigames/FortFight/UI/AmmunitionPanel.cs
Normal file
157
Assets/Scripts/Minigames/FortFight/UI/AmmunitionPanel.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using Minigames.FortFight.Core;
|
||||
using Minigames.FortFight.Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.FortFight.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages ammunition UI panel for a specific player.
|
||||
/// Shows/hides based on turn state and initializes buttons with player context.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize all ammo buttons with player-specific configuration
|
||||
/// </summary>
|
||||
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<AppleHills.Core.Settings.IFortFightSettings>();
|
||||
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
/// Called when any player's turn starts - show/hide panel accordingly
|
||||
/// </summary>
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show or hide the entire panel
|
||||
/// </summary>
|
||||
private void SetPanelVisibility(bool visible)
|
||||
{
|
||||
if (panelRoot != null)
|
||||
{
|
||||
panelRoot.SetActive(visible);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user