99 lines
2.8 KiB
C#
99 lines
2.8 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class Card : MonoBehaviour
|
|
{
|
|
[Header("Referensi Sisi Kartu")]
|
|
public GameObject frontSide;
|
|
public GameObject backSide;
|
|
public Image itemImage;
|
|
|
|
private Sprite mySprite;
|
|
private GameManager gameManager;
|
|
private bool isMatched = false;
|
|
private bool isFlipping = false;
|
|
|
|
public void SetCardData(Sprite img, GameManager manager)
|
|
{
|
|
mySprite = img;
|
|
gameManager = manager;
|
|
isMatched = false;
|
|
isFlipping = false;
|
|
|
|
if (itemImage != null)
|
|
{
|
|
itemImage.sprite = mySprite;
|
|
Color c = itemImage.color;
|
|
c.a = 1;
|
|
itemImage.color = c;
|
|
}
|
|
|
|
transform.localScale = Vector3.one;
|
|
frontSide.SetActive(false);
|
|
backSide.SetActive(true);
|
|
}
|
|
|
|
public Sprite GetSprite() => mySprite;
|
|
|
|
public void OnCardClicked()
|
|
{
|
|
// Panggil fungsi CardSelected di GameManager
|
|
if (isMatched || isFlipping || frontSide.activeSelf || !gameManager.canClick) return;
|
|
gameManager.CardSelected(this);
|
|
}
|
|
|
|
public void ShowCard() { if (gameObject.activeInHierarchy) StartCoroutine(FlipRoutine(true)); }
|
|
public void HideCard() { if (gameObject.activeInHierarchy) StartCoroutine(FlipRoutine(false)); }
|
|
|
|
public void SetMatched()
|
|
{
|
|
isMatched = true;
|
|
StartCoroutine(PulseRoutine());
|
|
}
|
|
|
|
IEnumerator PulseRoutine()
|
|
{
|
|
float duration = 0.15f;
|
|
Vector3 largeScale = new Vector3(1.15f, 1.15f, 1.15f);
|
|
float t = 0;
|
|
while (t < duration)
|
|
{
|
|
t += Time.deltaTime;
|
|
transform.localScale = Vector3.Lerp(Vector3.one, largeScale, t / duration);
|
|
yield return null;
|
|
}
|
|
t = 0;
|
|
while (t < duration)
|
|
{
|
|
t += Time.deltaTime;
|
|
transform.localScale = Vector3.Lerp(largeScale, Vector3.one, t / duration);
|
|
yield return null;
|
|
}
|
|
transform.localScale = Vector3.one;
|
|
}
|
|
|
|
IEnumerator FlipRoutine(bool showFront)
|
|
{
|
|
isFlipping = true;
|
|
float duration = 0.12f;
|
|
float t = 0;
|
|
while (t < duration)
|
|
{
|
|
t += Time.deltaTime;
|
|
transform.localScale = new Vector3(Mathf.Lerp(1, 0, t / duration), 1, 1);
|
|
yield return null;
|
|
}
|
|
frontSide.SetActive(showFront);
|
|
backSide.SetActive(!showFront);
|
|
t = 0;
|
|
while (t < duration)
|
|
{
|
|
t += Time.deltaTime;
|
|
transform.localScale = new Vector3(Mathf.Lerp(0, 1, t / duration), 1, 1);
|
|
yield return null;
|
|
}
|
|
transform.localScale = Vector3.one;
|
|
isFlipping = false;
|
|
}
|
|
} |