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

127 lines
5.3 KiB
C#
Raw Normal View History

2025-11-11 20:25:23 +01:00
using Core;
using Core.SaveLoad;
using UnityEngine;
2025-11-12 09:24:27 +01:00
using AppleHills.Core.Settings;
2025-11-17 23:49:37 +01:00
using Pixelplacement;
2025-11-11 20:25:23 +01:00
namespace UI.CardSystem.StateMachine.States
{
/// <summary>
/// Album enlarged state - card is enlarged when clicked from album slot.
/// Different from EnlargedNewState as it doesn't show "NEW" badge.
/// </summary>
2025-11-16 20:35:54 +01:00
public class CardAlbumEnlargedState : AppleState, ICardClickHandler
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;
private Transform _originalParent;
private Vector3 _originalLocalPosition;
private Quaternion _originalLocalRotation;
2025-11-17 23:49:37 +01:00
private Vector3 _originalWorldPosition;
2025-11-11 20:25:23 +01:00
// Events for page to manage backdrop and reparenting
public event System.Action<CardAlbumEnlargedState> OnEnlargeRequested;
public event System.Action<CardAlbumEnlargedState> OnShrinkRequested;
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()
{
2025-11-12 20:26:51 +01:00
// 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);
}
2025-11-11 20:25:23 +01:00
// Store original transform for restoration
_originalScale = _context.RootTransform.localScale;
_originalParent = _context.RootTransform.parent;
_originalLocalPosition = _context.RootTransform.localPosition;
_originalLocalRotation = _context.RootTransform.localRotation;
2025-11-17 23:49:37 +01:00
_originalWorldPosition = _context.RootTransform.position;
2025-11-11 20:25:23 +01:00
2025-11-17 23:49:37 +01:00
// Notify page to show backdrop and reparent card to top layer (preserve world transform)
2025-11-11 20:25:23 +01:00
OnEnlargeRequested?.Invoke(this);
2025-11-17 23:49:37 +01:00
// Animate into center and scale up significantly
2025-11-11 20:25:23 +01:00
if (_context.Animator != null)
{
2025-11-17 23:49:37 +01:00
// Move to center of enlarged container (assumes zero is centered)
_context.Animator.AnimateLocalPosition(Vector3.zero, _settings.ScaleDuration);
// Enlarge using settings-controlled scale
_context.Animator.PlayEnlarge(_settings.AlbumCardEnlargedScale, _settings.ScaleDuration);
2025-11-11 20:25:23 +01:00
}
Logging.Debug($"[CardAlbumEnlargedState] Card enlarged from album: {_context.CardData?.Name}");
}
2025-11-16 20:35:54 +01:00
public void OnCardClicked(CardContext context)
2025-11-11 20:25:23 +01:00
{
2025-11-17 23:49:37 +01:00
// Click to shrink back (play reverse of opening animation)
2025-11-11 20:25:23 +01:00
Logging.Debug($"[CardAlbumEnlargedState] Card clicked while enlarged, shrinking back");
2025-11-17 23:49:37 +01:00
// Ask page to hide backdrop (state will handle moving back & reparenting)
2025-11-11 20:25:23 +01:00
OnShrinkRequested?.Invoke(this);
2025-11-17 23:49:37 +01:00
// Animate back to original world position + shrink to original scale
float duration = _settings.ScaleDuration;
// Move using world-space tween so we land exactly on the original position
Tween.Position(context.RootTransform, _originalWorldPosition, duration, 0f, Tween.EaseInOut);
2025-11-16 20:35:54 +01:00
if (context.Animator != null)
2025-11-11 20:25:23 +01:00
{
2025-11-17 23:49:37 +01:00
context.Animator.PlayShrink(_originalScale, duration, onComplete: () =>
2025-11-11 20:25:23 +01:00
{
2025-11-17 23:49:37 +01:00
// Reparent back to original hierarchy and restore local transform
context.RootTransform.SetParent(_originalParent, true);
context.RootTransform.localPosition = _originalLocalPosition;
context.RootTransform.localRotation = _originalLocalRotation;
// Transition back to placed state
context.StateMachine.ChangeState(CardStateNames.PlacedInSlot);
2025-11-11 20:25:23 +01:00
});
}
2025-11-16 20:35:54 +01:00
else
{
2025-11-17 23:49:37 +01:00
// Fallback if no animator: snap back
context.RootTransform.position = _originalWorldPosition;
context.RootTransform.SetParent(_originalParent, true);
context.RootTransform.localPosition = _originalLocalPosition;
context.RootTransform.localRotation = _originalLocalRotation;
context.StateMachine.ChangeState(CardStateNames.PlacedInSlot);
2025-11-16 20:35:54 +01:00
}
2025-11-11 20:25:23 +01:00
}
/// <summary>
/// Get original parent for restoration
/// </summary>
public Transform GetOriginalParent() => _originalParent;
/// <summary>
/// Get original local position for restoration
/// </summary>
public Vector3 GetOriginalLocalPosition() => _originalLocalPosition;
/// <summary>
/// Get original local rotation for restoration
/// </summary>
public Quaternion GetOriginalLocalRotation() => _originalLocalRotation;
private void OnDisable()
{
// Restore original scale when exiting
if (_context?.RootTransform != null)
{
_context.RootTransform.localScale = _originalScale;
}
}
}
}