Files
AppleHillsProduction/Assets/Scripts/CardSystem/StateMachine/States/CardDraggingRevealedState.cs

170 lines
6.0 KiB
C#
Raw Normal View History

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: https://homelab.tailf7f81b.ts.net/tschesky/AppleHillsProduction/pulls/59
2025-11-18 08:40:59 +00:00
using Core;
using Core.SaveLoad;
using UnityEngine;
namespace UI.CardSystem.StateMachine.States
{
/// <summary>
/// Dragging revealed state for pending cards after flip.
/// Shows card front without badges, handles transition to placement after drag release.
/// Queries AlbumViewPage for page flip status instead of tracking state internally.
/// </summary>
public class CardDraggingRevealedState : AppleState, ICardStateDragHandler
{
private CardContext _context;
private Vector3 _originalScale;
// Placement info passed from PendingFaceDownState
private AlbumCardSlot _targetSlot;
private bool _dragAlreadyEnded = false; // Track if drag ended before we entered this state (instant-release case)
private void Awake()
{
_context = GetComponentInParent<CardContext>();
}
/// <summary>
/// Set target slot from previous state
/// Called by PendingFaceDownState before transition
/// </summary>
public void SetTargetSlot(AlbumCardSlot targetSlot)
{
_targetSlot = targetSlot;
}
/// <summary>
/// Set flag indicating drag already ended before entering this state
/// Called by PendingFaceDownState for instant-release case
/// </summary>
public void SetDragAlreadyEnded(bool ended)
{
_dragAlreadyEnded = ended;
}
public override void OnEnterState()
{
if (_context == null) return;
if (_context.CardDisplay != null)
{
_context.CardDisplay.gameObject.SetActive(true);
_context.CardDisplay.transform.localRotation = Quaternion.Euler(0,0,0);
}
_originalScale = _context.RootTransform.localScale;
_context.RootTransform.localScale = _originalScale * 1.15f;
// Check if drag already ended before we entered this state (instant-release case)
if (_dragAlreadyEnded)
{
Logging.Debug("[CardDraggingRevealedState] Drag ended before state entry - handling placement immediately");
_dragAlreadyEnded = false; // Clear flag
HandlePlacement();
}
}
/// <summary>
/// Already in dragging state, nothing to do
/// </summary>
public bool OnCardDragStarted(CardContext ctx)
{
return true; // Prevent default DraggingState transition
}
/// <summary>
/// Handle drag end - query AlbumViewPage for page flip status and place accordingly
/// </summary>
public bool OnCardDragEnded(CardContext ctx)
{
HandlePlacement();
return true;
}
/// <summary>
/// Handle card placement logic - called from OnCardDragEnded or OnEnterState (instant-release)
/// </summary>
private void HandlePlacement()
{
if (_targetSlot == null)
{
Logging.Warning("[CardDraggingRevealedState] No target slot set - cannot place card");
// Return to corner
_context.StateMachine.ChangeState(CardStateNames.PendingFaceDown);
return;
}
// Query AlbumViewPage for page flip status
var albumPage = _context.AlbumViewPage;
if (albumPage == null)
{
Logging.Warning("[CardDraggingRevealedState] AlbumViewPage not injected - placing immediately");
TransitionToPlacement(_context);
return;
}
// Check if page is still flipping
if (albumPage.IsPageFlipping)
{
// Wait for flip to complete
Logging.Debug("[CardDraggingRevealedState] Page still flipping - waiting before placement");
StartCoroutine(WaitForPageFlipThenPlace(_context, albumPage));
}
else
{
// Flip already done - place immediately
Logging.Debug("[CardDraggingRevealedState] Page flip complete - placing card immediately");
TransitionToPlacement(_context);
}
}
private System.Collections.IEnumerator WaitForPageFlipThenPlace(CardContext ctx, AlbumViewPage albumPage)
{
// Wait until page flip completes (max 0.5 seconds timeout)
float timeout = 0.5f;
float elapsed = 0f;
while (albumPage.IsPageFlipping && elapsed < timeout)
{
yield return null;
elapsed += Time.deltaTime;
}
if (elapsed >= timeout)
{
Logging.Warning("[CardDraggingRevealedState] Page flip wait timed out");
}
else
{
Logging.Debug("[CardDraggingRevealedState] Page flip completed, placing card");
}
// Now place the card
TransitionToPlacement(ctx);
}
private void TransitionToPlacement(CardContext ctx)
{
// Pass target slot to PlacedInSlotState
var card = ctx.GetComponent<Card>();
if (card != null)
{
var placedState = card.GetStateComponent<CardPlacedInSlotState>(CardStateNames.PlacedInSlot);
if (placedState != null)
{
placedState.SetPlacementInfo(_targetSlot);
}
}
// Transition to PlacedInSlotState
// The state will handle animation and finalization in OnEnterState
ctx.StateMachine.ChangeState(CardStateNames.PlacedInSlot);
}
private void OnDisable()
{
if (_context?.RootTransform != null)
{
_context.RootTransform.localScale = _originalScale;
}
}
}
}