Working refactor of cleaned up card structure

This commit is contained in:
Michal Pikulski
2025-11-18 01:03:45 +01:00
parent 689e177c99
commit 64c304bb6d
18 changed files with 163 additions and 73 deletions

View File

@@ -824,6 +824,10 @@ PrefabInstance:
propertyPath: m_SizeDelta.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2685537002028647152, guid: 88a05fdd940194543ade1cc2bcdada5f, type: 3}
propertyPath: interactable
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2717636461124059971, guid: 88a05fdd940194543ade1cc2bcdada5f, type: 3}
propertyPath: m_SizeDelta.x
value: 0

View File

@@ -603,6 +603,9 @@ namespace UI.CardSystem
// IMPORTANT: Assign to slot FIRST (establishes correct transform)
card.AssignToSlot(slot, false); // false = instant, no animation
// Inject AlbumViewPage dependency
card.Context.SetAlbumViewPage(this);
// THEN setup card for pending state (transitions to PendingFaceDownState)
// This ensures OriginalScale is captured AFTER slot assignment
card.SetupForAlbumPending();

View File

@@ -584,8 +584,8 @@ namespace UI.CardSystem
// Subscribe to CardDisplay click for selection
context.CardDisplay.OnCardClicked += (_) => OnCardClicked(card);
// Subscribe to reveal flow complete event
context.OnRevealFlowComplete += (ctx) => OnCardRevealComplete(card);
// Subscribe to reveal flow complete event from booster context
context.BoosterContext.OnRevealFlowComplete += () => OnCardRevealComplete(card);
// Track the card
_currentCards.Add(card);

View File

