First draft of the consolidated card system

This commit is contained in:
Michal Adam Pikulski
2025-10-20 10:29:22 +02:00
parent 07750dd5ba
commit 542dd9a4b7
19 changed files with 3014 additions and 1041 deletions

View File

@@ -24,6 +24,9 @@ namespace AppleHills.UI.CardSystem
[SerializeField] private Button resetFiltersButton;
[SerializeField] private CanvasGroup canvasGroup;
[Header("Navigation")]
[SerializeField] private Button backButton;
// Runtime references
private CardSystemManager _cardManager;
private List<CardUIElement> _displayedCards = new List<CardUIElement>();
@@ -37,6 +40,12 @@ namespace AppleHills.UI.CardSystem
canvasGroup = GetComponent<CanvasGroup>();
if (canvasGroup == null)
canvasGroup = gameObject.AddComponent<CanvasGroup>();
// Set up back button
if (backButton != null)
{
backButton.onClick.AddListener(OnBackButtonClicked);
}
}
/// <summary>
@@ -301,6 +310,10 @@ namespace AppleHills.UI.CardSystem
if (resetFiltersButton != null)
resetFiltersButton.onClick.RemoveListener(ResetFilters);
// Clean up back button listener
if (backButton != null)
backButton.onClick.RemoveListener(OnBackButtonClicked);
// Clean up card listeners
foreach (var card in _displayedCards)
@@ -319,5 +332,18 @@ namespace AppleHills.UI.CardSystem
}
}
}
/// <summary>
/// Handles click on the back button
/// </summary>
private void OnBackButtonClicked()
{
// Use the UIPageController to go back to the previous page
UIPageController pageController = UIPageController.Instance;
if (pageController != null)
{
pageController.PopPage();
}
}
}
}

View File

