Add backbone for card creation and implement Camera minigame mechanics
This commit is contained in:
264
Assets/Scripts/Data/CardSystem/CardSystemManager.cs
Normal file
264
Assets/Scripts/Data/CardSystem/CardSystemManager.cs
Normal file
@@ -0,0 +1,264 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AppleHills.Data.CardSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages the player's card collection, booster packs, and related operations.
|
||||
/// Uses a singleton pattern for global access.
|
||||
/// </summary>
|
||||
public class CardSystemManager : MonoBehaviour
|
||||
{
|
||||
private static CardSystemManager _instance;
|
||||
private static bool _isQuitting = false;
|
||||
|
||||
public static CardSystemManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null && Application.isPlaying && !_isQuitting)
|
||||
{
|
||||
_instance = FindAnyObjectByType<CardSystemManager>();
|
||||
if (_instance == null)
|
||||
{
|
||||
var go = new GameObject("CardSystemManager");
|
||||
_instance = go.AddComponent<CardSystemManager>();
|
||||
DontDestroyOnLoad(go);
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
[Header("Card Collection")]
|
||||
[SerializeField] private List<CardDefinition> availableCards = new List<CardDefinition>();
|
||||
|
||||
// Runtime data - will be serialized for save/load
|
||||
[SerializeField] private CardInventory playerInventory = new CardInventory();
|
||||
|
||||
// Dictionary to quickly look up card definitions by ID
|
||||
private Dictionary<string, CardDefinition> _definitionLookup = new Dictionary<string, CardDefinition>();
|
||||
|
||||
// Event callbacks using System.Action
|
||||
public event Action<List<CardData>> OnBoosterOpened;
|
||||
public event Action<CardData> OnCardCollected;
|
||||
public event Action<CardData> OnCardRarityUpgraded;
|
||||
public event Action<int> OnBoosterCountChanged;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
_instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
// Build lookup dictionary
|
||||
BuildDefinitionLookup();
|
||||
}
|
||||
|
||||
private void OnApplicationQuit()
|
||||
{
|
||||
_isQuitting = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a lookup dictionary for quick access to card definitions by ID
|
||||
/// </summary>
|
||||
private void BuildDefinitionLookup()
|
||||
{
|
||||
_definitionLookup.Clear();
|
||||
|
||||
foreach (var cardDef in availableCards)
|
||||
{
|
||||
if (cardDef != null && !string.IsNullOrEmpty(cardDef.Id))
|
||||
{
|
||||
_definitionLookup[cardDef.Id] = cardDef;
|
||||
}
|
||||
}
|
||||
|
||||
// Link existing card data to their definitions
|
||||
foreach (var cardData in playerInventory.CollectedCards.Values)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(cardData.DefinitionId) &&
|
||||
_definitionLookup.TryGetValue(cardData.DefinitionId, out CardDefinition def))
|
||||
{
|
||||
cardData.SetDefinition(def);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a booster pack to the player's inventory
|
||||
/// </summary>
|
||||
public void AddBoosterPack(int count = 1)
|
||||
{
|
||||
playerInventory.BoosterPackCount += count;
|
||||
OnBoosterCountChanged?.Invoke(playerInventory.BoosterPackCount);
|
||||
Debug.Log($"[CardSystemManager] Added {count} booster pack(s). Total: {playerInventory.BoosterPackCount}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a booster pack and returns the newly obtained cards
|
||||
/// </summary>
|
||||
public List<CardData> OpenBoosterPack()
|
||||
{
|
||||
if (playerInventory.BoosterPackCount <= 0)
|
||||
{
|
||||
Debug.LogWarning("[CardSystemManager] Attempted to open a booster pack, but none are available.");
|
||||
return new List<CardData>();
|
||||
}
|
||||
|
||||
playerInventory.BoosterPackCount--;
|
||||
OnBoosterCountChanged?.Invoke(playerInventory.BoosterPackCount);
|
||||
|
||||
// Draw 3 cards based on rarity distribution
|
||||
List<CardData> drawnCards = DrawRandomCards(3);
|
||||
|
||||
// Add cards to the inventory
|
||||
foreach (var card in drawnCards)
|
||||
{
|
||||
AddCardToInventory(card);
|
||||
}
|
||||
|
||||
// Notify listeners
|
||||
OnBoosterOpened?.Invoke(drawnCards);
|
||||
|
||||
Debug.Log($"[CardSystemManager] Opened a booster pack and obtained {drawnCards.Count} cards. Remaining boosters: {playerInventory.BoosterPackCount}");
|
||||
return drawnCards;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a card to the player's inventory, handles duplicates
|
||||
/// </summary>
|
||||
private void AddCardToInventory(CardData card)
|
||||
{
|
||||
// Check if the player already has this card type (definition)
|
||||
if (playerInventory.CollectedCards.TryGetValue(card.DefinitionId, out CardData existingCard))
|
||||
{
|
||||
existingCard.CopiesOwned++;
|
||||
|
||||
// Check if the card can be upgraded
|
||||
if (existingCard.TryUpgradeRarity())
|
||||
{
|
||||
OnCardRarityUpgraded?.Invoke(existingCard);
|
||||
}
|
||||
|
||||
Debug.Log($"[CardSystemManager] Added duplicate card '{card.Name}'. Now have {existingCard.CopiesOwned} copies.");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add new card
|
||||
playerInventory.CollectedCards.Add(card.DefinitionId, new CardData(card));
|
||||
OnCardCollected?.Invoke(card);
|
||||
|
||||
Debug.Log($"[CardSystemManager] Added new card '{card.Name}' to collection.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws random cards based on rarity distribution
|
||||
/// </summary>
|
||||
private List<CardData> DrawRandomCards(int count)
|
||||
{
|
||||
List<CardData> result = new List<CardData>();
|
||||
|
||||
if (availableCards.Count == 0)
|
||||
{
|
||||
Debug.LogError("[CardSystemManager] No available cards defined!");
|
||||
return result;
|
||||
}
|
||||
|
||||
// Simple weighted random selection based on rarity
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
// Determine card rarity first
|
||||
CardRarity rarity = DetermineRandomRarity();
|
||||
|
||||
// Filter cards by the selected rarity
|
||||
List<CardDefinition> cardsOfRarity = availableCards.FindAll(c => c.Rarity == rarity);
|
||||
|
||||
if (cardsOfRarity.Count > 0)
|
||||
{
|
||||
// Select a random card of this rarity
|
||||
int randomIndex = UnityEngine.Random.Range(0, cardsOfRarity.Count);
|
||||
CardDefinition selectedDef = cardsOfRarity[randomIndex];
|
||||
|
||||
// Create card data from definition
|
||||
CardData newCard = selectedDef.CreateCardData();
|
||||
result.Add(newCard);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback if no cards of the selected rarity
|
||||
Debug.LogWarning($"[CardSystemManager] No cards of rarity {rarity} available, selecting a random card instead.");
|
||||
int randomIndex = UnityEngine.Random.Range(0, availableCards.Count);
|
||||
CardDefinition randomDef = availableCards[randomIndex];
|
||||
|
||||
CardData newCard = randomDef.CreateCardData();
|
||||
result.Add(newCard);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines a random card rarity with appropriate weighting
|
||||
/// </summary>
|
||||
private CardRarity DetermineRandomRarity()
|
||||
{
|
||||
// Simple weighted random - can be adjusted for better distribution
|
||||
float rand = UnityEngine.Random.value;
|
||||
|
||||
if (rand < 0.6f) return CardRarity.Common;
|
||||
if (rand < 0.85f) return CardRarity.Uncommon;
|
||||
if (rand < 0.95f) return CardRarity.Rare;
|
||||
if (rand < 0.99f) return CardRarity.Epic;
|
||||
return CardRarity.Legendary;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all cards from the player's collection
|
||||
/// </summary>
|
||||
public List<CardData> GetAllCollectedCards()
|
||||
{
|
||||
List<CardData> result = new List<CardData>();
|
||||
foreach (var card in playerInventory.CollectedCards.Values)
|
||||
{
|
||||
result.Add(card);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of booster packs the player has
|
||||
/// </summary>
|
||||
public int GetBoosterPackCount()
|
||||
{
|
||||
return playerInventory.BoosterPackCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether a specific card definition has been collected
|
||||
/// </summary>
|
||||
public bool IsCardCollected(string definitionId)
|
||||
{
|
||||
return playerInventory.CollectedCards.ContainsKey(definitionId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializable class to store the player's card inventory
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class CardInventory
|
||||
{
|
||||
public Dictionary<string, CardData> CollectedCards = new Dictionary<string, CardData>();
|
||||
public int BoosterPackCount;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user