Updates to card testing scene

This commit is contained in:
Michal Pikulski
2025-11-12 14:39:38 +01:00
committed by Michal Pikulski
parent 4e7f774386
commit 755082c67d
26 changed files with 1336 additions and 841 deletions

View File

@@ -1,5 +1,7 @@
using AppleHills.Data.CardSystem;
using Core;
using Core.SaveLoad;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace UI.CardSystem.StateMachine
@@ -7,9 +9,10 @@ namespace UI.CardSystem.StateMachine
/// <summary>
/// Main Card controller component.
/// Orchestrates the card state machine, context, and animator.
/// Inherits from DraggableObject to provide drag/drop capabilities for album placement.
/// This is the single entry point for working with cards.
/// </summary>
public class Card : MonoBehaviour
public class Card : DraggableObject
{
[Header("Components")]
[SerializeField] private CardContext context;
@@ -25,8 +28,10 @@ namespace UI.CardSystem.StateMachine
public AppleMachine StateMachine => stateMachine;
public CardData CardData => context?.CardData;
private void Awake()
protected override void Initialize()
{
base.Initialize(); // Call DraggableObject initialization
// Auto-find components if not assigned
if (context == null)
context = GetComponent<CardContext>();
@@ -38,6 +43,44 @@ namespace UI.CardSystem.StateMachine
stateMachine = GetComponentInChildren<AppleMachine>();
}
#region DraggableObject Hooks - Trigger State Transitions
protected override void OnDragStartedHook()
{
base.OnDragStartedHook();
// Transition to dragging state when drag begins
Logging.Debug($"[Card] Drag started on {CardData?.Name}, transitioning to DraggingState");
ChangeState("DraggingState");
}
protected override void OnDragEndedHook()
{
base.OnDragEndedHook();
// Check if we dropped in a valid album slot
if (CurrentSlot is AlbumCardSlot albumSlot)
{
Logging.Debug($"[Card] Dropped in album slot, transitioning to PlacedInSlotState");
// Set the parent slot on PlacedInSlotState
var placedState = GetStateComponent<States.CardPlacedInSlotState>("PlacedInSlotState");
if (placedState != null)
{
placedState.SetParentSlot(albumSlot);
}
ChangeState("PlacedInSlotState");
}
else
{
Logging.Debug($"[Card] Dropped outside valid slot, returning to RevealedState");
ChangeState("RevealedState");
}
}
#endregion
/// <summary>
/// Setup the card with data and optional initial state
/// </summary>
@@ -58,18 +101,32 @@ namespace UI.CardSystem.StateMachine
/// <summary>
/// Setup for booster reveal flow (starts at IdleState, will flip on click)
/// Dragging is DISABLED for booster cards
/// </summary>
public void SetupForBoosterReveal(CardData data, bool isNew)
{
SetupCard(data, isNew, "IdleState");
SetDraggingEnabled(false); // Booster cards cannot be dragged
}
/// <summary>
/// Setup for album placement flow (starts at RevealedState, can be dragged)
/// Dragging is ENABLED for album placement cards
/// </summary>
public void SetupForAlbumPlacement(CardData data)
{
SetupCard(data, false, "RevealedState");
SetDraggingEnabled(true); // Album placement cards can be dragged
}
/// <summary>
/// Setup for album placement (starts at PlacedInSlotState)
/// Dragging is DISABLED once placed in slot
/// </summary>
public void SetupForAlbumSlot(CardData data, AlbumCardSlot slot)
{
SetupCard(data, false, "PlacedInSlotState");
SetDraggingEnabled(false); // Cards in slots cannot be dragged out
// Set the parent slot on the PlacedInSlotState
var placedState = GetStateComponent<States.CardPlacedInSlotState>("PlacedInSlotState");

View File

@@ -6,15 +6,15 @@ using AppleHills.Core.Settings;
namespace UI.CardSystem.StateMachine.States
{
/// <summary>
/// Dragging state - handles card being dragged for album placement.
/// Integrates with existing drag/drop system.
/// Dragging state - provides visual feedback when card is being dragged.
/// The actual drag logic is handled by Card.cs (inherits from DraggableObject).
/// This state only manages visual scaling during drag.
/// </summary>
public class CardDraggingState : AppleState
{
private CardContext _context;
private ICardSystemSettings _settings;
private Vector3 _originalScale;
private Vector3 _dragStartPosition;
private void Awake()
{
@@ -24,39 +24,14 @@ namespace UI.CardSystem.StateMachine.States
public override void OnEnterState()
{
// Store original transform
// Store original scale
_originalScale = _context.RootTransform.localScale;
_dragStartPosition = _context.RootTransform.position;
// Scale up slightly during drag for visual feedback
// DraggableObject handles actual position updates
_context.RootTransform.localScale = _originalScale * _settings.DragScale;
Logging.Debug($"[CardDraggingState] Entered drag state for card: {_context.CardData?.Name}");
}
/// <summary>
/// Update drag position (called by external drag handler)
/// </summary>
public void UpdateDragPosition(Vector3 worldPosition)
{
_context.RootTransform.position = worldPosition;
}
/// <summary>
/// Called when drag is released and card snaps to slot
/// </summary>
public void OnDroppedInSlot()
{
_context.StateMachine.ChangeState("PlacedInSlotState");
}
/// <summary>
/// Called when drag is released but not over valid slot
/// </summary>
public void OnDroppedOutsideSlot()
{
// Return to revealed state
_context.StateMachine.ChangeState("RevealedState");
Logging.Debug($"[CardDraggingState] Entered drag state for card: {_context.CardData?.Name}, scale: {_settings.DragScale}");
}
private void OnDisable()

View File

@@ -1,65 +0,0 @@
using UnityEngine;
using UnityEngine.EventSystems;
namespace UI.CardSystem.StateMachine.States
{
/// <summary>
/// Optional helper component for handling drag/drop integration with existing DragDrop system.
/// Can be attached to Card root to bridge between state machine and drag system.
/// </summary>
public class CardInteractionHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private CardContext _context;
private CardDraggingState _draggingState;
private bool _isDragging;
private void Awake()
{
_context = GetComponent<CardContext>();
}
public void OnBeginDrag(PointerEventData eventData)
{
// Only allow drag from certain states
var currentState = _context.StateMachine.currentState?.name;
if (currentState != "RevealedState" && currentState != "PlacedInSlotState")
{
return;
}
// Transition to dragging state
_context.StateMachine.ChangeState("DraggingState");
_draggingState = _context.StateMachine.currentState?.GetComponent<CardDraggingState>();
_isDragging = true;
}
public void OnDrag(PointerEventData eventData)
{
if (!_isDragging || _draggingState == null) return;
// Update drag position
Vector3 worldPosition;
RectTransformUtility.ScreenPointToWorldPointInRectangle(
transform as RectTransform,
eventData.position,
eventData.pressEventCamera,
out worldPosition
);
_draggingState.UpdateDragPosition(worldPosition);
}
public void OnEndDrag(PointerEventData eventData)
{
if (!_isDragging || _draggingState == null) return;
_isDragging = false;
// Check if dropped over a valid slot
// This would integrate with your existing AlbumCardSlot system
// For now, just return to revealed state
_draggingState.OnDroppedOutsideSlot();
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 860a5494378b465a9cc05b2b3d585bf9
timeCreated: 1762884900