같은그림찾기 gameManager.cs 복기

더보기

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;

public class gameManager : MonoBehaviour
{
    public Text timeTxt;
    public GameObject endTxt;
    public GameObject card;
    float time;
    public static gameManager I;
    public GameObject firstCard;
    public GameObject secondCard;

    public AudioClip match;
    public AudioSource AudioSource;
    void Awake()
    {
        I = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        // [] = list 선언 {} = 원하는 값
        int[] rtans = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
        //랜덤으로 섞기
        rtans = rtans.OrderBy(item => Random.Range(-1.0f, 1.0f)).ToArray();

        //for (조건 ; 기한 ; 행동)
        for (int i = 0; i < 16; i++)
        {
            // 자식 = newCard를 부모 = cards로 옮겨줘
            GameObject newCard = Instantiate(card);
            newCard.transform.parent = GameObject.Find("cards").transform;
            // % = 나머지 값
            float x = (i / 4) * 1.4f -2.1f;
            float y = (i % 4) * 1.4f -3.0f;
            newCard.transform.position = new Vector3(x, y, 0);

            string rtanName = "rtan" + rtans[i].ToString();
            newCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(rtanName);
        }

    }

    // Update is called once per frame
    void Update()
    {
        //시간이 흐르게 만듬
        time += Time.deltaTime;
        timeTxt.text = time.ToString("N2");

        if (time > 30f)
        {
            Time.timeScale = 0f;
            endTxt.SetActive(true);
        }
    }

    public void isMatched()
    {
        string firstCardImage = firstCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite.name;
        string secondCardImage = secondCard.transform.Find("front").GetComponent<SpriteRenderer>().sprite.name;

        if (firstCardImage == secondCardImage)
        {
            AudioSource.PlayOneShot(match);
            Debug.Log("같다");
            firstCard.GetComponent<card>().destroyCard();
            secondCard.GetComponent<card>().destroyCard();

            int cardLeft = GameObject.Find("cards").transform.childCount;
            if (cardLeft == 2)
            {
                Time.timeScale = 0f;
                endTxt.SetActive(true);
                Invoke("GameEnd", 1f);

            }
        }
        else
        {
            Debug.Log("다르다");
            firstCard.GetComponent <card>().closeCard();  
            secondCard.GetComponent<card>().closeCard();  
        }

        firstCard = null;
        secondCard = null;


    }
     void GameEnd()
    {
        Time.timeScale = 0f;
        endTxt.SetActive(true);
    }
}

card.cs 복기 중...

더보기

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class card : MonoBehaviour
{
    public Animator anim;

    void Start()
    {
        
    }

    void Update()
    {
        
    }

    public void openCard()
    {
        anim.SetBool("isOpen", true);
        transform.Find("front").gameObject.SetActive(true);
        transform.Find("back").gameObject.SetActive(false);

        if (gameManager.I.firstCard == null)
        {
            gameManager.I.firstCard = gameObject;
        }
        else
        {
            gameManager.I.secondCard = gameObject;
            gameManager.I.isMatched();
        }
    }
    public void destroyCard()
    {
        Invoke("destroyCardInvoke", 1.0f);
    }

    void destroyCardInvoke()
    {
        Destroy(gameObject);
    }

    public void closeCard()
    {
        Invoke("closeCardInvoke", 1.0f);
    }

    void closeCardInvoke()
    {
        anim.SetBool("isOpen", false);
        transform.Find("back").gameObject.SetActive(true);
        transform.Find("front").gameObject.SetActive(false);
    }
}

반응형