Fix up card flows, align with the old 1:1

This commit is contained in:
Michal Pikulski
2025-11-12 10:55:00 +01:00
parent 8a3471bd1a
commit a9f102b3c4
15 changed files with 2033 additions and 479 deletions

View File

@@ -1,4 +1,5 @@
using AppleHills.Data.CardSystem;
using System;
using AppleHills.Data.CardSystem;
using Core.SaveLoad;
using UnityEngine;
@@ -29,6 +30,18 @@ namespace UI.CardSystem.StateMachine
public bool IsNewCard { get; set; }
public int RepeatCardCount { get; set; }
// Events for external coordination (BoosterOpeningPage, etc.)
public event Action<CardContext> OnFlipComplete;
public event Action<CardContext> OnCardDismissed;
public event Action<CardContext> OnCardInteractionComplete;
public event Action<CardContext> OnUpgradeTriggered;
// Helper methods for states
public void FireFlipComplete() => OnFlipComplete?.Invoke(this);
public void FireCardDismissed() => OnCardDismissed?.Invoke(this);
public void FireCardInteractionComplete() => OnCardInteractionComplete?.Invoke(this);
public void FireUpgradeTriggered() => OnUpgradeTriggered?.Invoke(this);
private void Awake()
{
// Auto-find components if not assigned

View File

@@ -45,6 +45,9 @@ namespace UI.CardSystem.StateMachine.States
public void OnPointerClick(PointerEventData eventData)
{
// Fire dismissed event
_context.FireCardDismissed();
// Tap to dismiss - shrink back and transition to revealed state
if (_context.Animator != null)
{
@@ -53,6 +56,11 @@ namespace UI.CardSystem.StateMachine.States
_context.StateMachine.ChangeState("RevealedState");
});
}
else
{
// Fallback if no animator
_context.StateMachine.ChangeState("RevealedState");
}
}
private void OnDisable()

View File