@@ -0,0 +1,51 @@
using System;
namespace UI.CardSystem.StateMachine
{
/// <summary>
/// Booster-specific card context for reveal flow coordination.
/// Separated from generic CardContext to maintain single responsibility.
/// </summary>
public class BoosterCardContext
{
// Booster reveal workflow state
private bool _hasCompletedReveal = false;
/// <summary>
/// Has this card completed its booster reveal flow?
/// </summary>
public bool HasCompletedReveal => _hasCompletedReveal;
/// <summary>
/// Suppress NEW/REPEAT badges in revealed state
/// </summary>
public bool SuppressRevealBadges { get; set; } = false;
/// <summary>
/// Event fired when reveal flow is complete (card dismissed)
/// </summary>
public event Action OnRevealFlowComplete;
/// <summary>
/// Signal that this card has completed its reveal flow
/// </summary>
public void NotifyRevealComplete()
{
if (!_hasCompletedReveal)
{
_hasCompletedReveal = true;
OnRevealFlowComplete?.Invoke();
}
}
/// <summary>
/// Reset reveal state (for card reuse/pooling)
/// </summary>
public void Reset()
{
_hasCompletedReveal = false;
SuppressRevealBadges = false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 484a792a835a418bb690947b82e45da7
timeCreated: 1763421968

View File

@@ -24,9 +24,9 @@ namespace UI.CardSystem.StateMachine
public CardData CardData => context?.CardData;
// State inspection properties for booster flow
public bool IsIdle => GetCurrentStateName() == "IdleState";
public bool IsIdle => GetCurrentStateName() == CardStateNames.Idle;
public bool IsRevealing => !IsIdle && !IsComplete;
public bool IsComplete => context?.HasCompletedReveal ?? false;
public bool IsComplete => context?.BoosterContext.HasCompletedReveal ?? false;
// Event fired when this card is successfully placed into an AlbumCardSlot
public event System.Action<Card, AlbumCardSlot> OnPlacedInAlbumSlot;
@@ -89,23 +89,23 @@ namespace UI.CardSystem.StateMachine
// Default behavior for states that don't implement custom drag end
string current = GetCurrentStateName();
if (current == "DraggingState")
if (current == CardStateNames.Dragging)
{
if (CurrentSlot is AlbumCardSlot albumSlot)
{
Logging.Debug($"[Card] Dropped in album slot, transitioning to PlacedInSlotState");
var placedState = GetStateComponent<States.CardPlacedInSlotState>("PlacedInSlotState");
var placedState = GetStateComponent<States.CardPlacedInSlotState>(CardStateNames.PlacedInSlot);
if (placedState != null)
{
placedState.SetParentSlot(albumSlot);
}
ChangeState("PlacedInSlotState");
ChangeState(CardStateNames.PlacedInSlot);
OnPlacedInAlbumSlot?.Invoke(this, albumSlot);
}
else
{
Logging.Debug("[Card] Dropped outside valid slot, returning to RevealedState");
ChangeState("RevealedState");
ChangeState(CardStateNames.Revealed);
}
}
}
@@ -137,7 +137,7 @@ namespace UI.CardSystem.StateMachine
/// </summary>
public void SetupForBoosterReveal(CardData data, bool isNew)
{
SetupCard(data, "IdleState");
SetupCard(data, CardStateNames.Idle);
SetDraggingEnabled(false); // Booster cards cannot be dragged
}
@@ -147,11 +147,11 @@ namespace UI.CardSystem.StateMachine
/// </summary>
public void SetupForAlbumSlot(CardData data, AlbumCardSlot slot)
{
SetupCard(data, "PlacedInSlotState");
SetupCard(data, CardStateNames.PlacedInSlot);
SetDraggingEnabled(false); // Cards in slots cannot be dragged out
// Set the parent slot on the PlacedInSlotState
var placedState = GetStateComponent<States.CardPlacedInSlotState>("PlacedInSlotState");
var placedState = GetStateComponent<States.CardPlacedInSlotState>(CardStateNames.PlacedInSlot);
if (placedState != null)
{
placedState.SetParentSlot(slot);
@@ -165,7 +165,7 @@ namespace UI.CardSystem.StateMachine
public void SetupForAlbumPending()
{
// Start with no data; state will assign when dragged
SetupCard(null, "PendingFaceDownState");
SetupCard(null, CardStateNames.PendingFaceDown);
SetDraggingEnabled(true);
}

View File

@@ -19,8 +19,8 @@ namespace UI.CardSystem.StateMachine
[Header("Card Data")]
private CardData cardData;
// Cached reference to AlbumViewPage (lazy-loaded)
private AlbumViewPage _albumViewPage;
// Injected dependencies
private AlbumViewPage albumViewPage;
// Public accessors
public CardDisplay CardDisplay => cardDisplay;
@@ -30,56 +30,44 @@ namespace UI.CardSystem.StateMachine
public CardData CardData => cardData;
/// <summary>
/// Get the AlbumViewPage instance (cached to avoid repeated FindFirstObjectByType calls)
/// Get the injected AlbumViewPage (null if not set)
/// </summary>
public AlbumViewPage AlbumViewPage
{
get
{
if (_albumViewPage == null)
{
_albumViewPage = FindFirstObjectByType<AlbumViewPage>();
}
return _albumViewPage;
}
}
public AlbumViewPage AlbumViewPage => albumViewPage;
// Runtime state
public bool IsClickable { get; set; } = true;
// TODO: Move to booster-specific states - this is workflow-specific, not generic context
public bool SuppressRevealBadges { get; set; } = false; // Set by states to suppress NEW/REPEAT badges in revealed state
// Original transform data (captured on spawn for shrink animations)
public Vector3 OriginalScale { get; private set; }
public Vector3 OriginalPosition { get; private set; }
public Quaternion OriginalRotation { get; private set; }
// TODO: Move to BoosterOpeningPage - reveal flow is booster-specific workflow coordination
// Single event for reveal flow completion
public event Action<CardContext> OnRevealFlowComplete;
// Generic drag event - fired when drag starts, consumers decide how to handle based on current state
public event Action<CardContext> OnDragStarted;
// Generic drag end event - fired when drag ends, consumers decide how to handle based on current state
public event Action<CardContext> OnDragEnded;
// TODO: Move to booster-specific states - this tracks reveal workflow completion
private bool _hasCompletedReveal = false;
public bool HasCompletedReveal => _hasCompletedReveal;
// TODO: Move to booster-specific states - workflow coordination method
// Helper method for states to signal completion
public void NotifyRevealComplete()
// Booster-specific context (optional, only set for booster flow cards)
private BoosterCardContext boosterContext;
public BoosterCardContext BoosterContext
{
if (!_hasCompletedReveal)
get
{
_hasCompletedReveal = true;
OnRevealFlowComplete?.Invoke(this);
if (boosterContext == null)
boosterContext = new BoosterCardContext();
return boosterContext;
}
}
/// <summary>
/// Inject AlbumViewPage dependency
/// </summary>
public void SetAlbumViewPage(AlbumViewPage page)
{
albumViewPage = page;
}
// Helper method for states/card to signal drag started
public void NotifyDragStarted()
{
@@ -141,7 +129,12 @@ namespace UI.CardSystem.StateMachine
public void SetupCard(CardData data)
{
cardData = data;
_hasCompletedReveal = false; // Reset completion flag
// Reset booster context if it exists
if (boosterContext != null)
{
boosterContext.Reset();
}
// Capture original transform for shrink animations.
// If current scale is ~0 (pop-in staging), default to Vector3.one.
@@ -171,7 +164,12 @@ namespace UI.CardSystem.StateMachine
public void UpdateCardData(CardData data)
{
cardData = data;
_hasCompletedReveal = false; // Reset completion flag
// Reset booster context if it exists
if (boosterContext != null)
{
boosterContext.Reset();
}
// Don't re-capture OriginalScale/Position/Rotation
// This preserves the transform values captured during initial setup

View File

@@ -0,0 +1,26 @@
namespace UI.CardSystem.StateMachine
{
/// <summary>
/// Centralized string constants for card state names.
/// Prevents typos and provides compile-time checking for state transitions.
/// </summary>
public static class CardStateNames
{
// Booster Flow States
public const string Idle = "IdleState";
public const string EnlargedNew = "EnlargedNewState";
public const string EnlargedRepeat = "EnlargedRepeatState";
public const string EnlargedLegendaryRepeat = "EnlargedLegendaryRepeatState";
public const string Revealed = "RevealedState";
// Album Placement Flow States
public const string PendingFaceDown = "PendingFaceDownState";
public const string DraggingRevealed = "DraggingRevealedState";
public const string PlacedInSlot = "PlacedInSlotState";
public const string AlbumEnlarged = "AlbumEnlargedState";
// Generic Drag State
public const string Dragging = "DraggingState";
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: de659f0ceb6f4ef6bab333d5f7de00ec
timeCreated: 1763421948

View File

@@ -85,7 +85,7 @@ namespace UI.CardSystem.StateMachine.States
context.RootTransform.localRotation = _originalLocalRotation;
// Transition back to placed state
context.StateMachine.ChangeState("PlacedInSlotState");
context.StateMachine.ChangeState(CardStateNames.PlacedInSlot);
});
}
else
@@ -95,7 +95,7 @@ namespace UI.CardSystem.StateMachine.States
context.RootTransform.SetParent(_originalParent, true);
context.RootTransform.localPosition = _originalLocalPosition;
context.RootTransform.localRotation = _originalLocalRotation;
context.StateMachine.ChangeState("PlacedInSlotState");
context.StateMachine.ChangeState(CardStateNames.PlacedInSlot);
}
}

View File

@@ -87,7 +87,7 @@ namespace UI.CardSystem.StateMachine.States
{
Logging.Warning("[CardDraggingRevealedState] No target slot set - cannot place card");
// Return to corner
_context.StateMachine.ChangeState("PendingFaceDownState");
_context.StateMachine.ChangeState(CardStateNames.PendingFaceDown);
return;
}
@@ -95,7 +95,7 @@ namespace UI.CardSystem.StateMachine.States
var albumPage = _context.AlbumViewPage;
if (albumPage == null)
{
Logging.Warning("[CardDraggingRevealedState] AlbumViewPage not found - placing immediately");
Logging.Warning("[CardDraggingRevealedState] AlbumViewPage not injected - placing immediately");
TransitionToPlacement(_context);
return;
}
@@ -146,7 +146,7 @@ namespace UI.CardSystem.StateMachine.States
var card = ctx.GetComponent<Card>();
if (card != null)
{
var placedState = card.GetStateComponent<CardPlacedInSlotState>("PlacedInSlotState");
var placedState = card.GetStateComponent<CardPlacedInSlotState>(CardStateNames.PlacedInSlot);
if (placedState != null)
{
placedState.SetPlacementInfo(_targetSlot);
@@ -155,7 +155,7 @@ namespace UI.CardSystem.StateMachine.States
// Transition to PlacedInSlotState
// The state will handle animation and finalization in OnEnterState
ctx.StateMachine.ChangeState("PlacedInSlotState");
ctx.StateMachine.ChangeState(CardStateNames.PlacedInSlot);
}
private void OnDisable()

View File

@@ -58,13 +58,13 @@ namespace UI.CardSystem.StateMachine.States
{
context.Animator.PlayShrink(context.OriginalScale, onComplete: () =>
{
context.StateMachine.ChangeState("RevealedState");
context.StateMachine.ChangeState(CardStateNames.Revealed);
});
}
else
{
// Fallback if no animator
context.StateMachine.ChangeState("RevealedState");
context.StateMachine.ChangeState(CardStateNames.Revealed);
}
}

View File

@@ -162,12 +162,12 @@ namespace UI.CardSystem.StateMachine.States
if (newRarity == CardRarity.Legendary)
{
// Show special enlarged legendary presentation, await click to shrink to revealed
_context.StateMachine.ChangeState("EnlargedLegendaryRepeatState");
_context.StateMachine.ChangeState(CardStateNames.EnlargedLegendaryRepeat);
}
else
{
// Treat as NEW at higher rarity (enlarged with NEW visuals handled there)
_context.StateMachine.ChangeState("EnlargedNewState");
_context.StateMachine.ChangeState(CardStateNames.EnlargedNew);
}
}
}
@@ -182,7 +182,7 @@ namespace UI.CardSystem.StateMachine.States
// Transition to EnlargedNewState (card is already enlarged, will show NEW badge)
// State will query fresh collection data to determine if truly new
_context.StateMachine.ChangeState("EnlargedNewState");
_context.StateMachine.ChangeState(CardStateNames.EnlargedNew);
}
public void OnCardClicked(CardContext context)
@@ -196,7 +196,7 @@ namespace UI.CardSystem.StateMachine.States
{
context.Animator.PlayShrink(context.OriginalScale, onComplete: () =>
{
context.StateMachine.ChangeState("RevealedState");
context.StateMachine.ChangeState(CardStateNames.Revealed);
});
}
else

View File

@@ -121,7 +121,7 @@ namespace UI.CardSystem.StateMachine.States
if (isNew)
{
// New card - show "NEW" badge and enlarge
_context.StateMachine.ChangeState("EnlargedNewState");
_context.StateMachine.ChangeState(CardStateNames.EnlargedNew);
}
else if (_context.CardData != null && _context.CardData.Rarity == AppleHills.Data.CardSystem.CardRarity.Legendary)
{
@@ -131,12 +131,12 @@ namespace UI.CardSystem.StateMachine.States
{
Data.CardSystem.CardSystemManager.Instance.AddCardToInventoryDelayed(_context.CardData);
}
_context.StateMachine.ChangeState("RevealedState");
_context.StateMachine.ChangeState(CardStateNames.Revealed);
}
else
{
// Repeat card - show progress toward upgrade
_context.StateMachine.ChangeState("EnlargedRepeatState");
_context.StateMachine.ChangeState(CardStateNames.EnlargedRepeat);
}
}

