Files
AppleHillsProduction/Assets/Scripts/UI/CardSystem/Testing/CardTestController.cs
2025-11-15 20:37:02 +01:00

386 lines
12 KiB
C#

using System.Collections.Generic;
using AppleHills.Data.CardSystem;
using Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UI.CardSystem.StateMachine;
using UI.CardSystem.StateMachine.States;
namespace UI.CardSystem.Testing
{
/// <summary>
/// Test controller for card state machine testing.
/// Provides UI controls to manually test state transitions, animations, and flows.
/// </summary>
public class CardTestController : MonoBehaviour
{
[Header("Test Card")]
[SerializeField] private Card testCard;
[SerializeField] private CardData testCardData;
[Header("Album Slots for Drag Testing")]
[SerializeField] private AlbumCardSlot slot1;
[SerializeField] private AlbumCardSlot slot2;
[Header("UI References")]
[SerializeField] private TextMeshProUGUI eventLogText;
[SerializeField] private Toggle isNewToggle;
[SerializeField] private Slider repeatCountSlider;
[SerializeField] private TextMeshProUGUI repeatCountLabel;
[SerializeField] private TMP_Dropdown rarityDropdown;
[SerializeField] private Toggle isClickableToggle;
[SerializeField] private TextMeshProUGUI currentStateText;
private List<string> _eventLog = new List<string>();
private CardContext _cardContext;
private Vector3 _originalCardPosition;
private void Awake()
{
if (testCard != null)
{
_cardContext = testCard.GetComponent<CardContext>();
_originalCardPosition = testCard.transform.position;
// Subscribe to all card events
if (_cardContext != null)
{
_cardContext.OnFlipComplete += OnCardFlipComplete;
_cardContext.OnCardDismissed += OnCardDismissed;
_cardContext.OnCardInteractionComplete += OnCardInteractionComplete;
_cardContext.OnUpgradeTriggered += OnCardUpgradeTriggered;
}
}
// Setup UI listeners
if (repeatCountSlider != null)
{
repeatCountSlider.onValueChanged.AddListener(OnRepeatCountChanged);
}
if (isClickableToggle != null)
{
isClickableToggle.onValueChanged.AddListener(OnIsClickableToggled);
}
}
private void Start()
{
// Initialize card with test data
if (testCard != null && testCardData != null && _cardContext != null)
{
_cardContext.SetupCard(testCardData, isNew: true, repeatCount: 0);
}
LogEvent("Card Test Scene Initialized");
UpdateCurrentStateDisplay();
}
private void Update()
{
// Update current state display every frame
if (Time.frameCount % 30 == 0) // Every 0.5 seconds at 60fps
{
UpdateCurrentStateDisplay();
}
}
#region State Transition Buttons
public void TransitionToIdleState()
{
_cardContext?.StateMachine.ChangeState("IdleState");
LogEvent("Transitioned to IdleState");
}
public void TransitionToRevealedState()
{
_cardContext?.StateMachine.ChangeState("RevealedState");
LogEvent("Transitioned to RevealedState");
}
public void TransitionToEnlargedNewState()
{
_cardContext?.StateMachine.ChangeState("EnlargedNewState");
LogEvent("Transitioned to EnlargedNewState");
}
public void TransitionToEnlargedRepeatState()
{
_cardContext?.StateMachine.ChangeState("EnlargedRepeatState");
LogEvent("Transitioned to EnlargedRepeatState");
}
public void TransitionToDraggingState()
{
_cardContext?.StateMachine.ChangeState("DraggingState");
LogEvent("Transitioned to DraggingState");
}
public void TransitionToPlacedInSlotState()
{
_cardContext?.StateMachine.ChangeState("PlacedInSlotState");
LogEvent("Transitioned to PlacedInSlotState");
}
public void TransitionToAlbumEnlargedState()
{
_cardContext?.StateMachine.ChangeState("AlbumEnlargedState");
LogEvent("Transitioned to AlbumEnlargedState");
}
#endregion
#region Simulation Buttons
public void SimulateNewCardFlow()
{
if (_cardContext == null) return;
_cardContext.IsNewCard = true;
_cardContext.RepeatCardCount = 0;
_cardContext.IsClickable = true;
TransitionToIdleState();
LogEvent("Simulating NEW CARD flow - click card to flip");
}
public void SimulateRepeatCardFlow()
{
if (_cardContext == null) return;
int repeatCount = Mathf.RoundToInt(repeatCountSlider.value);
_cardContext.IsNewCard = false;
_cardContext.RepeatCardCount = repeatCount;
_cardContext.IsClickable = true;
TransitionToIdleState();
LogEvent($"Simulating REPEAT CARD flow ({repeatCount}/5) - click card to flip");
}
public void SimulateUpgradeFlow()
{
if (_cardContext == null) return;
_cardContext.IsNewCard = false;
_cardContext.RepeatCardCount = 5; // Trigger upgrade
_cardContext.IsClickable = true;
TransitionToIdleState();
LogEvent("Simulating UPGRADE flow (5/5) - click card to flip and auto-upgrade");
}
public void SimulateAlbumPlacementFlow()
{
if (_cardContext == null) return;
_cardContext.IsNewCard = false;
_cardContext.RepeatCardCount = 0;
TransitionToRevealedState();
LogEvent("Simulating ALBUM PLACEMENT - drag card to slot");
}
#endregion
#region Card Setup Controls
public void ApplyCardSetup()
{
if (_cardContext == null) return;
bool isNew = isNewToggle != null && isNewToggle.isOn;
int repeatCount = repeatCountSlider != null ? Mathf.RoundToInt(repeatCountSlider.value) : 0;
_cardContext.IsNewCard = isNew;
_cardContext.RepeatCardCount = repeatCount;
// Apply rarity if needed
if (rarityDropdown != null && testCardData != null)
{
testCardData.Rarity = (CardRarity)rarityDropdown.value;
}
LogEvent($"Card setup applied: IsNew={isNew}, RepeatCount={repeatCount}");
}
private void OnRepeatCountChanged(float value)
{
if (repeatCountLabel != null)
{
repeatCountLabel.text = $"{Mathf.RoundToInt(value)}/5";
}
}
private void OnIsClickableToggled(bool isClickable)
{
if (_cardContext != null)
{
_cardContext.IsClickable = isClickable;
LogEvent($"Card clickable: {isClickable}");
}
}
#endregion
#region Animation Test Buttons
public void PlayFlipAnimation()
{
// Transition to IdleState and programmatically trigger flip
TransitionToIdleState();
// Get IdleState and trigger click
var idleState = testCard.GetComponentInChildren<CardIdleState>();
if (idleState != null)
{
idleState.OnPointerClick(null);
LogEvent("Playing flip animation");
}
}
public void PlayEnlargeAnimation()
{
if (_cardContext?.Animator != null)
{
_cardContext.Animator.PlayEnlarge(2.5f);
LogEvent("Playing enlarge animation");
}
}
public void PlayShrinkAnimation()
{
if (_cardContext?.Animator != null)
{
_cardContext.Animator.PlayShrink(Vector3.one, null);
LogEvent("Playing shrink animation");
}
}
public void StartIdleHoverAnimation()
{
if (_cardContext?.Animator != null)
{
_cardContext.Animator.StartIdleHover(10f, 1.5f);
LogEvent("Started idle hover animation");
}
}
public void StopIdleHoverAnimation()
{
// Stopping hover is handled by IdleState's OnDisable
LogEvent("Idle hover stopped (change state to stop)");
}
#endregion
#region Utility Buttons
public void ResetCardPosition()
{
if (testCard != null)
{
testCard.transform.position = _originalCardPosition;
testCard.transform.localScale = Vector3.one;
LogEvent("Card position reset");
}
}
public void ClearEventLog()
{
_eventLog.Clear();
UpdateEventLog();
LogEvent("Event log cleared");
}
#endregion
#region Event Handlers
private void OnCardFlipComplete(CardContext context)
{
LogEvent($"Event: OnFlipComplete - IsNew={context.IsNewCard}, RepeatCount={context.RepeatCardCount}");
}
private void OnCardDismissed(CardContext context)
{
LogEvent("Event: OnCardDismissed");
}
private void OnCardInteractionComplete(CardContext context)
{
LogEvent("Event: OnCardInteractionComplete");
}
private void OnCardUpgradeTriggered(CardContext context)
{
LogEvent($"Event: OnUpgradeTriggered - New Rarity={context.CardData?.Rarity}");
}
#endregion
#region Event Log
private void LogEvent(string message)
{
string timestamp = $"[{Time.time:F2}s]";
_eventLog.Add($"{timestamp} {message}");
// Keep only last 20 events
if (_eventLog.Count > 20)
{
_eventLog.RemoveAt(0);
}
UpdateEventLog();
Logging.Debug($"[CardTest] {message}");
}
private void UpdateEventLog()
{
if (eventLogText != null)
{
eventLogText.text = string.Join("\n", _eventLog);
}
}
private void UpdateCurrentStateDisplay()
{
if (currentStateText != null && _cardContext != null && _cardContext.StateMachine != null)
{
// Get the active state by checking which child state GameObject is active
string stateName = "Unknown";
Transform stateMachineTransform = _cardContext.StateMachine.transform;
for (int i = 0; i < stateMachineTransform.childCount; i++)
{
Transform child = stateMachineTransform.GetChild(i);
if (child.gameObject.activeSelf)
{
stateName = child.name;
break;
}
}
currentStateText.text = $"Current State: {stateName}";
}
}
#endregion
private void OnDestroy()
{
// Unsubscribe from events
if (_cardContext != null)
{
_cardContext.OnFlipComplete -= OnCardFlipComplete;
_cardContext.OnCardDismissed -= OnCardDismissed;
_cardContext.OnCardInteractionComplete -= OnCardInteractionComplete;
_cardContext.OnUpgradeTriggered -= OnCardUpgradeTriggered;
}
}
}
}