Setup and play area prefab
This commit is contained in:
parent
fe61df43e7
commit
af12b601fc
166 changed files with 14014 additions and 10 deletions
73
WizardDuelsProject/Assets/Scripts/CardArea.cs
Normal file
73
WizardDuelsProject/Assets/Scripts/CardArea.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue