Slotting cards in album after revealing

This commit is contained in:
Michal Pikulski
2025-11-07 01:51:03 +01:00
parent debe70c9b1
commit 3e607f3857
20 changed files with 2986 additions and 1459 deletions

View File

@@ -0,0 +1,191 @@
using System;
using System.Collections;
using AppleHills.Data.CardSystem;
using Data.CardSystem;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace UI.CardSystem
{
/// <summary>
/// Draggable card for album reveal system.
/// Handles both tap and drag-hold interactions for revealing cards.
/// Auto-snaps to matching album slot on release/tap.
/// </summary>
public class AlbumCardDraggable : DraggableObject
{
[Header("Album Card Settings")]
[SerializeField] private FlippableCard flippableCard;
[SerializeField] private float holdRevealDelay = 0.1f;
private CardData _cardData;
private bool _isRevealed = false;
private bool _isDragRevealing = false;
private bool _waitingForPlacementTap = false;
private Coroutine _holdRevealCoroutine;
private bool _isHolding = false; // Track if pointer is currently down
// Events
public event Action<AlbumCardDraggable, CardData> OnCardRevealed;
public event Action<AlbumCardDraggable, CardData> OnCardPlacedInAlbum;
public CardData CardData => _cardData;
public bool IsRevealed => _isRevealed;
public CardZone Zone => _cardData?.Zone ?? CardZone.AppleHills;
protected override void Initialize()
{
base.Initialize();
// Auto-find FlippableCard if not assigned
if (flippableCard == null)
{
flippableCard = GetComponent<FlippableCard>();
}
}
/// <summary>
/// Setup the card data (stores it but doesn't reveal until tapped/dragged)
/// </summary>
public void SetupCard(CardData data)
{
_cardData = data;
if (flippableCard != null)
{
flippableCard.SetupCard(data);
}
}
/// <summary>
/// Reveal the card (flip to show front)
/// </summary>
public void RevealCard()
{
if (_isRevealed) return;
_isRevealed = true;
if (flippableCard != null)
{
flippableCard.FlipToReveal();
}
OnCardRevealed?.Invoke(this, _cardData);
}
/// <summary>
/// Snap to the matching album slot
/// </summary>
public void SnapToAlbumSlot()
{
if (_cardData == null)
{
Debug.LogWarning("[AlbumCardDraggable] Cannot snap to slot - no card data assigned.");
return;
}
// Find all album card slots in the scene
AlbumCardSlot[] allSlots = FindObjectsByType<AlbumCardSlot>(FindObjectsSortMode.None);
AlbumCardSlot matchingSlot = null;
foreach (var slot in allSlots)
{
if (slot.CanAcceptCard(_cardData))
{
matchingSlot = slot;
break;
}
}
if (matchingSlot != null)
{
// Assign to slot with animation
AssignToSlot(matchingSlot, true);
// Mark slot as permanently occupied
matchingSlot.OnCardPlaced();
// Disable dragging - card is now static in album
SetDraggingEnabled(false);
// Notify that card was placed
// Note: Card already moved from pending to inventory in OnCardRevealed
OnCardPlacedInAlbum?.Invoke(this, _cardData);
}
else
{
Debug.LogWarning($"[AlbumCardDraggable] Could not find matching slot for card '{_cardData.Name}' (Zone: {_cardData.Zone}, Index: {_cardData.CollectionIndex})");
}
}
protected override void OnPointerDownHook()
{
base.OnPointerDownHook();
_isHolding = true;
// Start hold-reveal timer if card not yet revealed
if (!_isRevealed && _holdRevealCoroutine == null)
{
_holdRevealCoroutine = StartCoroutine(HoldRevealTimer());
}
}
protected override void OnPointerUpHook(bool longPress)
{
base.OnPointerUpHook(longPress);
_isHolding = false;
// Cancel hold timer if running
if (_holdRevealCoroutine != null)
{
StopCoroutine(_holdRevealCoroutine);
_holdRevealCoroutine = null;
}
// Handle tap (not dragged)
if (!_wasDragged)
{
if (!_isRevealed)
{
// First tap: reveal the card
RevealCard();
_waitingForPlacementTap = true;
}
else if (_waitingForPlacementTap)
{
// Second tap: snap to slot
_waitingForPlacementTap = false;
SnapToAlbumSlot();
}
}
else if (_isDragRevealing)
{
// Was drag-revealed, auto-snap on release
_isDragRevealing = false;
SnapToAlbumSlot();
}
}
/// <summary>
/// Coroutine to reveal card after holding for specified duration
/// </summary>
private IEnumerator HoldRevealTimer()
{
yield return new WaitForSeconds(holdRevealDelay);
// If still holding after delay, reveal the card
if (!_isRevealed && _isHolding)
{
RevealCard();
_isDragRevealing = true;
Debug.Log("[AlbumCardDraggable] Card revealed via hold");
}
_holdRevealCoroutine = null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 706803638ea24880bae19c87d3851ce6
timeCreated: 1762470947

View File

@@ -0,0 +1,57 @@
using AppleHills.Data.CardSystem;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace UI.CardSystem
{
/// <summary>
/// Specialized slot for album pages that only accepts a specific card.
/// Validates cards based on their CardDefinition.
/// </summary>
public class AlbumCardSlot : DraggableSlot
{
[Header("Album Slot Configuration")]
[SerializeField] private CardDefinition targetCardDefinition; // Which card this slot accepts
private bool _isOccupiedPermanently = false;
/// <summary>
/// Set the target card this slot should accept
/// </summary>
public void SetTargetCard(CardDefinition definition)
{
targetCardDefinition = definition;
}
/// <summary>
/// Check if this slot can accept a specific card
/// </summary>
public bool CanAcceptCard(CardData cardData)
{
if (cardData == null || targetCardDefinition == null) return false;
if (_isOccupiedPermanently) return false;
// Card must match this slot's target definition
return cardData.DefinitionId == targetCardDefinition.Id;
}
/// <summary>
/// Called when a card is successfully placed in this slot
/// </summary>
public void OnCardPlaced()
{
_isOccupiedPermanently = true;
}
/// <summary>
/// Check if this slot has been permanently filled
/// </summary>
public bool IsOccupiedPermanently => _isOccupiedPermanently;
/// <summary>
/// Get the target card definition for this slot
/// </summary>
public CardDefinition TargetCardDefinition => targetCardDefinition;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 514a349ba18d4842bc4292cb034f0d76
timeCreated: 1762470924

View File

@@ -1,7 +1,11 @@
using Bootstrap;
using System.Collections.Generic;
using System.Linq;
using AppleHills.Data.CardSystem;
using Bootstrap;
using Data.CardSystem;
using Pixelplacement;
using UI.Core;
using UI.DragAndDrop.Core;
using UnityEngine;
using UnityEngine.UI;
@@ -18,11 +22,20 @@ namespace UI.CardSystem
[SerializeField] private Button exitButton;
[SerializeField] private BookCurlPro.BookPro book;
[Header("Zone Navigation")]
[SerializeField] private BookTabButton[] zoneTabs; // All zone tab buttons
[Header("Album Card Reveal")]
[SerializeField] private SlotContainer bottomRightSlots;
[SerializeField] private GameObject albumCardPrefab;
[Header("Booster Pack UI")]
[SerializeField] private GameObject[] boosterPackButtons;
[SerializeField] private BoosterOpeningPage boosterOpeningPage;
private Input.InputMode _previousInputMode;
private List<AlbumCardDraggable> _activeCards = new List<AlbumCardDraggable>();
private const int MAX_VISIBLE_CARDS = 3;
private void Awake()
{
@@ -54,6 +67,8 @@ namespace UI.CardSystem
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.OnBoosterCountChanged += OnBoosterCountChanged;
// NOTE: OnPendingCardAdded is subscribed in TransitionIn, not here
// This prevents spawning cards when page is not active
// Update initial button visibility
int initialCount = CardSystemManager.Instance.GetBoosterPackCount();
@@ -83,6 +98,7 @@ namespace UI.CardSystem
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.OnBoosterCountChanged -= OnBoosterCountChanged;
// NOTE: OnPendingCardAdded is unsubscribed in TransitionOut
}
// Clean up exit button
@@ -105,6 +121,9 @@ namespace UI.CardSystem
}
}
}
// Clean up active cards
CleanupActiveCards();
}
private void OnExitButtonClicked()
@@ -182,11 +201,26 @@ namespace UI.CardSystem
Debug.Log("[AlbumViewPage] Switched to UI-only input mode on first entry");
}
// Subscribe to pending card events while page is active
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.OnPendingCardAdded += OnPendingCardAdded;
}
// Spawn pending cards when opening album
SpawnPendingCards();
base.TransitionIn();
}
public override void TransitionOut()
{
// Unsubscribe from pending card events when page closes
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.OnPendingCardAdded -= OnPendingCardAdded;
}
// Don't restore input mode here - only restore when actually exiting (in OnExitButtonClicked)
base.TransitionOut();
}
@@ -219,5 +253,312 @@ namespace UI.CardSystem
onComplete?.Invoke();
}
}
#region Album Card Reveal System
/// <summary>
/// Spawn pending cards from CardSystemManager
/// Only spawns unique cards (one per definition+rarity, not one per copy)
/// </summary>
private void SpawnPendingCards()
{
if (CardSystemManager.Instance == null || bottomRightSlots == null || albumCardPrefab == null)
return;
var pending = CardSystemManager.Instance.GetPendingRevealCards();
// Get unique cards only (by DefinitionId + Rarity)
// Filter out cards with CopiesOwned = 0 (shouldn't happen but guard against it)
var uniquePending = pending
.Where(c => c.CopiesOwned > 0) // Guard: exclude zero-count cards
.GroupBy(c => new { c.DefinitionId, c.Rarity })
.Select(g => g.First()) // Take first instance of each unique card
.ToList();
int spawnCount = Mathf.Min(uniquePending.Count, MAX_VISIBLE_CARDS);
Debug.Log($"[AlbumViewPage] Spawning {spawnCount} unique pending cards (total pending: {pending.Count})");
for (int i = 0; i < spawnCount; i++)
{
SpawnCardInSlot(i, uniquePending[i]);
}
}
/// <summary>
/// Spawn a card in a specific slot
/// </summary>
private void SpawnCardInSlot(int slotIndex, CardData cardData)
{
// Guard: Don't spawn cards with zero copies
if (cardData.CopiesOwned <= 0)
{
Debug.LogWarning($"[AlbumViewPage] Skipping spawn of card '{cardData.Name}' with {cardData.CopiesOwned} copies");
return;
}
DraggableSlot slot = FindSlotByIndex(slotIndex);
if (slot == null)
{
Debug.LogWarning($"[AlbumViewPage] Could not find slot with SlotIndex {slotIndex}");
return;
}
// Instantiate card directly as child of the slot container (not the slot itself, not canvas root)
// This keeps it in the correct UI hierarchy
GameObject cardObj = Instantiate(albumCardPrefab, bottomRightSlots.transform);
AlbumCardDraggable card = cardObj.GetComponent<AlbumCardDraggable>();
if (card != null)
{
// Setup card data
card.SetupCard(cardData);
// Subscribe to events
card.OnCardRevealed += OnCardRevealed;
card.OnCardPlacedInAlbum += OnCardPlacedInAlbum;
// NOW assign to slot - this will:
// 1. Reparent to slot
// 2. Apply slot's occupantSizeMode scaling
// 3. Animate to slot position
card.AssignToSlot(slot, true);
// Track it
_activeCards.Add(card);
Debug.Log($"[AlbumViewPage] Spawned card '{cardData.Name}' (CopiesOwned: {cardData.CopiesOwned}) in slot {slotIndex}");
}
else
{
Debug.LogWarning($"[AlbumViewPage] Spawned card has no AlbumCardDraggable component!");
Destroy(cardObj);
}
}
/// <summary>
/// Handle when a new card is added to pending queue
/// Only spawn if this unique card isn't already visualized
/// </summary>
private void OnPendingCardAdded(CardData card)
{
// Guard: Don't spawn cards with zero copies
if (card.CopiesOwned <= 0)
{
Debug.LogWarning($"[AlbumViewPage] Ignoring pending card '{card.Name}' with {card.CopiesOwned} copies");
return;
}
// Check if we already have a card with this definition + rarity spawned
bool alreadySpawned = _activeCards.Any(c =>
c.CardData.DefinitionId == card.DefinitionId &&
c.CardData.Rarity == card.Rarity);
if (alreadySpawned)
{
Debug.Log($"[AlbumViewPage] Card '{card.Name}' already spawned, skipping duplicate spawn");
return; // Don't spawn duplicates
}
// Try to spawn if we have space
if (_activeCards.Count < MAX_VISIBLE_CARDS)
{
int nextSlotIndex = _activeCards.Count;
SpawnCardInSlot(nextSlotIndex, card);
}
}
/// <summary>
/// Handle when a card is revealed (flipped)
/// </summary>
private void OnCardRevealed(AlbumCardDraggable card, CardData cardData)
{
Debug.Log($"[AlbumViewPage] Card revealed: {cardData.Name} (Zone: {cardData.Zone}, CopiesOwned: {cardData.CopiesOwned})");
// IMMEDIATELY move card from pending to inventory upon reveal
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.MarkCardAsPlaced(cardData);
Debug.Log($"[AlbumViewPage] Moved card '{cardData.Name}' from pending to inventory on reveal");
}
// Remove this card from active cards list
_activeCards.Remove(card);
// Check if we're currently viewing the correct zone for this card
CardZone currentZone = GetCurrentZone();
if (currentZone != cardData.Zone)
{
// Card is from a different zone - navigate to its zone
Debug.Log($"[AlbumViewPage] Card zone ({cardData.Zone}) doesn't match current zone ({currentZone}). Navigating to card's zone...");
NavigateToZone(cardData.Zone);
}
else
{
Debug.Log($"[AlbumViewPage] Card zone ({cardData.Zone}) matches current zone - no navigation needed.");
}
// Shuffle remaining cards to front and spawn next unique card
ShuffleCardsToFront();
TrySpawnNextCard();
}
/// <summary>
/// Handle when a card is placed in the album (from AlbumCardDraggable)
/// Card data already moved to inventory in OnCardRevealed
/// This just handles cleanup
/// </summary>
private void OnCardPlacedInAlbum(AlbumCardDraggable card, CardData cardData)
{
Debug.Log($"[AlbumViewPage] Card placed in album slot: {cardData.Name}");
// Unsubscribe from events (card is now static in album)
card.OnCardRevealed -= OnCardRevealed;
card.OnCardPlacedInAlbum -= OnCardPlacedInAlbum;
// Note: Card already removed from _activeCards in OnCardRevealed
// Note: Shuffle and spawn already done in OnCardRevealed
}
/// <summary>
/// Shuffle active cards to occupy front slots
/// </summary>
private void ShuffleCardsToFront()
{
if (bottomRightSlots == null || _activeCards.Count == 0)
return;
// Convert to base DraggableObject list for helper method
List<DraggableObject> draggableList = _activeCards.Cast<DraggableObject>().ToList();
SlotContainerHelper.ShuffleToFront(bottomRightSlots, draggableList, animate: true);
}
/// <summary>
/// Try to spawn the next pending card
/// Only spawns unique cards (not duplicates)
/// </summary>
private void TrySpawnNextCard()
{
if (CardSystemManager.Instance == null)
return;
if (_activeCards.Count >= MAX_VISIBLE_CARDS)
return; // Already at max
var pending = CardSystemManager.Instance.GetPendingRevealCards();
// Get unique pending cards, excluding zero-count cards
var uniquePending = pending
.Where(c => c.CopiesOwned > 0) // Guard: exclude zero-count cards
.GroupBy(c => new { c.DefinitionId, c.Rarity })
.Select(g => g.First())
.ToList();
// Find first unique card that's not already spawned
foreach (var cardData in uniquePending)
{
bool alreadySpawned = _activeCards.Any(c =>
c.CardData.DefinitionId == cardData.DefinitionId &&
c.CardData.Rarity == cardData.Rarity);
if (!alreadySpawned)
{
int nextSlotIndex = _activeCards.Count;
SpawnCardInSlot(nextSlotIndex, cardData);
break;
}
}
}
/// <summary>
/// Find a slot by its SlotIndex property
/// </summary>
private DraggableSlot FindSlotByIndex(int slotIndex)
{
if (bottomRightSlots == null)
return null;
foreach (var slot in bottomRightSlots.Slots)
{
if (slot.SlotIndex == slotIndex)
{
return slot;
}
}
return null;
}
/// <summary>
/// Get the current zone based on book page
/// </summary>
public CardZone GetCurrentZone()
{
if (book == null || zoneTabs == null || zoneTabs.Length == 0)
return CardZone.AppleHills; // Default
int currentPage = book.CurrentPaper;
// Find tab with matching target page
foreach (var tab in zoneTabs)
{
if (tab.TargetPage == currentPage)
{
return tab.Zone;
}
}
// Fallback to first zone
return zoneTabs[0].Zone;
}
/// <summary>
/// Get tab for a specific zone
/// </summary>
public BookTabButton GetTabForZone(CardZone zone)
{
if (zoneTabs == null)
return null;
foreach (var tab in zoneTabs)
{
if (tab.Zone == zone)
{
return tab;
}
}
return null;
}
/// <summary>
/// Navigate to a specific zone
/// </summary>
public void NavigateToZone(CardZone zone)
{
BookTabButton tab = GetTabForZone(zone);
if (tab != null)
{
tab.ActivateTab();
}
}
/// <summary>
/// Clean up all active cards
/// </summary>
private void CleanupActiveCards()
{
foreach (var card in _activeCards)
{
if (card != null && card.gameObject != null)
{
card.OnCardRevealed -= OnCardRevealed;
card.OnCardPlacedInAlbum -= OnCardPlacedInAlbum;
Destroy(card.gameObject);
}
}
_activeCards.Clear();
}
#endregion
}
}

