Semi-working rarity upgrades

This commit is contained in:
Michal Pikulski
2025-11-06 23:06:41 +01:00
parent d23c000347
commit a705b3a829
7 changed files with 969 additions and 70 deletions

View File

@@ -11,9 +11,17 @@ namespace AppleHills.Data.CardSystem
[Serializable]
public class CardInventory
{
// Dictionary of collected cards indexed by definition ID
// Dictionary of collected cards indexed by definition ID + rarity (e.g., "Pikachu_Normal", "Pikachu_Rare")
[SerializeField] private Dictionary<string, CardData> collectedCards = new Dictionary<string, CardData>();
/// <summary>
/// Generate a unique key for a card based on definition ID and rarity
/// </summary>
private string GetCardKey(string definitionId, CardRarity rarity)
{
return $"{definitionId}_{rarity}";
}
// Number of unopened booster packs the player has
[SerializeField] private int boosterPackCount;
@@ -96,7 +104,9 @@ namespace AppleHills.Data.CardSystem
{
if (card == null) return;
if (collectedCards.TryGetValue(card.DefinitionId, out CardData existingCard))
string key = GetCardKey(card.DefinitionId, card.Rarity);
if (collectedCards.TryGetValue(key, out CardData existingCard))
{
// Increase copies of existing card
existingCard.CopiesOwned++;
@@ -105,7 +115,7 @@ namespace AppleHills.Data.CardSystem
{
// Add new card to collection
var newCard = new CardData(card);
collectedCards[card.DefinitionId] = newCard;
collectedCards[key] = newCard;
// Add to lookup dictionaries
cardsByZone[newCard.Zone].Add(newCard);
@@ -133,19 +143,21 @@ namespace AppleHills.Data.CardSystem
}
/// <summary>
/// Get a specific card from the collection by definition ID
/// Get a specific card from the collection by definition ID and rarity
/// </summary>
public CardData GetCard(string definitionId)
public CardData GetCard(string definitionId, CardRarity rarity)
{
return collectedCards.TryGetValue(definitionId, out CardData card) ? card : null;
string key = GetCardKey(definitionId, rarity);
return collectedCards.TryGetValue(key, out CardData card) ? card : null;
}
/// <summary>
/// Check if the player has a specific card
/// Check if the player has a specific card at a specific rarity
/// </summary>
public bool HasCard(string definitionId)
public bool HasCard(string definitionId, CardRarity rarity)
{
return collectedCards.ContainsKey(definitionId);
string key = GetCardKey(definitionId, rarity);
return collectedCards.ContainsKey(key);
}
/// <summary>

View File

@@ -1,14 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AppleHills.Data.CardSystem;
using Bootstrap;
using Core;
using Core.SaveLoad;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Data.CardSystem
{
@@ -150,6 +146,7 @@ namespace Data.CardSystem
/// <summary>
/// Opens a booster pack and returns the newly obtained cards
/// NOTE: Cards are NOT added to inventory immediately - they're added after the reveal interaction
/// </summary>
public List<CardData> OpenBoosterPack()
{
@@ -165,11 +162,9 @@ namespace Data.CardSystem
// Draw 3 cards based on rarity distribution
List<CardData> drawnCards = DrawRandomCards(3);
// Add cards to the inventory
foreach (var card in drawnCards)
{
AddCardToInventory(card);
}
// NOTE: Cards are NOT added to inventory here anymore
// They will be added after the player interacts with each revealed card
// This allows us to show new/repeat status before adding to collection
// Notify listeners
OnBoosterOpened?.Invoke(drawnCards);
@@ -177,33 +172,56 @@ namespace Data.CardSystem
Logging.Debug($"[CardSystemManager] Opened a booster pack and obtained {drawnCards.Count} cards. Remaining boosters: {playerInventory.BoosterPackCount}");
return drawnCards;
}
/// <summary>
/// Check if a card is new to the player's collection at the specified rarity
/// </summary>
/// <param name="cardData">The card to check</param>
/// <param name="existingCard">Out parameter - the existing card if found, null otherwise</param>
/// <returns>True if this is a new card at this rarity, false if already owned</returns>
public bool IsCardNew(CardData cardData, out CardData existingCard)
{
if (playerInventory.HasCard(cardData.DefinitionId, cardData.Rarity))
{
existingCard = playerInventory.GetCard(cardData.DefinitionId, cardData.Rarity);
return false;
}
existingCard = null;
return true;
}
/// <summary>
/// Adds a card to the player's inventory after reveal (delayed add)
/// Public wrapper for AddCardToInventory to support delayed inventory updates
/// </summary>
public void AddCardToInventoryDelayed(CardData card)
{
AddCardToInventory(card);
}
/// <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.HasCard(card.DefinitionId))
// Check if the player already has this card at this rarity
if (playerInventory.HasCard(card.DefinitionId, card.Rarity))
{
CardData existingCard = playerInventory.GetCard(card.DefinitionId);
CardData existingCard = playerInventory.GetCard(card.DefinitionId, card.Rarity);
existingCard.CopiesOwned++;
// Check if the card can be upgraded
if (existingCard.TryUpgradeRarity())
{
OnCardRarityUpgraded?.Invoke(existingCard);
}
// Note: Upgrades are now handled separately in BoosterOpeningPage
// We don't auto-upgrade here anymore
Logging.Debug($"[CardSystemManager] Added duplicate card '{card.Name}'. Now have {existingCard.CopiesOwned} copies.");
Logging.Debug($"[CardSystemManager] Added duplicate card '{card.Name}' ({card.Rarity}). Now have {existingCard.CopiesOwned} copies.");
}
else
{
// Add new card
// Add new card at this rarity
playerInventory.AddCard(card);
OnCardCollected?.Invoke(card);
Logging.Debug($"[CardSystemManager] Added new card '{card.Name}' to collection.");
Logging.Debug($"[CardSystemManager] Added new card '{card.Name}' ({card.Rarity}) to collection.");
}
}
@@ -300,11 +318,17 @@ namespace Data.CardSystem
}
/// <summary>
/// Returns whether a specific card definition has been collected
/// Returns whether a specific card definition has been collected (at any rarity)
/// </summary>
public bool IsCardCollected(string definitionId)
{
return playerInventory.HasCard(definitionId);
// Check if the card exists at any rarity
foreach (CardRarity rarity in System.Enum.GetValues(typeof(CardRarity)))
{
if (playerInventory.HasCard(definitionId, rarity))
return true;
}
return false;
}
/// <summary>