using AppleHills.Data.CardSystem; using Core.SaveLoad; using UnityEngine; namespace UI.CardSystem.StateMachine { /// /// Main Card controller component. /// Orchestrates the card state machine, context, and animator. /// This is the single entry point for working with cards. /// public class Card : MonoBehaviour { [Header("Components")] [SerializeField] private CardContext context; [SerializeField] private CardAnimator animator; [SerializeField] private AppleMachine stateMachine; [Header("Configuration")] [SerializeField] private string initialState = "IdleState"; // Public accessors public CardContext Context => context; public CardAnimator Animator => animator; public AppleMachine StateMachine => stateMachine; public CardData CardData => context?.CardData; private void Awake() { // Auto-find components if not assigned if (context == null) context = GetComponent(); if (animator == null) animator = GetComponent(); if (stateMachine == null) stateMachine = GetComponentInChildren(); } /// /// Setup the card with data and optional initial state /// public void SetupCard(CardData data, bool isNew = false, string startState = null) { if (context != null) { context.SetupCard(data, isNew); } // Start the state machine with specified or default state string targetState = startState ?? initialState; if (stateMachine != null && !string.IsNullOrEmpty(targetState)) { stateMachine.ChangeState(targetState); } } /// /// Setup for booster reveal flow (starts at IdleState, will flip on click) /// public void SetupForBoosterReveal(CardData data, bool isNew) { SetupCard(data, isNew, "IdleState"); } /// /// Setup for album placement (starts at PlacedInSlotState) /// public void SetupForAlbumSlot(CardData data, AlbumCardSlot slot) { SetupCard(data, false, "PlacedInSlotState"); // Set the parent slot on the PlacedInSlotState var placedState = GetStateComponent("PlacedInSlotState"); if (placedState != null) { placedState.SetParentSlot(slot); } } /// /// Transition to a specific state /// public void ChangeState(string stateName) { if (stateMachine != null) { stateMachine.ChangeState(stateName); } } /// /// Get a specific state component by name /// public T GetStateComponent(string stateName) where T : AppleState { if (stateMachine == null) return null; Transform stateTransform = stateMachine.transform.Find(stateName); if (stateTransform != null) { return stateTransform.GetComponent(); } return null; } /// /// Get the current active state name /// public string GetCurrentStateName() { if (stateMachine?.currentState != null) { return stateMachine.currentState.name; } return "None"; } } }