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
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
using Core;
|
||||
using Core.SaveLoad;
|
||||
using Data.CardSystem;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI.CardSystem.StateMachine.States
|
||||
{
|
||||
/// <summary>
|
||||
/// Placed in slot state - card is being/has been placed in an album slot.
|
||||
/// SMART STATE: Handles snap-to-slot animation on entry, then finalizes placement.
|
||||
/// </summary>
|
||||
public class CardPlacedInSlotState : AppleState, ICardClickHandler
|
||||
{
|
||||
private CardContext _context;
|
||||
private AlbumCardSlot _parentSlot;
|
||||
private AlbumCardSlot _targetSlotForAnimation; // Set by DraggingRevealedState for animated placement
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_context = GetComponentInParent<CardContext>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set placement info from previous state (for animated placement from drag)
|
||||
/// </summary>
|
||||
public void SetPlacementInfo(AlbumCardSlot targetSlot)
|
||||
{
|
||||
_targetSlotForAnimation = targetSlot;
|
||||
}
|
||||
|
||||
public override void OnEnterState()
|
||||
{
|
||||
// Ensure card front is visible and facing camera
|
||||
if (_context.CardDisplay != null)
|
||||
{
|
||||
_context.CardDisplay.gameObject.SetActive(true);
|
||||
_context.CardDisplay.transform.localRotation = Quaternion.Euler(0, 0, 0);
|
||||
}
|
||||
|
||||
// Check if this is animated placement (from drag) or direct placement (from spawn)
|
||||
if (_targetSlotForAnimation != null)
|
||||
{
|
||||
// Animated placement - play tween to slot
|
||||
Logging.Debug($"[CardPlacedInSlotState] Animating card '{_context.CardData?.Name}' to slot");
|
||||
AnimateToSlot(_targetSlotForAnimation);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Direct placement (spawned in slot) - already positioned correctly
|
||||
// Disable dragging for spawned cards too
|
||||
var card = _context.GetComponent<Card>();
|
||||
if (card != null)
|
||||
{
|
||||
card.SetDraggingEnabled(false);
|
||||
}
|
||||
Logging.Debug($"[CardPlacedInSlotState] Card '{_context.CardData?.Name}' directly placed in slot");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Animate card to slot position and finalize placement
|
||||
/// </summary>
|
||||
private void AnimateToSlot(AlbumCardSlot slot)
|
||||
{
|
||||
var card = _context.GetComponent<Card>();
|
||||
if (card == null) return;
|
||||
|
||||
// Reparent to slot immediately, keeping world position
|
||||
card.transform.SetParent(slot.transform, true);
|
||||
|
||||
// Tween position, scale, rotation simultaneously
|
||||
float tweenDuration = 0.4f;
|
||||
|
||||
Pixelplacement.Tween.LocalPosition(card.transform, Vector3.zero, tweenDuration, 0f, Pixelplacement.Tween.EaseOutBack);
|
||||
Pixelplacement.Tween.LocalScale(card.transform, Vector3.one, tweenDuration, 0f, Pixelplacement.Tween.EaseOutBack);
|
||||
Pixelplacement.Tween.LocalRotation(card.transform, Quaternion.identity, tweenDuration, 0f, Pixelplacement.Tween.EaseOutBack,
|
||||
completeCallback: () => FinalizePlacement(card, slot));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finalize placement after animation completes
|
||||
/// </summary>
|
||||
private void FinalizePlacement(Card card, AlbumCardSlot slot)
|
||||
{
|
||||
// Ensure final position/rotation
|
||||
card.transform.localPosition = Vector3.zero;
|
||||
card.transform.localRotation = Quaternion.identity;
|
||||
|
||||
// Resize to match slot
|
||||
RectTransform cardRect = card.transform as RectTransform;
|
||||
RectTransform slotRect = slot.transform as RectTransform;
|
||||
if (cardRect != null && slotRect != null)
|
||||
{
|
||||
float targetHeight = slotRect.rect.height;
|
||||
cardRect.sizeDelta = new Vector2(cardRect.sizeDelta.x, targetHeight);
|
||||
}
|
||||
|
||||
// Set parent slot
|
||||
_parentSlot = slot;
|
||||
|
||||
// Disable dragging - cards in slots should only respond to clicks for enlargement
|
||||
card.SetDraggingEnabled(false);
|
||||
|
||||
// Notify slot
|
||||
slot.AssignCard(card);
|
||||
|
||||
// Mark as placed in inventory
|
||||
if (CardSystemManager.Instance != null)
|
||||
{
|
||||
CardSystemManager.Instance.MarkCardAsPlaced(card.CardData);
|
||||
}
|
||||
|
||||
// Notify AlbumViewPage for registration
|
||||
var albumPage = Object.FindFirstObjectByType<AlbumViewPage>();
|
||||
if (albumPage != null)
|
||||
{
|
||||
albumPage.NotifyCardPlaced(card);
|
||||
}
|
||||
|
||||
Logging.Debug($"[CardPlacedInSlotState] Card placement finalized: {card.CardData?.Name}");
|
||||
|
||||
// Clear animation target
|
||||
_targetSlotForAnimation = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the parent slot this card belongs to (for direct placement without animation)
|
||||
/// </summary>
|
||||
public void SetParentSlot(AlbumCardSlot slot)
|
||||
{
|
||||
_parentSlot = slot;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the parent slot
|
||||
/// </summary>
|
||||
public AlbumCardSlot GetParentSlot()
|
||||
{
|
||||
return _parentSlot;
|
||||
}
|
||||
|
||||
public void OnCardClicked(CardContext context)
|
||||
{
|
||||
// Click to enlarge when in album
|
||||
Logging.Debug($"[CardPlacedInSlotState] Card clicked in slot, transitioning to enlarged state");
|
||||
context.StateMachine.ChangeState(CardStateNames.AlbumEnlarged);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user