73 lines
1.9 KiB
C#
73 lines
1.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class CardArea : MonoBehaviour
|
|
{
|
|
[SerializeField] private AudioManager audio;
|
|
[SerializeField] private Card selectedCard;
|
|
[SerializeField] private GameObject slotPrefab;
|
|
|
|
[SerializeField] private Dictionary<Card, GameObject> _cardSlotMapping = new Dictionary<Card, GameObject>();
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if (other.gameObject.TryGetComponent(out Card card)
|
|
&& card.IsDragging
|
|
&& !_cardSlotMapping.Keys.Contains(card))
|
|
{
|
|
StartCoroutine(WaitTriggerEnter());
|
|
card.HoveringArea = this;
|
|
}
|
|
}
|
|
|
|
IEnumerator WaitTriggerEnter()
|
|
{
|
|
yield return 0;
|
|
}
|
|
|
|
private void OnTriggerExit2D(Collider2D other)
|
|
{
|
|
if (other.gameObject.TryGetComponent(out Card card)
|
|
&& card.IsDragging
|
|
&& !_cardSlotMapping.Keys.Contains(card))
|
|
{
|
|
card.HoveringArea = null;
|
|
}
|
|
}
|
|
|
|
public void AddCardToArea(Card card)
|
|
{
|
|
GameObject slot = Instantiate(slotPrefab, transform);
|
|
_cardSlotMapping[card] = slot;
|
|
card.SetSlot(slot);
|
|
card.SetParentArea(this);
|
|
RefreshLayout();
|
|
audio.Play("playcard");
|
|
}
|
|
|
|
public void RemoveCardFromArea(Card card)
|
|
{
|
|
Destroy(_cardSlotMapping[card]);
|
|
_cardSlotMapping.Remove(card);
|
|
RefreshLayout();
|
|
}
|
|
|
|
private void RefreshLayout()
|
|
{
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
|
|
StartCoroutine(MoveCardsNextFrame());
|
|
}
|
|
|
|
private IEnumerator MoveCardsNextFrame()
|
|
{
|
|
yield return null; // Waits until the next frame
|
|
foreach (Card card in _cardSlotMapping.Keys)
|
|
{
|
|
card.MoveToSlot();
|
|
}
|
|
}
|
|
}
|