@@ -1,27 +1,27 @@
using Core.SaveLoad;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using AppleHills.Core.Settings;
using Core;
using AppleHills.Data.CardSystem;
namespace UI.CardSystem.StateMachine.States
{
/// <summary>
/// Enlarged state for REPEAT cards - shows progress bar toward next rarity upgrade.
/// Owns the ProgressBarUI as a child GameObject.
/// Uses ProgressBarController to animate progress filling.
/// Auto-upgrades card when threshold is reached.
/// </summary>
public class CardEnlargedRepeatState : AppleState, IPointerClickHandler
{
[Header("State-Owned Visuals")]
[SerializeField] private GameObject progressBarContainer;
[SerializeField] private Image progressBarFill;
[SerializeField] private TextMeshProUGUI progressText;
[SerializeField] private ProgressBarController progressBar;
private CardContext _context;
private ICardSystemSettings _settings;
private Vector3 _originalScale;
private bool _waitingForTap = false;
private void Awake()
{
@@ -33,12 +33,12 @@ namespace UI.CardSystem.StateMachine.States
{
// Store original scale
_originalScale = _context.RootTransform.localScale;
_waitingForTap = false;
// Show progress bar
// Show progress bar container
if (progressBarContainer != null)
{
progressBarContainer.SetActive(true);
UpdateProgressBar();
}
// Enlarge the card
@@ -46,27 +46,148 @@ namespace UI.CardSystem.StateMachine.States
{
_context.Animator.PlayEnlarge(_settings.NewCardEnlargedScale);
}
// Show progress with blink animation
if (progressBar != null)
{
int currentCount = _context.RepeatCardCount + 1; // +1 because we just got this card
int maxCount = _settings.CardsToUpgrade;
progressBar.ShowProgress(currentCount, maxCount, OnProgressComplete);
}
else
{
Logging.Warning("[CardEnlargedRepeatState] ProgressBar component not assigned!");
OnProgressComplete();
}
}
private void UpdateProgressBar()
private void OnProgressComplete()
{
int currentCount = _context.RepeatCardCount;
int cardsToUpgrade = _settings.CardsToUpgrade;
float progress = (float)currentCount / cardsToUpgrade;
if (progressBarFill != null)
// Check if this card triggers an upgrade
if (ShouldTriggerUpgrade())
{
progressBarFill.fillAmount = progress;
Logging.Debug($"[CardEnlargedRepeatState] Card will trigger upgrade! ({_context.RepeatCardCount + 1}/{_settings.CardsToUpgrade})");
TriggerUpgrade();
}
else
{
// No upgrade - just wait for tap to dismiss
Logging.Debug($"[CardEnlargedRepeatState] Progress shown, waiting for tap to dismiss");
_waitingForTap = true;
}
}
private bool ShouldTriggerUpgrade()
{
int currentCount = _context.RepeatCardCount + 1; // +1 for the card we just got
CardRarity currentRarity = _context.CardData.Rarity;
// Can't upgrade if already Legendary
if (currentRarity == CardRarity.Legendary)
return false;
// Check if we've hit the threshold
return currentCount >= _settings.CardsToUpgrade;
}
private void TriggerUpgrade()
{
_context.FireUpgradeTriggered();
CardData cardData = _context.CardData;
CardRarity oldRarity = cardData.Rarity;
CardRarity newRarity = oldRarity + 1;
Logging.Debug($"[CardEnlargedRepeatState] Upgrading card from {oldRarity} to {newRarity}");
// Get the existing card at lower rarity from inventory
CardData existingLowerRarity = Data.CardSystem.CardSystemManager.Instance.GetCardInventory().GetCard(cardData.Name, oldRarity);
if (existingLowerRarity != null)
{
// Reset lower rarity count to 0
existingLowerRarity.CopiesOwned = 0;
}
if (progressText != null)
// Create upgraded card data
CardData upgradedCard = new CardData(cardData);
upgradedCard.Rarity = newRarity;
upgradedCard.CopiesOwned = 1;
// Check if this card is new at the higher rarity
bool isNewAtHigherRarity = Data.CardSystem.CardSystemManager.Instance.IsCardNew(upgradedCard, out CardData existingHigherRarity);
// Add to inventory
Data.CardSystem.CardSystemManager.Instance.GetCardInventory().AddCard(upgradedCard);
// Update our card data to show upgraded rarity
cardData.Rarity = newRarity;
// Update the CardDisplay to show new rarity
if (_context.CardDisplay != null)
{
progressText.text = $"{currentCount}/{cardsToUpgrade}";
_context.CardDisplay.SetupCard(cardData);
}
// Determine next transition
if (isNewAtHigherRarity || newRarity == CardRarity.Legendary)
{
// Show as NEW at higher rarity
Logging.Debug($"[CardEnlargedRepeatState] Card is NEW at {newRarity}, transitioning to EnlargedNewState");
TransitionToNewCardView();
}
else
{
// Already have copies at higher rarity - show progress there too
int ownedAtHigherRarity = existingHigherRarity.CopiesOwned;
Logging.Debug($"[CardEnlargedRepeatState] Card is REPEAT at {newRarity} ({ownedAtHigherRarity}/{_settings.CardsToUpgrade}), showing progress");
// Update context for higher rarity
_context.IsNewCard = false;
_context.RepeatCardCount = ownedAtHigherRarity;
// Re-enter this state with updated data (will show progress for higher rarity)
// First hide current progress, then show new progress
if (progressBar != null)
{
progressBar.ShowProgress(ownedAtHigherRarity + 1, _settings.CardsToUpgrade, () =>
{
// After showing progress at higher rarity, transition to NEW
TransitionToNewCardView();
});
}
else
{
TransitionToNewCardView();
}
}
}
private void TransitionToNewCardView()
{
// Update context to show as new card
_context.IsNewCard = true;
// Hide progress bar before transitioning
if (progressBarContainer != null)
{
progressBarContainer.SetActive(false);
}
// Transition to EnlargedNewState (card is already enlarged, will show NEW badge)
_context.StateMachine.ChangeState("EnlargedNewState");
}
public void OnPointerClick(PointerEventData eventData)
{
// Only respond to clicks if waiting for tap
if (!_waitingForTap)
return;
// Fire dismissed event
_context.FireCardDismissed();
// Tap to dismiss - shrink back and transition to revealed state
if (_context.Animator != null)
{
@@ -75,6 +196,10 @@ namespace UI.CardSystem.StateMachine.States
_context.StateMachine.ChangeState("RevealedState");
});
}
else
{
_context.StateMachine.ChangeState("RevealedState");
}
}
private void OnDisable()

View File

@@ -47,11 +47,21 @@ namespace UI.CardSystem.StateMachine.States
private void OnFlipComplete()
{
// Fire flip complete event
_context.FireFlipComplete();
// Transition based on whether this is a new card or repeat
if (_context.IsNewCard)
{
_context.StateMachine.ChangeState("EnlargedNewState");
}
else if (_context.CardData.Rarity == AppleHills.Data.CardSystem.CardRarity.Legendary)
{
// Legendary repeat - skip enlarge, they can't upgrade
// Add to inventory and move to revealed state
Data.CardSystem.CardSystemManager.Instance.AddCardToInventoryDelayed(_context.CardData);
_context.StateMachine.ChangeState("RevealedState");
}
else if (_context.RepeatCardCount > 0)
{
_context.StateMachine.ChangeState("EnlargedRepeatState");

View File

@@ -1,14 +1,14 @@
using Core.SaveLoad;
using UnityEngine;
using UnityEngine.EventSystems;
namespace UI.CardSystem.StateMachine.States
{
/// <summary>
/// Revealed state - card is flipped and visible, waiting for interaction.
/// Can be clicked to enlarge or dragged to place in album.
/// Revealed state - card is flipped and visible at normal size.
/// This is the "waiting" state:
/// - In booster flow: waiting for all cards to finish before animating to album
/// - In album placement flow: waiting to be dragged to a slot
/// </summary>
public class CardRevealedState : AppleState, IPointerClickHandler
public class CardRevealedState : AppleState
{
private CardContext _context;
@@ -19,14 +19,9 @@ namespace UI.CardSystem.StateMachine.States
public override void OnEnterState()
{
// Card is simply visible and interactable
// No special animations needed
}
public void OnPointerClick(PointerEventData eventData)
{
// Click to enlarge
_context.StateMachine.ChangeState("EnlargedNewState");
// Card is at normal size, fully revealed
// Fire interaction complete event (for BoosterOpeningPage tracking)
_context.FireCardInteractionComplete();
}
}
}