View File

@@ -56,11 +56,11 @@ namespace UI.CardSystem.StateMachine.States
{
if (_isFlipping) return true; // Already handling
// Step 1: Find AlbumViewPage
AlbumViewPage albumPage = Object.FindFirstObjectByType<AlbumViewPage>();
// Get AlbumViewPage from context (injected dependency)
var albumPage = context.AlbumViewPage;
if (albumPage == null)
{
Logging.Warning("[CardPendingFaceDownState] AlbumViewPage not found!");
Logging.Warning("[CardPendingFaceDownState] AlbumViewPage not injected!");
return true;
}
@@ -143,11 +143,11 @@ namespace UI.CardSystem.StateMachine.States
private void OnFlipComplete()
{
// Transition to dragging revealed state
// Pass target slot to next state (it will query AlbumViewPage for flip status)
// Pass target slot to next state (it will query AlbumService for flip status)
var card = _context.GetComponent<Card>();
if (card != null)
{
var draggingState = card.GetStateComponent<CardDraggingRevealedState>("DraggingRevealedState");
var draggingState = card.GetStateComponent<CardDraggingRevealedState>(CardStateNames.DraggingRevealed);
if (draggingState != null)
{
draggingState.SetTargetSlot(_targetSlot);
@@ -161,7 +161,7 @@ namespace UI.CardSystem.StateMachine.States
}
}
_context.StateMachine.ChangeState("DraggingRevealedState");
_context.StateMachine.ChangeState(CardStateNames.DraggingRevealed);
}
private void OnDisable()

View File

@@ -1,4 +1,4 @@
using Core;
using Core;
using Core.SaveLoad;
using Data.CardSystem;
using UnityEngine;
@@ -143,7 +143,7 @@ namespace UI.CardSystem.StateMachine.States
{
// Click to enlarge when in album
Logging.Debug($"[CardPlacedInSlotState] Card clicked in slot, transitioning to enlarged state");
context.StateMachine.ChangeState("AlbumEnlargedState");
context.StateMachine.ChangeState(CardStateNames.AlbumEnlarged);
}
}
}

