Files
AppleHillsProduction/Assets/Scripts/UI/CardSystem/CardAlbumUI.cs
2025-10-20 16:33:58 +02:00

329 lines
12 KiB
C#

using System;
using System.Collections;
using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using Pixelplacement;
using UnityEngine;
using UnityEngine.UI;
namespace AppleHills.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 : MonoBehaviour
{
[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; // Changed to BoosterNotificationDot
[SerializeField] private GameObject backpackAnimationTarget;
[SerializeField] private GameObject newCardNotification;
[Header("Notification Settings")]
[SerializeField] private float notificationDuration = 3f;
[SerializeField] private AudioSource notificationSound;
private UIPageController _pageController;
private CardSystemManager _cardManager;
private bool _isInitialized = false;
private bool _hasUnseenCards = false;
private Coroutine _notificationCoroutine;
private void Awake()
{
// Get or create a UI page controller
_pageController = FindAnyObjectByType<UIPageController>();
if (_pageController == null)
{
GameObject controllerObj = new GameObject("UIPageController");
controllerObj.transform.SetParent(transform);
_pageController = controllerObj.AddComponent<UIPageController>();
}
// Initialize pages and hide them
InitializePages();
// Set up backpack button
if (backpackButton != null)
{
backpackButton.onClick.AddListener(OnBackpackButtonClicked);
}
// Initially show only the backpack icon
ShowOnlyBackpackIcon();
// Hide notifications initially
if (boosterNotificationDot != null)
boosterNotificationDot.gameObject.SetActive(false);
if (newCardNotification != null)
newCardNotification.SetActive(false);
}
private void Start()
{
// Get card manager
_cardManager = CardSystemManager.Instance;
// Subscribe to events
if (_cardManager != null)
{
_cardManager.OnBoosterCountChanged += UpdateBoosterCount;
_cardManager.OnBoosterOpened += HandleBoosterOpened;
_cardManager.OnCardCollected += HandleCardCollected;
_cardManager.OnCardRarityUpgraded += HandleCardRarityUpgraded;
// Initialize UI with current values
UpdateBoosterCount(_cardManager.GetBoosterPackCount());
}
_isInitialized = true;
}
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);
}
// Stop any coroutines
if (_notificationCoroutine != null)
StopCoroutine(_notificationCoroutine);
}
/// <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();
// If no pages are open, push the main menu
if (_pageController.CurrentPage == null)
{
_pageController.PushPage(mainMenuPage);
// Clear notification
if (boosterNotificationDot != null)
boosterNotificationDot.gameObject.SetActive(false);
_hasUnseenCards = false;
}
else if (_pageController.CurrentPage == mainMenuPage)
{
// If main menu is open, pop it
_pageController.PopPage();
}
}
/// <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();
}
else
{
backpackIcon.SetActive(false);
}
}
/// <summary>
/// Shows only the backpack icon, hiding all other UI elements
/// </summary>
private void ShowOnlyBackpackIcon()
{
backpackIcon.SetActive(true);
}
/// <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");
ShowNoBoostersNotification();
}
}
/// <summary>
/// Shows a notification that no booster packs are available
/// </summary>
private void ShowNoBoostersNotification()
{
if (newCardNotification != null)
{
// Set notification text
Text notificationText = newCardNotification.GetComponentInChildren<Text>();
if (notificationText != null)
notificationText.text = "No booster packs available!";
// Show and animate the notification
ShowTemporaryNotification(newCardNotification);
}
}
/// <summary>
/// Shows a notification temporarily and then hides it
/// </summary>
private void ShowTemporaryNotification(GameObject notification)
{
if (notification == null) return;
// Stop any existing notification coroutine
if (_notificationCoroutine != null)
StopCoroutine(_notificationCoroutine);
// Start new notification coroutine
_notificationCoroutine = StartCoroutine(ShowNotificationCoroutine(notification));
}
private IEnumerator ShowNotificationCoroutine(GameObject notification)
{
// Show notification
notification.SetActive(true);
notification.transform.localScale = Vector3.zero;
// Animate in
Tween.LocalScale(notification.transform, Vector3.one, 0.3f, 0f, Tween.EaseOutBack);
// Wait for duration
yield return new WaitForSeconds(notificationDuration);
// Animate out
Tween.LocalScale(notification.transform, Vector3.zero, 0.3f, 0f, Tween.EaseInBack, Tween.LoopType.None, null, () => {
notification.SetActive(false);
});
}
/// <summary>
/// Updates the booster count display
/// </summary>
private void UpdateBoosterCount(int count)
{
if (boosterNotificationDot != null)
{
boosterNotificationDot.SetCount(count);
// Animate the text for feedback
boosterNotificationDot.transform.localScale = Vector3.one * 1.2f;
Tween.LocalScale(boosterNotificationDot.transform, Vector3.one, 0.3f, 0f);
}
}
/// <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;
if (boosterNotificationDot != null)
boosterNotificationDot.gameObject.SetActive(true);
}
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)
{
// Show a special notification for rarity upgrade
if (newCardNotification != null)
{
// Set notification text
Text notificationText = newCardNotification.GetComponentInChildren<Text>();
if (notificationText != null)
notificationText.text = $"{card.Name} upgraded to {card.Rarity}!";
// Show and animate the notification
ShowTemporaryNotification(newCardNotification);
}
Logging.Debug($"[CardAlbumUI] Card upgraded: {card.Name} to {card.Rarity}");
}
}
}