Files
3DBlobici-WorkingTitle/3D blobici/Assets/Scripts/Cards/CardManager.cs
Tomáš Pěnkava 53c07b69f8 Edge case fix
Fixed an edge case where there were no cards left and it caused an error
2025-07-29 00:34:47 +02:00

97 lines
2.4 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System;
using UnityEngine.Rendering;
public class CardManager : MonoBehaviour
{
public static CardManager Instance;
[SerializeField] private List<GameObject> cardPrefabs;
[SerializeField] private Transform cardsParent;
private PlayerSkillTree playerSkillTree;
private GameObject cards;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
playerSkillTree = PlayerSkillTree.Instance;
cards = GameObject.Find("Cards");
cards.SetActive(false);
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
cards.SetActive(true);
ShowRandomCards(3);
}
}
public void ShowRandomCards(int count)
{
var unlockedSkills = playerSkillTree.GetPlayerSkills();
var availableCards = cardPrefabs
.Select(x => x.GetComponent<CardUI>())
.Where(x => x != null && !unlockedSkills.Contains(x.Skill))
.ToList();
foreach (Transform child in cardsParent)
{
Destroy(child.gameObject);
}
var shuffled = availableCards.OrderBy(x => UnityEngine.Random.value).Take(count).ToList();
Debug.Log($"Showing cards: {string.Join(", ", shuffled.Select(x => x.Skill))}");
Debug.Log($"Available cards: {string.Join(", ", availableCards.Select(x => x.Skill))}");
Debug.Log($"Unlocked skills: {string.Join(", ", unlockedSkills)}");
if (shuffled.Count <= 0)
return;
float step = Screen.width / shuffled.Count;
float pos = step - Screen.width / (2*shuffled.Count);
foreach (var cardUI in shuffled)
{
var newCard = Instantiate(cardUI.gameObject, new Vector3(pos, Screen.height/2, 0), Quaternion.identity, cardsParent);
pos += step;
}
}
public void SelectCard(PlayerSkillTree.Skills skill)
{
playerSkillTree.UnlockSkill(skill);
foreach (Transform child in cardsParent)
{
var cardUI = child.GetComponent<CardUI>();
if (cardUI.Skill == skill)
{
Destroy(child.gameObject);
}
}
cards.SetActive(false);
}
}