2025-11-11 20:25:23 +01:00
|
|
|
|
using Core.SaveLoad;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.EventSystems;
|
2025-11-12 09:24:27 +01:00
|
|
|
|
using AppleHills.Core.Settings;
|
|
|
|
|
|
using Core;
|
2025-11-12 10:55:00 +01:00
|
|
|
|
using AppleHills.Data.CardSystem;
|
2025-11-11 20:25:23 +01:00
|
|
|
|
|
|
|
|
|
|
namespace UI.CardSystem.StateMachine.States
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Enlarged state for REPEAT cards - shows progress bar toward next rarity upgrade.
|
2025-11-12 10:55:00 +01:00
|
|
|
|
/// Uses ProgressBarController to animate progress filling.
|
|
|
|
|
|
/// Auto-upgrades card when threshold is reached.
|
2025-11-11 20:25:23 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class CardEnlargedRepeatState : AppleState, IPointerClickHandler
|
|
|
|
|
|
{
|
|
|
|
|
|
[Header("State-Owned Visuals")]
|
2025-11-12 10:55:00 +01:00
|
|
|
|
[SerializeField] private ProgressBarController progressBar;
|
2025-11-11 20:25:23 +01:00
|
|
|
|
|
|
|
|
|
|
private CardContext _context;
|
2025-11-12 09:24:27 +01:00
|
|
|
|
private ICardSystemSettings _settings;
|
2025-11-11 20:25:23 +01:00
|
|
|
|
private Vector3 _originalScale;
|
2025-11-12 10:55:00 +01:00
|
|
|
|
private bool _waitingForTap = false;
|
2025-11-11 20:25:23 +01:00
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
_context = GetComponentInParent<CardContext>();
|
2025-11-12 09:24:27 +01:00
|
|
|
|
_settings = GameManager.GetSettingsObject<ICardSystemSettings>();
|
2025-11-11 20:25:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnEnterState()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Store original scale
|
|
|
|
|
|
_originalScale = _context.RootTransform.localScale;
|
2025-11-12 10:55:00 +01:00
|
|
|
|
_waitingForTap = false;
|
2025-11-11 20:25:23 +01:00
|
|
|
|
|
2025-11-12 11:58:01 +01:00
|
|
|
|
// Show progress bar
|
2025-11-12 10:55:00 +01:00
|
|
|
|
if (progressBar != null)
|
|
|
|
|
|
{
|
2025-11-12 11:58:01 +01:00
|
|
|
|
progressBar.gameObject.SetActive(true);
|
|
|
|
|
|
|
2025-11-12 10:55:00 +01:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
2025-11-12 11:58:01 +01:00
|
|
|
|
|
|
|
|
|
|
// Enlarge the card
|
|
|
|
|
|
if (_context.Animator != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_context.Animator.PlayEnlarge(_settings.NewCardEnlargedScale);
|
|
|
|
|
|
}
|
2025-11-12 10:55:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnProgressComplete()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Check if this card triggers an upgrade
|
|
|
|
|
|
if (ShouldTriggerUpgrade())
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
2025-11-11 20:25:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-12 10:55:00 +01:00
|
|
|
|
private void TriggerUpgrade()
|
2025-11-11 20:25:23 +01:00
|
|
|
|
{
|
2025-11-12 10:55:00 +01:00
|
|
|
|
_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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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);
|
2025-11-11 20:25:23 +01:00
|
|
|
|
|
2025-11-12 10:55:00 +01:00
|
|
|
|
// 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)
|
2025-11-11 20:25:23 +01:00
|
|
|
|
{
|
2025-11-12 10:55:00 +01:00
|
|
|
|
_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();
|
|
|
|
|
|
}
|
2025-11-11 20:25:23 +01:00
|
|
|
|
}
|
2025-11-12 10:55:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void TransitionToNewCardView()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Update context to show as new card
|
|
|
|
|
|
_context.IsNewCard = true;
|
2025-11-11 20:25:23 +01:00
|
|
|
|
|
2025-11-12 10:55:00 +01:00
|
|
|
|
// Hide progress bar before transitioning
|
2025-11-12 11:58:01 +01:00
|
|
|
|
if (progressBar != null)
|
2025-11-11 20:25:23 +01:00
|
|
|
|
{
|
2025-11-12 11:58:01 +01:00
|
|
|
|
progressBar.gameObject.SetActive(false);
|
2025-11-11 20:25:23 +01:00
|
|
|
|
}
|
2025-11-12 10:55:00 +01:00
|
|
|
|
|
|
|
|
|
|
// Transition to EnlargedNewState (card is already enlarged, will show NEW badge)
|
|
|
|
|
|
_context.StateMachine.ChangeState("EnlargedNewState");
|
2025-11-11 20:25:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
|
|
|
|
{
|
2025-11-12 10:55:00 +01:00
|
|
|
|
// Only respond to clicks if waiting for tap
|
|
|
|
|
|
if (!_waitingForTap)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Fire dismissed event
|
|
|
|
|
|
_context.FireCardDismissed();
|
|
|
|
|
|
|
2025-11-11 20:25:23 +01:00
|
|
|
|
// Tap to dismiss - shrink back and transition to revealed state
|
|
|
|
|
|
if (_context.Animator != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_context.Animator.PlayShrink(_originalScale, onComplete: () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
_context.StateMachine.ChangeState("RevealedState");
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-11-12 10:55:00 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_context.StateMachine.ChangeState("RevealedState");
|
|
|
|
|
|
}
|
2025-11-11 20:25:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Hide progress bar when leaving state
|
2025-11-12 11:58:01 +01:00
|
|
|
|
if (progressBar != null)
|
2025-11-11 20:25:23 +01:00
|
|
|
|
{
|
2025-11-12 11:58:01 +01:00
|
|
|
|
progressBar.gameObject.SetActive(false);
|
2025-11-11 20:25:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|