View File

@@ -34,7 +34,7 @@ namespace UI.CardSystem.StateMachine.States
}
// Show appropriate idle badge unless suppressed
if (_context.SuppressRevealBadges)
if (_context.BoosterContext.SuppressRevealBadges)
{
if (newCardIdleBadge != null) newCardIdleBadge.SetActive(false);
if (repeatCardIdleBadge != null) repeatCardIdleBadge.SetActive(false);
@@ -61,7 +61,7 @@ namespace UI.CardSystem.StateMachine.States
}
// Fire reveal flow complete event (signals booster page that this card is done)
_context.NotifyRevealComplete();
_context.BoosterContext.NotifyRevealComplete();
}
private void OnDisable()

View File

@@ -54,7 +54,8 @@ namespace UI.CardSystem.Testing
// Subscribe to card events (new simplified event model)
if (_cardContext != null)
{
_cardContext.OnRevealFlowComplete += OnCardRevealFlowComplete;
// TODO: FIX
// _cardContext.OnRevealFlowComplete += OnCardRevealFlowComplete;
}
// Subscribe to drag events to ensure card snaps back when released
@@ -438,7 +439,8 @@ namespace UI.CardSystem.Testing
// Unsubscribe from events
if (_cardContext != null)
{
_cardContext.OnRevealFlowComplete -= OnCardRevealFlowComplete;
// TODO: FIX
// _cardContext.OnRevealFlowComplete -= OnCardRevealFlowComplete;
}
if (testCard != null)