View File

@@ -1,4 +1,5 @@
using System;
using AppleHills.Data.CardSystem;
using BookCurlPro;
using UnityEngine;
using UnityEngine.UI;
@@ -18,6 +19,7 @@ namespace UI.CardSystem
[Header("Tab Configuration")]
[SerializeField] private int targetPage;
[SerializeField] private CardZone zone;
[Header("Visual Settings")]
[SerializeField] private bool enableScaling = true;
@@ -31,6 +33,10 @@ namespace UI.CardSystem
// Static dispatcher for coordinating all tabs
private static event Action<BookTabButton> OnTabClicked;
// Public properties to access this tab's configuration
public CardZone Zone => zone;
public int TargetPage => targetPage;
private void Awake()
{

View File

@@ -0,0 +1,70 @@
 using System.Collections.Generic;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace UI.CardSystem
{
/// <summary>
/// Helper utility for shuffling draggable objects in a SlotContainer.
/// Moves objects to occupy the first available slots (0, 1, 2, etc.)
/// </summary>
public static class SlotContainerHelper
{
/// <summary>
/// Shuffles draggable objects to always occupy the first available slots.
/// Unassigns all objects from their current slots, then reassigns them starting from slot 0.
/// </summary>
/// <param name="container">The slot container holding the slots</param>
/// <param name="objects">List of draggable objects to shuffle</param>
/// <param name="animate">Whether to animate the movement</param>
public static void ShuffleToFront(SlotContainer container, List<DraggableObject> objects, bool animate = true)
{
if (container == null || objects == null || objects.Count == 0)
return;
Debug.Log($"[SlotContainerHelper] Shuffling {objects.Count} objects to front slots");
// Unassign all objects from their current slots
foreach (var obj in objects)
{
if (obj.CurrentSlot != null)
{
obj.CurrentSlot.Vacate();
}
}
// Reassign objects to first N slots starting from slot 0
for (int i = 0; i < objects.Count; i++)
{
DraggableSlot targetSlot = FindSlotByIndex(container, i);
DraggableObject obj = objects[i];
if (targetSlot != null)
{
Debug.Log($"[SlotContainerHelper] Assigning object to slot with SlotIndex {i}");
obj.AssignToSlot(targetSlot, animate);
}
else
{
Debug.LogWarning($"[SlotContainerHelper] Could not find slot with SlotIndex {i}");
}
}
}
/// <summary>
/// Find a slot by its SlotIndex property (not list position)
/// </summary>
private static DraggableSlot FindSlotByIndex(SlotContainer container, int slotIndex)
{
foreach (var slot in container.Slots)
{
if (slot.SlotIndex == slotIndex)
{
return slot;
}
}
return null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cad44f85ab1a4672ab4bb14e2f919413
timeCreated: 1762470959