155 lines
5.9 KiB
C#
155 lines
5.9 KiB
C#
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);
|
|
// Play card drop in album sound
|
|
AudioManager.Instance.LoadAndPlayUIAudio("card_albumdrop", false);
|
|
}
|
|
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");
|
|
AudioManager.Instance.LoadAndPlayUIAudio("card_albumdrop", false);
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
}
|
|
}
|
|
|