312 lines
11 KiB
C#
312 lines
11 KiB
C#
using AppleHills.Data.CardSystem;
|
|
using Core;
|
|
using Data.CardSystem;
|
|
using Input;
|
|
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 : 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;
|
|
|
|
[Header("Notification Settings")]
|
|
[SerializeField] private AudioSource notificationSound;
|
|
|
|
// Public property to access the backpack icon for animations
|
|
public GameObject BackpackIcon => backpackIcon;
|
|
|
|
private UIPageController _pageController;
|
|
private CardSystemManager _cardManager;
|
|
private bool _isInitialized = false;
|
|
private bool _hasUnseenCards = false;
|
|
|
|
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 notification dot initially
|
|
if (boosterNotificationDot != null)
|
|
boosterNotificationDot.gameObject.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);
|
|
}
|
|
|
|
// Unsubscribe from page controller events
|
|
if (_pageController != null)
|
|
{
|
|
_pageController.OnPageChanged -= OnPageChanged;
|
|
}
|
|
}
|
|
|
|
/// <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();
|
|
|
|
// Eat input so the characters don't walk around
|
|
var backpackInput = backpackIcon.gameObject.GetComponentInParent<BackpackInput>();
|
|
InputManager.Instance.RegisterOverrideConsumer(backpackInput);
|
|
|
|
// If no pages are open, push the main menu
|
|
if (_pageController.CurrentPage == null)
|
|
{
|
|
_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);
|
|
}
|
|
}
|
|
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();
|
|
var backpackInput = backpackIcon.gameObject.GetComponentInParent<BackpackInput>();
|
|
InputManager.Instance.UnregisterOverrideConsumer(backpackInput);
|
|
}
|
|
else
|
|
{
|
|
if (backpackButton != null)
|
|
backpackButton.gameObject.SetActive(false);
|
|
}
|
|
|
|
// 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);
|
|
|
|
// 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}");
|
|
}
|
|
}
|
|
}
|