2025-11-07 11:24:19 +01:00
|
|
|
|
using AppleHills.Data.CardSystem;
|
2025-11-10 13:03:36 +01:00
|
|
|
|
using Core;
|
2025-11-07 11:24:19 +01:00
|
|
|
|
using 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.
|
|
|
|
|
|
/// Self-populates with owned cards when enabled.
|
|
|
|
|
|
/// </summary>
|
2025-11-16 20:35:54 +01:00
|
|
|
|
public class AlbumCardSlot : DraggableSlot
|
2025-11-07 11:24:19 +01:00
|
|
|
|
{
|
|
|
|
|
|
[Header("Album Slot Configuration")]
|
|
|
|
|
|
[SerializeField] private CardDefinition targetCardDefinition; // Which card this slot accepts
|
2025-11-16 20:35:54 +01:00
|
|
|
|
[SerializeField] private GameObject cardPrefab; // Card prefab to spawn when card is owned
|
2025-11-07 11:24:19 +01:00
|
|
|
|
|
2025-11-16 20:35:54 +01:00
|
|
|
|
private StateMachine.Card _placedCard;
|
2025-11-07 11:24:19 +01:00
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Check if we should spawn a card for this slot
|
|
|
|
|
|
CheckAndSpawnOwnedCard();
|
|
|
|
|
|
}
|
2025-11-16 20:35:54 +01:00
|
|
|
|
|
2025-11-07 11:24:19 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Check if player owns the card for this slot and spawn it if so
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void CheckAndSpawnOwnedCard()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Guard: need CardSystemManager and target definition
|
|
|
|
|
|
if (CardSystemManager.Instance == null || targetCardDefinition == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Guard: don't spawn if already occupied
|
2025-11-16 20:35:54 +01:00
|
|
|
|
if (_placedCard != null)
|
2025-11-07 11:24:19 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Guard: need prefab to spawn
|
2025-11-16 20:35:54 +01:00
|
|
|
|
if (cardPrefab == null)
|
2025-11-07 11:24:19 +01:00
|
|
|
|
{
|
2025-11-16 20:35:54 +01:00
|
|
|
|
Logging.Warning($"[AlbumCardSlot] No cardPrefab assigned for slot targeting {targetCardDefinition.name}");
|
2025-11-07 11:24:19 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if player owns this card at ANY rarity (prioritize highest rarity)
|
|
|
|
|
|
CardData ownedCard = null;
|
|
|
|
|
|
|
|
|
|
|
|
// Check in order: Legendary > Rare > Normal
|
|
|
|
|
|
foreach (CardRarity rarity in new[] { CardRarity.Legendary, CardRarity.Rare, CardRarity.Normal })
|
|
|
|
|
|
{
|
|
|
|
|
|
CardData card = CardSystemManager.Instance.GetCardInventory().GetCard(targetCardDefinition.Id, rarity);
|
|
|
|
|
|
if (card != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ownedCard = card;
|
|
|
|
|
|
break; // Found highest rarity owned
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Spawn card if owned
|
|
|
|
|
|
if (ownedCard != null)
|
|
|
|
|
|
{
|
2025-11-16 20:35:54 +01:00
|
|
|
|
SpawnCard(ownedCard);
|
2025-11-07 11:24:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-11-16 20:35:54 +01:00
|
|
|
|
/// Spawn a Card in this slot using the PlacedInSlotState
|
2025-11-07 11:24:19 +01:00
|
|
|
|
/// </summary>
|
2025-11-16 20:35:54 +01:00
|
|
|
|
private void SpawnCard(CardData cardData)
|
2025-11-07 11:24:19 +01:00
|
|
|
|
{
|
2025-11-16 20:35:54 +01:00
|
|
|
|
GameObject cardObj = Instantiate(cardPrefab, transform);
|
|
|
|
|
|
var card = cardObj.GetComponent<StateMachine.Card>();
|
2025-11-07 11:24:19 +01:00
|
|
|
|
|
2025-11-16 20:35:54 +01:00
|
|
|
|
if (card != null)
|
2025-11-07 11:24:19 +01:00
|
|
|
|
{
|
2025-11-16 20:35:54 +01:00
|
|
|
|
// Setup card for album slot (starts in PlacedInSlotState)
|
|
|
|
|
|
card.SetupForAlbumSlot(cardData, this);
|
|
|
|
|
|
_placedCard = card;
|
2025-11-07 11:24:19 +01:00
|
|
|
|
|
2025-11-16 20:35:54 +01:00
|
|
|
|
// Resize the card to match the slot size
|
|
|
|
|
|
RectTransform cardRect = card.transform as RectTransform;
|
2025-11-07 15:38:31 +00:00
|
|
|
|
RectTransform slotRect = transform as RectTransform;
|
|
|
|
|
|
if (cardRect != null && slotRect != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Set height to match slot height (AspectRatioFitter will handle width)
|
|
|
|
|
|
float targetHeight = slotRect.rect.height;
|
|
|
|
|
|
cardRect.sizeDelta = new Vector2(cardRect.sizeDelta.x, targetHeight);
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure position and rotation are centered
|
|
|
|
|
|
cardRect.localPosition = Vector3.zero;
|
|
|
|
|
|
cardRect.localRotation = Quaternion.identity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-07 11:24:19 +01:00
|
|
|
|
// Register with AlbumViewPage for enlarge/shrink handling
|
2025-11-11 08:48:29 +00:00
|
|
|
|
AlbumViewPage albumPage = FindFirstObjectByType<AlbumViewPage>();
|
2025-11-07 11:24:19 +01:00
|
|
|
|
if (albumPage != null)
|
|
|
|
|
|
{
|
2025-11-16 20:35:54 +01:00
|
|
|
|
albumPage.RegisterCardInAlbum(card);
|
2025-11-07 11:24:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-10 13:03:36 +01:00
|
|
|
|
Logging.Debug($"[AlbumCardSlot] Spawned owned card '{cardData.Name}' ({cardData.Rarity}) in slot");
|
2025-11-07 11:24:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-11-16 20:35:54 +01:00
|
|
|
|
Logging.Warning($"[AlbumCardSlot] Spawned prefab has no Card component!");
|
2025-11-07 11:24:19 +01:00
|
|
|
|
Destroy(cardObj);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-16 20:35:54 +01:00
|
|
|
|
|
2025-11-07 11:24:19 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the target card definition for this slot
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public CardDefinition TargetCardDefinition => targetCardDefinition;
|
2025-11-17 10:59:59 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the target card definition for this slot (method version for compatibility)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public CardDefinition GetTargetCardDefinition() => targetCardDefinition;
|
2025-11-07 11:24:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|