Files
AppleHillsProduction/Assets/Scripts/CardSystem/StateMachine/States/CardIdleState.cs
tschesky 235fa04eba Merge a card refresh (#59)
- **Refactored Card Placement Flow**
  - Separated card presentation from orchestration logic
  - Extracted `CornerCardManager` for pending card lifecycle (spawn, shuffle, rebuild)
  - Extracted `AlbumNavigationService` for book page navigation and zone mapping
  - Extracted `CardEnlargeController` for backdrop management and card reparenting
  - Implemented controller pattern (non-MonoBehaviour) for complex logic
  - Cards now unparent from slots before rebuild to prevent premature destruction

- **Improved Corner Card Display**
  - Fixed cards spawning on top of each other during rebuild
  - Implemented shuffle-to-front logic (remaining cards occupy slots 0→1→2)
  - Added smart card selection (prioritizes cards matching current album page)
  - Pending cards now removed from queue immediately on drag start
  - Corner cards rebuild after each placement with proper slot reassignment

- **Enhanced Album Card Viewing**
  - Added dramatic scale increase when viewing cards from album slots
  - Implemented shrink animation when dismissing enlarged cards
  - Cards transition: `PlacedInSlotState` → `AlbumEnlargedState` → `PlacedInSlotState`
  - Backdrop shows/hides with card enlarge/shrink cycle
  - Cards reparent to enlarged container while viewing, return to slot after

- **State Machine Improvements**
  - Added `CardStateNames` constants for type-safe state transitions
  - Implemented `ICardClickHandler` and `ICardStateDragHandler` interfaces
  - State transitions now use cached property indices
  - `BoosterCardContext` separated from `CardContext` for single responsibility

- **Code Cleanup**
  - Identified unused `SlotContainerHelper.cs` (superseded by `CornerCardManager`)
  - Identified unused `BoosterPackDraggable.canOpenOnDrop` field
  - Identified unused `AlbumViewPage._previousInputMode` (hardcoded value)
  - Identified unused `Card.OnPlacedInAlbumSlot` event (no subscribers)

Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com>
Reviewed-on: #59
2025-11-18 08:40:59 +00:00

178 lines
6.4 KiB
C#

using Core.SaveLoad;
using Pixelplacement.TweenSystem;
using UnityEngine;
using UnityEngine.EventSystems;
using AppleHills.Core.Settings;
using AppleHills.Data.CardSystem;
using Core;
namespace UI.CardSystem.StateMachine.States
{
/// <summary>
/// Idle state - card back is showing with gentle hover animation.
/// Waiting for click to flip and reveal.
/// Based on FlippableCard's idle behavior.
/// </summary>
public class CardIdleState : AppleState, ICardClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
[Header("State-Owned Visuals")]
[SerializeField] private GameObject cardBackVisual;
[Header("Idle Hover Settings")]
[SerializeField] private bool enableIdleHover = true;
private CardContext _context;
private ICardSystemSettings _settings;
private TweenBase _idleHoverTween;
private Vector2 _originalPosition;
private void Awake()
{
_context = GetComponentInParent<CardContext>();
_settings = GameManager.GetSettingsObject<ICardSystemSettings>();
}
public override void OnEnterState()
{
// Show card back, hide card front
if (cardBackVisual != null)
{
cardBackVisual.SetActive(true);
// Ensure card back is at 0° rotation (facing camera)
cardBackVisual.transform.localRotation = Quaternion.Euler(0, 0, 0);
}
if (_context.CardDisplay != null)
{
_context.CardDisplay.gameObject.SetActive(false);
// Ensure card front starts at 180° rotation (flipped away)
_context.CardDisplay.transform.localRotation = Quaternion.Euler(0, 180, 0);
}
// Save original position for hover animation
RectTransform rectTransform = _context.RootTransform.GetComponent<RectTransform>();
if (rectTransform != null)
{
_originalPosition = rectTransform.anchoredPosition;
}
// Start idle hover animation
if (enableIdleHover && _context.Animator != null)
{
_idleHoverTween = _context.Animator.StartIdleHover(_settings.IdleHoverHeight, _settings.IdleHoverDuration);
}
}
public void OnPointerEnter(PointerEventData eventData)
{
// Scale up slightly on hover
if (_context.Animator != null)
{
_context.Animator.AnimateScale(Vector3.one * _settings.HoverScaleMultiplier, 0.2f);
}
}
public void OnPointerExit(PointerEventData eventData)
{
// Scale back to normal
if (_context.Animator != null)
{
_context.Animator.AnimateScale(Vector3.one, 0.2f);
}
}
public void OnCardClicked(CardContext context)
{
// Check if card is clickable (prevents multi-flip in booster opening)
if (!context.IsClickable)
{
Logging.Debug($"[CardIdleState] Card is not clickable, ignoring click");
return;
}
// Stop idle hover and pointer interactions
StopIdleHover();
// Play flip animation directly
if (context.Animator != null)
{
context.Animator.PlayFlip(
cardBack: cardBackVisual != null ? cardBackVisual.transform : null,
cardFront: context.CardDisplay != null ? context.CardDisplay.transform : null,
onComplete: OnFlipComplete
);
context.Animator.PlayFlipScalePunch();
}
}
public void OnPointerClick(PointerEventData eventData)
{
// Forward to same logic as routed click to keep behavior unified
OnCardClicked(_context);
}
private void OnFlipComplete()
{
// Query current collection state from CardSystemManager (don't use cached values)
bool isNew = Data.CardSystem.CardSystemManager.Instance.IsCardNew(_context.CardData, out CardData existingCard);
// Transition based on whether this is a new card or repeat
if (isNew)
{
// New card - show "NEW" badge and enlarge
_context.StateMachine.ChangeState(CardStateNames.EnlargedNew);
}
else if (_context.CardData != null && _context.CardData.Rarity == AppleHills.Data.CardSystem.CardRarity.Legendary)
{
// Legendary repeat - skip enlarge, they can't upgrade
// Add to inventory and move to revealed state
if (Data.CardSystem.CardSystemManager.Instance != null)
{
Data.CardSystem.CardSystemManager.Instance.AddCardToInventoryDelayed(_context.CardData);
}
_context.StateMachine.ChangeState(CardStateNames.Revealed);
}
else
{
// Repeat card - show progress toward upgrade
_context.StateMachine.ChangeState(CardStateNames.EnlargedRepeat);
}
}
private void StopIdleHover()
{
if (_idleHoverTween != null)
{
_idleHoverTween.Stop();
_idleHoverTween = null;
// Return to original position
RectTransform rectTransform = _context.RootTransform.GetComponent<RectTransform>();
if (rectTransform != null && _context.Animator != null)
{
_context.Animator.AnimateAnchoredPosition(_originalPosition, 0.3f);
}
}
}
private void OnDisable()
{
// Stop idle hover animation when leaving state
StopIdleHover();
// Reset scale
if (_context?.Animator != null)
{
_context.Animator.AnimateScale(Vector3.one, 0.2f);
}
// Hide card back when leaving state
if (cardBackVisual != null)
{
cardBackVisual.SetActive(false);
}
}
}
}