Kind of working booster packs

This commit is contained in:
Michal Adam Pikulski
2025-10-20 13:45:56 +02:00
parent 32f726d229
commit 83b5c8994d
12 changed files with 1244 additions and 299 deletions

View File

@@ -77,158 +77,108 @@ namespace AppleHills.UI.CardSystem
if (collectedCards.Count > 0)
{
// Create card UI elements
CreateCardStack(collectedCards);
DisplayCards(collectedCards);
}
}
/// <summary>
/// Creates UI elements for the player's collected cards
/// </summary>
private void CreateCardStack(List<CardData> cards)
private void DisplayCards(List<CardData> cards)
{
if (albumGrid == null || cardPrefab == null) return;
// Stack offset for visual effect
Vector3 stackOffset = new Vector3(5f, -5f, 0f);
Vector3 basePosition = Vector3.zero;
// Sort cards by collection index
cards.Sort((a, b) => a.CollectionIndex.CompareTo(b.CollectionIndex));
// Create card UI elements
for (int i = 0; i < cards.Count; i++)
foreach (var cardData in cards)
{
GameObject cardObj = Instantiate(cardPrefab, albumGrid.transform);
CardUIElement cardUI = cardObj.GetComponent<CardUIElement>();
// Configure the card UI with the card data
CardUIElement cardUI = cardObj.GetComponent<CardUIElement>();
if (cardUI != null)
{
// Position in stack
cardObj.GetComponent<RectTransform>().anchoredPosition = basePosition + (stackOffset * i);
// Set up card data
cardUI.SetupCard(cards[i]);
// Add drag handlers
SetupCardDragHandlers(cardUI);
// Add to tracked cards
cardUI.SetupCard(cardData);
_displayedCards.Add(cardUI);
}
}
}
/// <summary>
/// Sets up drag and drop handlers for a card
/// </summary>
private void SetupCardDragHandlers(CardUIElement cardUI)
{
// Add click listener to handle card clicks
Button cardButton = cardUI.GetComponent<Button>();
if (cardButton != null)
{
cardButton.onClick.AddListener(() => OnCardClicked(cardUI));
}
// Subscribe to the card's own click event
cardUI.OnCardClicked += OnCardClicked;
}
/// <summary>
/// Handles when a card is clicked (simplistic alternative to drag and drop)
/// </summary>
private void OnCardClicked(CardUIElement cardUI)
{
CardData cardData = cardUI.GetCardData();
// Currently, clicking a card does not perform any action
// This can be expanded to show card details or other interactions
}
/// <summary>
/// Clears all cards from the album
/// Clears all card UI elements from the album
/// </summary>
private void ClearAlbum()
{
// Clear card UI elements
foreach (var card in _displayedCards)
{
if (card != null && card.gameObject != null)
{
Destroy(card.gameObject);
}
}
_displayedCards.Clear();
}
/// <summary>
/// Initialize filtering UI and set up listeners
/// Initializes filter dropdowns
/// </summary>
private void InitializeFilters()
{
// Setup zone filter
// Setup zone filter dropdown
if (zoneFilterDropdown != null)
{
zoneFilterDropdown.ClearOptions();
// Add "All Zones" option
List<string> zoneOptions = new List<string> { "All Zones" };
List<string> zoneOptions = new List<string>() { "All Zones" };
// Add each zone
// Add options for each zone
foreach (CardZone zone in System.Enum.GetValues(typeof(CardZone)))
{
zoneOptions.Add(zone.ToString());
}
zoneFilterDropdown.AddOptions(zoneOptions);
zoneFilterDropdown.onValueChanged.AddListener(OnZoneFilterChanged);
zoneFilterDropdown.onValueChanged.AddListener(OnFilterChanged);
}
// Setup rarity filter
// Setup rarity filter dropdown
if (rarityFilterDropdown != null)
{
rarityFilterDropdown.ClearOptions();
// Add "All Rarities" option
List<string> rarityOptions = new List<string> { "All Rarities" };
List<string> rarityOptions = new List<string>() { "All Rarities" };
// Add each rarity
// Add options for each rarity
foreach (CardRarity rarity in System.Enum.GetValues(typeof(CardRarity)))
{
rarityOptions.Add(rarity.ToString());
}
rarityFilterDropdown.AddOptions(rarityOptions);
rarityFilterDropdown.onValueChanged.AddListener(OnRarityFilterChanged);
rarityFilterDropdown.onValueChanged.AddListener(OnFilterChanged);
}
// Setup reset button
// Setup reset filters button
if (resetFiltersButton != null)
{
resetFiltersButton.onClick.AddListener(ResetFilters);
resetFiltersButton.onClick.AddListener(OnResetFiltersClicked);
}
}
/// <summary>
/// Called when zone filter dropdown changes
/// Handles changes to the filter dropdowns
/// </summary>
private void OnZoneFilterChanged(int index)
private void OnFilterChanged(int value)
{
ApplyFilters();
}
/// <summary>
/// Called when rarity filter dropdown changes
/// Resets all filters to their default values
/// </summary>
private void OnRarityFilterChanged(int index)
{
ApplyFilters();
}
/// <summary>
/// Reset all filters to default values
/// </summary>
private void ResetFilters()
private void OnResetFiltersClicked()
{
if (zoneFilterDropdown != null)
zoneFilterDropdown.value = 0;
@@ -240,97 +190,68 @@ namespace AppleHills.UI.CardSystem
}
/// <summary>
/// Apply current filter settings to the displayed cards
/// Applies the current filter selections to the displayed cards
/// </summary>
private void ApplyFilters()
{
if (_cardManager == null) return;
// Clear current cards
ClearAlbum();
// Get all cards
List<CardData> allCards = _cardManager.GetAllCollectedCards();
List<CardData> filteredCards = new List<CardData>(allCards);
// Get selected filters
CardZone? selectedZone = null;
CardRarity? selectedRarity = null;
// Apply zone filter
// Get zone filter value
if (zoneFilterDropdown != null && zoneFilterDropdown.value > 0)
{
CardZone selectedZone = (CardZone)(zoneFilterDropdown.value - 1);
filteredCards = filteredCards.FindAll(card => card.Zone == selectedZone);
selectedZone = (CardZone)(zoneFilterDropdown.value - 1);
}
// Apply rarity filter
// Get rarity filter value
if (rarityFilterDropdown != null && rarityFilterDropdown.value > 0)
{
CardRarity selectedRarity = (CardRarity)(rarityFilterDropdown.value - 1);
filteredCards = filteredCards.FindAll(card => card.Rarity == selectedRarity);
selectedRarity = (CardRarity)(rarityFilterDropdown.value - 1);
}
// Update the displayed cards
ClearAlbum();
CreateCardStack(filteredCards);
// Get filtered cards
List<CardData> filteredCards = GetFilteredCards(selectedZone, selectedRarity);
// Create card UI elements for the filtered cards
DisplayCards(filteredCards);
Logging.Debug($"[AlbumViewPage] Applied filters. Showing {filteredCards.Count} cards.");
}
// Override for transition animations
protected override void DoTransitionIn(System.Action onComplete)
/// <summary>
/// Gets cards filtered by zone and/or rarity
/// </summary>
private List<CardData> GetFilteredCards(CardZone? zone, CardRarity? rarity)
{
// Reset the canvas group
if (canvasGroup != null)
List<CardData> result;
// Get all collected cards
if (zone == null && rarity == null)
{
canvasGroup.alpha = 0f;
Tween.Value(0f, 1f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete);
// No filters, return all cards
result = _cardManager.GetAllCollectedCards();
}
else if (zone != null && rarity != null)
{
// Both filters, get cards by zone and rarity
result = _cardManager.GetCardsByZoneAndRarity(zone.Value, rarity.Value);
}
else if (zone != null)
{
// Only zone filter
result = _cardManager.GetCardsByZone(zone.Value);
}
else
{
// Simple scale animation if no canvas group
transform.localScale = Vector3.zero;
Tween.LocalScale(transform, Vector3.one, transitionDuration, 0f, Tween.EaseOutBack, Tween.LoopType.None, null, onComplete);
// Only rarity filter
result = _cardManager.GetCardsByRarity(rarity.Value);
}
}
protected override void DoTransitionOut(System.Action onComplete)
{
// Simple fade animation
if (canvasGroup != null)
{
Tween.Value(canvasGroup.alpha, 0f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete);
}
else
{
Tween.LocalScale(transform, Vector3.zero, transitionDuration, 0f, Tween.EaseInBack, Tween.LoopType.None, null, onComplete);
}
}
private void OnDestroy()
{
// Clean up filter listeners
if (zoneFilterDropdown != null)
zoneFilterDropdown.onValueChanged.RemoveListener(OnZoneFilterChanged);
if (rarityFilterDropdown != null)
rarityFilterDropdown.onValueChanged.RemoveListener(OnRarityFilterChanged);
if (resetFiltersButton != null)
resetFiltersButton.onClick.RemoveListener(ResetFilters);
// Clean up back button listener
if (backButton != null)
backButton.onClick.RemoveListener(OnBackButtonClicked);
// Clean up card listeners
foreach (var card in _displayedCards)
{
if (card != null)
{
// Unsubscribe from card events
card.OnCardClicked -= OnCardClicked;
// Remove button listeners
Button cardButton = card.GetComponent<Button>();
if (cardButton != null)
{
cardButton.onClick.RemoveAllListeners();
}
}
}
return result;
}
/// <summary>
@@ -345,5 +266,66 @@ namespace AppleHills.UI.CardSystem
pageController.PopPage();
}
}
protected override void DoTransitionIn(System.Action onComplete)
{
// Simple fade in animation
if (canvasGroup != null)
{
canvasGroup.alpha = 0f;
Tween.Value(0f, 1f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete);
}
else
{
// Fallback if no CanvasGroup
onComplete?.Invoke();
}
}
protected override void DoTransitionOut(System.Action onComplete)
{
// Simple fade out animation
if (canvasGroup != null)
{
Tween.Value(canvasGroup.alpha, 0f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete);
}
else
{
// Fallback if no CanvasGroup
onComplete?.Invoke();
}
}
private void OnDestroy()
{
// Clean up button listeners
if (backButton != null)
{
backButton.onClick.RemoveListener(OnBackButtonClicked);
}
if (zoneFilterDropdown != null)
{
zoneFilterDropdown.onValueChanged.RemoveListener(OnFilterChanged);
}
if (rarityFilterDropdown != null)
{
rarityFilterDropdown.onValueChanged.RemoveListener(OnFilterChanged);
}
if (resetFiltersButton != null)
{
resetFiltersButton.onClick.RemoveListener(OnResetFiltersClicked);
}
}
private void OnEnable()
{
if (_cardManager == null)
{
_cardManager = CardSystemManager.Instance;
}
}
}
}