Files
AppleHillsProduction/Assets/Scripts/UI/CardSystem/CardAlbumUI.cs
2025-11-05 20:37:17 +01:00

336 lines
12 KiB
C#

using AppleHills.Data.CardSystem;
using Core;
using Core.Lifecycle;
using Data.CardSystem;
using Pixelplacement;
using UI.Core;
using UnityEngine;
using UnityEngine.UI;
namespace UI.CardSystem
{
/// <summary>
/// Main UI controller for the card album system.
/// Manages the backpack icon and navigation between card system pages.
/// </summary>
public class CardAlbumUI : ManagedBehaviour
{
[Header("UI References")]
[SerializeField] private GameObject backpackIcon;
[SerializeField] private UIPage mainMenuPage;
[SerializeField] private UIPage albumViewPage;
[SerializeField] private UIPage boosterOpeningPage;
[Header("UI Elements")]
[SerializeField] private Button backpackButton;
[SerializeField] private BoosterNotificationDot boosterNotificationDot;
[Header("Notification Settings")]
[SerializeField] private AudioSource notificationSound;
// Public property to access the backpack icon for animations
public GameObject BackpackIcon => backpackIcon;
private UIPageController PageController => UIPageController.Instance;
private CardSystemManager _cardManager;
private bool _hasUnseenCards;
public override int ManagedAwakePriority => 65; // UI card systems
protected override void OnManagedAwake()
{
base.OnManagedAwake();
// Get card manager - safe to access .Instance here
_cardManager = CardSystemManager.Instance;
// Set up backpack button
if (backpackButton != null)
{
backpackButton.onClick.AddListener(OnBackpackButtonClicked);
}
// Hide notification dot initially
if (boosterNotificationDot != null)
boosterNotificationDot.gameObject.SetActive(false);
// Initially show only the backpack icon
ShowOnlyBackpackIcon();
// Initialize pages and hide them
InitializePages();
// Subscribe to card manager events
if (_cardManager != null)
{
_cardManager.OnBoosterCountChanged += UpdateBoosterCount;
_cardManager.OnBoosterOpened += HandleBoosterOpened;
_cardManager.OnCardCollected += HandleCardCollected;
_cardManager.OnCardRarityUpgraded += HandleCardRarityUpgraded;
// Initialize UI with current values
UpdateBoosterCount(_cardManager.GetBoosterPackCount());
}
// React to global UI hide/show events (top-page only) by toggling this GameObject
if (UIPageController.Instance != null)
{
UIPageController.Instance.OnAllUIHidden += HandleAllUIHidden;
UIPageController.Instance.OnAllUIShown += HandleAllUIShown;
}
}
private void OnDestroy()
{
// Unsubscribe from events
if (_cardManager != null)
{
_cardManager.OnBoosterCountChanged -= UpdateBoosterCount;
_cardManager.OnBoosterOpened -= HandleBoosterOpened;
_cardManager.OnCardCollected -= HandleCardCollected;
_cardManager.OnCardRarityUpgraded -= HandleCardRarityUpgraded;
}
// Clean up button listeners
if (backpackButton != null)
{
backpackButton.onClick.RemoveListener(OnBackpackButtonClicked);
}
// Unsubscribe from page controller events
if (PageController != null)
{
PageController.OnPageChanged -= OnPageChanged;
}
// Unsubscribe from global UI hide/show events
if (UIPageController.Instance != null)
{
UIPageController.Instance.OnAllUIHidden -= HandleAllUIHidden;
UIPageController.Instance.OnAllUIShown -= HandleAllUIShown;
}
}
/// <summary>
/// Initializes all UI pages and sets them up with proper callbacks
/// </summary>
private void InitializePages()
{
// Ensure all pages are inactive at start
if (mainMenuPage != null)
{
mainMenuPage.gameObject.SetActive(false);
}
if (albumViewPage != null)
{
albumViewPage.gameObject.SetActive(false);
}
if (boosterOpeningPage != null)
{
boosterOpeningPage.gameObject.SetActive(false);
}
// Set up page changed callback
if (PageController != null)
{
PageController.OnPageChanged += OnPageChanged;
}
}
/// <summary>
/// Handles click on the backpack icon
/// </summary>
private void OnBackpackButtonClicked()
{
// Play button sound if available
if (notificationSound != null)
notificationSound.Play();
PageController.PushPage(mainMenuPage);
// Clear notification for unseen cards when opening menu
if (_hasUnseenCards)
{
_hasUnseenCards = false;
}
// Hide the backpack button when entering menu
if (backpackButton != null)
{
backpackButton.gameObject.SetActive(false);
}
GameManager.Instance.RequestPause(this);
}
/// <summary>
/// Handles changes to the current page
/// </summary>
private void OnPageChanged(UIPage newPage)
{
// Hide/show backpack icon based on current page
if (newPage == null)
{
ShowOnlyBackpackIcon();
GameManager.Instance.ReleasePause(this);
}
else
{
// Check if any page in the stack has "Card" or "Album" in the name
bool hasCardOrAlbumPage = false;
if (PageController != null && PageController.PageStack != null)
{
foreach (var page in PageController.PageStack)
{
if (page != null && page.PageName != null)
{
if (page.PageName.Contains("Card") || page.PageName.Contains("Album"))
{
hasCardOrAlbumPage = true;
break;
}
}
}
}
// Hide backpack button if there's a card/album page in the stack
if (backpackButton != null)
{
backpackButton.gameObject.SetActive(!hasCardOrAlbumPage);
}
}
// Update menu if it's the main menu page
if (newPage == mainMenuPage && mainMenuPage is CardMenuPage menuPage)
{
// Force UI refresh when returning to main menu
menuPage.RefreshUI();
}
}
/// <summary>
/// Shows only the backpack icon, hiding all other UI elements
/// </summary>
private void ShowOnlyBackpackIcon()
{
if (backpackButton != null)
{
backpackButton.gameObject.SetActive(true);
// Update notification visibility based on booster count
bool hasBooters = _cardManager != null && _cardManager.GetBoosterPackCount() > 0;
// Show notification dot if there are boosters or unseen cards
if (boosterNotificationDot != null)
{
boosterNotificationDot.gameObject.SetActive(hasBooters || _hasUnseenCards);
}
}
}
/// <summary>
/// Opens the album view page
/// </summary>
public void OpenAlbumView()
{
PageController.PushPage(albumViewPage);
}
/// <summary>
/// Opens the booster opening page if boosters are available
/// </summary>
public void OpenBoosterPack()
{
if (_cardManager != null && _cardManager.GetBoosterPackCount() > 0)
{
PageController.PushPage(boosterOpeningPage);
}
else
{
Logging.Debug("[CardAlbumUI] No booster packs available");
}
}
/// <summary>
/// Updates the booster count display
/// </summary>
private void UpdateBoosterCount(int count)
{
if (boosterNotificationDot != null)
{
boosterNotificationDot.SetCount(count);
// Animate the notification dot for feedback
boosterNotificationDot.transform.localScale = Vector3.one * 1.2f;
Tween.LocalScale(boosterNotificationDot.transform, Vector3.one, 0.3f, 0f, obeyTimescale: false);
// Update visibility based on count
UpdateBoosterVisibility();
}
}
/// <summary>
/// Updates the visibility of the booster notification dot based on current state
/// </summary>
private void UpdateBoosterVisibility()
{
if (boosterNotificationDot != null)
{
// Show dot if there are boosters or unseen cards
bool hasBooters = _cardManager != null && _cardManager.GetBoosterPackCount() > 0;
boosterNotificationDot.gameObject.SetActive(hasBooters || _hasUnseenCards);
}
}
/// <summary>
/// Handles event when a booster pack is opened
/// </summary>
private void HandleBoosterOpened(System.Collections.Generic.List<CardData> cards)
{
Logging.Debug($"[CardAlbumUI] Booster opened with {cards.Count} cards");
// The booster opening page handles the UI for this event
}
/// <summary>
/// Handles event when a new card is collected
/// </summary>
private void HandleCardCollected(CardData card)
{
// If we're not in the album view or booster opening view,
// show a notification dot on the backpack
if (PageController.CurrentPage != albumViewPage &&
PageController.CurrentPage != boosterOpeningPage)
{
_hasUnseenCards = true;
UpdateBoosterVisibility();
}
Logging.Debug($"[CardAlbumUI] New card collected: {card.Name}");
}
/// <summary>
/// Handles event when a card is upgraded to a higher rarity
/// </summary>
private void HandleCardRarityUpgraded(CardData card)
{
// Just log the upgrade event without showing a notification
Logging.Debug($"[CardAlbumUI] Card upgraded: {card.Name} to {card.Rarity}");
}
// Handlers for UI controller hide/show events — toggle this GameObject active state
private void HandleAllUIHidden()
{
// Ensure UI returns to backpack-only state before deactivating so we don't leave the game paused
ShowOnlyBackpackIcon();
backpackButton.gameObject.SetActive(false);
}
private void HandleAllUIShown()
{
backpackButton.gameObject.SetActive(true);
}
}
}