@@ -4,6 +4,7 @@ using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using Pixelplacement;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
@@ -16,25 +17,26 @@ namespace AppleHills.UI.CardSystem
{
[Header("UI Elements")]
[SerializeField] private GameObject boosterPackObject;
[SerializeField] private Transform cardRevealTransform;
[SerializeField] private RectTransform cardRevealContainer; // This should have a HorizontalLayoutGroup component
[SerializeField] private GameObject cardPrefab;
[SerializeField] private GameObject cardBackPrefab;
[SerializeField] private Button openBoosterButton;
[SerializeField] private Button continueButton;
[SerializeField] private CanvasGroup canvasGroup;
[Header("Navigation")]
[SerializeField] private Button backButton;
[Header("Animation Settings")]
[SerializeField] private float cardRevealDelay = 0.5f;
[SerializeField] private float cardMoveToBackpackDelay = 1.0f;
[SerializeField] private Transform backpackTargetTransform;
[SerializeField] private AudioSource cardRevealSound;
[SerializeField] private AudioSource rareCardSound;
[SerializeField] private ParticleSystem cardRevealParticles;
[SerializeField] private ParticleSystem rareCardParticles;
[SerializeField] private float cardRevealDelay = 0.3f;
[SerializeField] private float cardMoveToBackpackDelay = 0.8f;
[SerializeField] private float flipAnimationDuration = 0.5f;
// State tracking
private enum OpeningState
{
BoosterReady,
CardBacksVisible,
CardsRevealing,
CardsRevealed,
MovingToBackpack,
@@ -43,13 +45,17 @@ namespace AppleHills.UI.CardSystem
private OpeningState _currentState = OpeningState.BoosterReady;
private List<CardUIElement> _revealedCards = new List<CardUIElement>();
private List<GameObject> _cardBacks = new List<GameObject>();
private List<CardData> _boosterCards = new List<CardData>();
private int _revealedCardCount = 0;
private CardSystemManager _cardManager;
private Coroutine _revealCoroutine;
private CardAlbumUI _cardAlbumUI;
private Coroutine _moveToBackpackCoroutine;
private void Awake()
{
_cardManager = CardSystemManager.Instance;
_cardAlbumUI = FindObjectOfType<CardAlbumUI>();
// Set up button listeners
if (openBoosterButton != null)
@@ -63,6 +69,12 @@ namespace AppleHills.UI.CardSystem
continueButton.gameObject.SetActive(false);
}
// Set up back button
if (backButton != null)
{
backButton.onClick.AddListener(OnBackButtonClicked);
}
// Make sure we have a CanvasGroup for transitions
if (canvasGroup == null)
canvasGroup = GetComponent<CanvasGroup>();
@@ -83,14 +95,36 @@ namespace AppleHills.UI.CardSystem
continueButton.onClick.RemoveListener(OnContinueClicked);
}
if (backButton != null)
{
backButton.onClick.RemoveListener(OnBackButtonClicked);
}
// Stop any running coroutines
if (_revealCoroutine != null)
StopCoroutine(_revealCoroutine);
if (_moveToBackpackCoroutine != null)
StopCoroutine(_moveToBackpackCoroutine);
}
/// <summary>
/// Handles click on the back button
/// </summary>
private void OnBackButtonClicked()
{
// Don't allow going back during animations or card reveals
if (_currentState == OpeningState.CardsRevealing ||
_currentState == OpeningState.MovingToBackpack)
{
return;
}
// Use the UIPageController to go back to the previous page
UIPageController pageController = UIPageController.Instance;
if (pageController != null)
{
pageController.PopPage();
}
}
/// <summary>
/// Resets the page to its initial state when it becomes active
/// </summary>
@@ -114,10 +148,19 @@ namespace AppleHills.UI.CardSystem
}
_revealedCards.Clear();
// Clear card backs
foreach (var cardBack in _cardBacks)
{
if (cardBack != null)
Destroy(cardBack);
}
_cardBacks.Clear();
// Reset state
_currentState = OpeningState.BoosterReady;
_revealedCardCount = 0;
// Show booster pack, hide continue button
// Show booster pack, show open button, hide continue button
if (boosterPackObject != null)
{
boosterPackObject.SetActive(true);
@@ -132,6 +175,12 @@ namespace AppleHills.UI.CardSystem
{
continueButton.gameObject.SetActive(false);
}
// Make back button visible
if (backButton != null)
{
backButton.gameObject.SetActive(true);
}
}
/// <summary>
@@ -141,12 +190,12 @@ namespace AppleHills.UI.CardSystem
{
if (_currentState != OpeningState.BoosterReady) return;
_currentState = OpeningState.CardsRevealing;
_currentState = OpeningState.CardBacksVisible;
// Open the booster pack and get the cards
List<CardData> newCards = _cardManager.OpenBoosterPack();
_boosterCards = _cardManager.OpenBoosterPack();
if (newCards.Count > 0)
if (_boosterCards.Count > 0)
{
// Hide the booster pack and open button
if (boosterPackObject != null)
@@ -162,8 +211,8 @@ namespace AppleHills.UI.CardSystem
openBoosterButton.gameObject.SetActive(false);
}
// Start revealing cards
_revealCoroutine = StartCoroutine(RevealCards(newCards));
// Show card backs first
StartCoroutine(ShowCardBacks());
}
else
{
@@ -173,81 +222,120 @@ namespace AppleHills.UI.CardSystem
}
/// <summary>
/// Reveals cards one by one with animation
/// Shows card backs in the reveal positions
/// </summary>
private IEnumerator RevealCards(List<CardData> cards)
private IEnumerator ShowCardBacks()
{
// Wait a short delay before revealing cards
// Wait a short delay before showing card backs
yield return new WaitForSeconds(0.5f);
// Reveal each card
foreach (var cardData in cards)
// Create card backs for each position (usually 3)
for (int i = 0; i < Mathf.Min(_boosterCards.Count, cardRevealContainer.childCount); i++)
{
// Instantiate card UI element
GameObject cardObj = Instantiate(cardPrefab, cardRevealTransform);
CardUIElement cardUI = cardObj.GetComponent<CardUIElement>();
if (cardBackPrefab == null || cardRevealContainer.GetChild(i) == null) continue;
if (cardUI != null)
// Instantiate card back object at the correct position
GameObject cardBackObj = Instantiate(cardBackPrefab, cardRevealContainer.GetChild(i));
cardBackObj.transform.localPosition = Vector3.zero;
_cardBacks.Add(cardBackObj);
// Store the index for later reference when clicked
int cardIndex = i;
// Add click handler to the card back
Button cardBackButton = cardBackObj.GetComponent<Button>();
if (cardBackButton == null)
{
// Set up the card data
cardUI.SetupCard(cardData);
_revealedCards.Add(cardUI);
// Set initial scale to zero for animation
cardObj.transform.localScale = Vector3.zero;
// Play reveal animation using Pixelplacement.Tween
Tween.LocalScale(cardObj.transform, Vector3.one, 0.5f, 0f, Tween.EaseOutBack);
// Call card's show animation
cardUI.OnShowAnimation();
// Play appropriate sound and particles based on rarity
if (cardData.Rarity >= CardRarity.Rare)
{
// Play rare card sound
if (rareCardSound != null)
{
rareCardSound.Play();
}
// Play rare card particles
if (rareCardParticles != null)
{
rareCardParticles.transform.position = cardObj.transform.position;
rareCardParticles.Play();
}
}
else
{
// Play regular card sound
if (cardRevealSound != null)
{
cardRevealSound.Play();
}
// Play regular particles
if (cardRevealParticles != null)
{
cardRevealParticles.transform.position = cardObj.transform.position;
cardRevealParticles.Play();
}
}
// Wait for animation delay
yield return new WaitForSeconds(cardRevealDelay);
cardBackButton = cardBackObj.AddComponent<Button>();
}
// Configure the button
cardBackButton.onClick.AddListener(() => OnCardBackClicked(cardIndex));
// Set initial scale to zero for animation
cardBackObj.transform.localScale = Vector3.zero;
// Play reveal animation using Pixelplacement.Tween
Tween.LocalScale(cardBackObj.transform, Vector3.one, 0.5f, 0f, Tween.EaseOutBack);
// Wait for animation delay
yield return new WaitForSeconds(cardRevealDelay);
}
// Update state and show continue button
_currentState = OpeningState.CardsRevealed;
if (continueButton != null)
// Update state
_currentState = OpeningState.CardBacksVisible;
}
/// <summary>
/// Handles click on a card back to reveal the card
/// </summary>
private void OnCardBackClicked(int cardIndex)
{
// Only respond to clicks when in the appropriate state
if (_currentState != OpeningState.CardBacksVisible) return;
// Ensure the index is valid
if (cardIndex < 0 || cardIndex >= _boosterCards.Count || cardIndex >= _cardBacks.Count) return;
// Get the card data and card back
CardData cardData = _boosterCards[cardIndex];
GameObject cardBack = _cardBacks[cardIndex];
// Start the reveal animation for this specific card
StartCoroutine(RevealCard(cardIndex, cardData, cardBack));
}
/// <summary>
/// Reveals an individual card with animation
/// </summary>
private IEnumerator RevealCard(int cardIndex, CardData cardData, GameObject cardBack)
{
if (cardBack == null || cardRevealContainer.GetChild(cardIndex) == null) yield break;
// Start flip animation
Transform cardBackTransform = cardBack.transform;
// Step 1: Flip the card 90 degrees (showing the edge)
Tween.LocalRotation(cardBackTransform, new Vector3(0, 90, 0), flipAnimationDuration * 0.5f, 0);
// Wait for half the flip duration
yield return new WaitForSeconds(flipAnimationDuration * 0.5f);
// Step 2: Remove the card back and create the actual card
Destroy(cardBack);
_cardBacks[cardIndex] = null;
// Create the actual card at the same position
GameObject cardObj = Instantiate(cardPrefab, cardRevealContainer.GetChild(cardIndex));
cardObj.transform.localPosition = Vector3.zero;
cardObj.transform.localRotation = Quaternion.Euler(0, 90, 0); // Start at 90 degrees
// Set up the card data
CardUIElement cardUI = cardObj.GetComponent<CardUIElement>();
if (cardUI != null)
{
continueButton.gameObject.SetActive(true);
// Animate button appearance
continueButton.transform.localScale = Vector3.zero;
Tween.LocalScale(continueButton.transform, Vector3.one, 0.3f, 0f, Tween.EaseOutBack);
cardUI.SetupCard(cardData);
_revealedCards.Add(cardUI);
}
// Step 3: Complete the flip animation
Tween.LocalRotation(cardObj.transform, Vector3.zero, flipAnimationDuration * 0.5f, 0);
// Increment the revealed card count
_revealedCardCount++;
// If all cards have been revealed, show the continue button
if (_revealedCardCount >= _boosterCards.Count)
{
_currentState = OpeningState.CardsRevealed;
if (continueButton != null)
{
continueButton.gameObject.SetActive(true);
// Animate button appearance
continueButton.transform.localScale = Vector3.zero;
Tween.LocalScale(continueButton.transform, Vector3.one, 0.3f, 0f, Tween.EaseOutBack);
}
}
}
@@ -274,10 +362,20 @@ namespace AppleHills.UI.CardSystem
/// </summary>
private IEnumerator MoveCardsToBackpack()
{
// If no backpack target, use a default position (lower-right corner)
Vector3 targetPosition = (backpackTargetTransform != null)
? backpackTargetTransform.position
: new Vector3(Screen.width * 0.9f, Screen.height * 0.1f, 0f);
// Use the backpackIcon from CardAlbumUI as the target
Vector3 targetPosition;
if (_cardAlbumUI != null && _cardAlbumUI.transform.Find("BackpackIcon") != null)
{
// Get the world position of the backpack icon
targetPosition = _cardAlbumUI.transform.Find("BackpackIcon").position;
}
else
{
// Fallback to a default position (lower-right corner)
targetPosition = new Vector3(Screen.width * 0.9f, Screen.height * 0.1f, 0f);
Logging.Warning("[BoosterOpeningPage] Couldn't find backpack icon, using default position.");
}
// Move each card to backpack with animation
foreach (var cardUI in _revealedCards)

View File

@@ -23,19 +23,18 @@ namespace AppleHills.UI.CardSystem
[Header("UI Elements")]
[SerializeField] private Button backpackButton;
[SerializeField] private BoosterNotificationDot boosterNotificationDot; // Changed to BoosterNotificationDot
[SerializeField] private GameObject backpackAnimationTarget;
[SerializeField] private GameObject newCardNotification;
[SerializeField] private BoosterNotificationDot boosterNotificationDot;
[Header("Notification Settings")]
[SerializeField] private float notificationDuration = 3f;
[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 Coroutine _notificationCoroutine;
private void Awake()
{
@@ -60,12 +59,9 @@ namespace AppleHills.UI.CardSystem
// Initially show only the backpack icon
ShowOnlyBackpackIcon();
// Hide notifications initially
// Hide notification dot initially
if (boosterNotificationDot != null)
boosterNotificationDot.gameObject.SetActive(false);
if (newCardNotification != null)
newCardNotification.SetActive(false);
}
private void Start()
@@ -105,9 +101,11 @@ namespace AppleHills.UI.CardSystem
backpackButton.onClick.RemoveListener(OnBackpackButtonClicked);
}
// Stop any coroutines
if (_notificationCoroutine != null)
StopCoroutine(_notificationCoroutine);
// Unsubscribe from page controller events
if (_pageController != null)
{
_pageController.OnPageChanged -= OnPageChanged;
}
}
/// <summary>
@@ -152,11 +150,12 @@ namespace AppleHills.UI.CardSystem
{
_pageController.PushPage(mainMenuPage);
// Clear notification
if (boosterNotificationDot != null)
boosterNotificationDot.gameObject.SetActive(false);
_hasUnseenCards = false;
// Clear notification for unseen cards when opening menu
if (_hasUnseenCards)
{
_hasUnseenCards = false;
UpdateBoosterVisibility();
}
}
else if (_pageController.CurrentPage == mainMenuPage)
{
@@ -177,7 +176,15 @@ namespace AppleHills.UI.CardSystem
}
else
{
backpackIcon.SetActive(false);
if (backpackIcon != null)
backpackIcon.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();
}
}
@@ -186,7 +193,11 @@ namespace AppleHills.UI.CardSystem
/// </summary>
private void ShowOnlyBackpackIcon()
{
backpackIcon.SetActive(true);
if (backpackIcon != null)
backpackIcon.SetActive(true);
// Update booster notification visibility
UpdateBoosterVisibility();
}
/// <summary>
@@ -209,60 +220,9 @@ namespace AppleHills.UI.CardSystem
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>
@@ -272,9 +232,25 @@ namespace AppleHills.UI.CardSystem
{
boosterNotificationDot.SetCount(count);
// Animate the text for feedback
// 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);
}
}
@@ -298,8 +274,7 @@ namespace AppleHills.UI.CardSystem
_pageController.CurrentPage != boosterOpeningPage)
{
_hasUnseenCards = true;
if (boosterNotificationDot != null)
boosterNotificationDot.gameObject.SetActive(true);
UpdateBoosterVisibility();
}
Logging.Debug($"[CardAlbumUI] New card collected: {card.Name}");
@@ -310,18 +285,7 @@ namespace AppleHills.UI.CardSystem
/// </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);
}
// Just log the upgrade event without showing a notification
Logging.Debug($"[CardAlbumUI] Card upgraded: {card.Name} to {card.Rarity}");
}
}

View File

@@ -82,6 +82,14 @@ namespace AppleHills.UI.CardSystem
}
}
/// <summary>
/// Public method to refresh UI state when returning to this page
/// </summary>
public void RefreshUI()
{
UpdateUI();
}
/// <summary>
/// Updates the UI elements based on current state
/// </summary>