Unity_2lab/2/Assets/Scripts/CardCollector.cs
2025-10-10 02:07:41 +03:00

49 lines
934 B
C#

using UnityEngine;
public class CardCollector : MonoBehaviour
{
private Card _firstCard;
private Card _secondCard;
public void FindCards()
{
Card[] cards = FindObjectsOfType<Card>();
for (int i = 0; i < cards.Length; i++)
{
cards[i].SetCardCollector(this);
}
}
public void OpenCard(Card card)
{
if(_firstCard == null)
_firstCard=card;
else
{
_secondCard=card;
Invoke(nameof(CompareCards), 0.8f);
}
}
private void CompareCards()
{
if (_firstCard.Index == _secondCard.Index)
{
Destroy(_firstCard.gameObject);
Destroy(_secondCard.gameObject);
}
else
{
_secondCard.CardAnimation();
_firstCard.CardAnimation();
}
_secondCard = null;
_firstCard = null;
}
public bool TwoCardsClosed()
{
return _secondCard==null;
}
}