└▶연습

탄막5

101won 2024. 10. 7. 22:58

 

리스타트

게임 오버 시, 버틴 시간을 점수로 하게 함

 - TextMeshProUGUI도 게임 오브젝트로 불러서 제어할 수 있었다.

 

PlayerPrefs로 최고 점수를 저장해 화면에 띄우는 거 해야겠다.

 

  • UICtrl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.VisualScripting;
using UnityEngine.SceneManagement;

public class UICtrl : MonoBehaviour
{
    public TextMeshProUGUI TimeCount;
    public TextMeshProUGUI LifeCount;
    public TextMeshProUGUI Score;

    int maxHp = 3;
    float delta = 0;

    public Player player;
    public GameObject redFilter;
    public GameObject reStart;

    void Start()
    {
        player = FindObjectOfType<Player>();

        Score.GetComponent<TextMeshProUGUI>();
        
        Time.timeScale = 1f;
    }

    void Update()
    {
        delta += Time.deltaTime;

        this.TimeCount.GetComponent<TMP_Text>().text = delta.ToString("Time: 0");
        this.LifeCount.GetComponent<TMP_Text>().text = maxHp.ToString($"Life: {maxHp}");

        if (Input.GetMouseButtonUp(0) && this.maxHp == 0)
        {
            // Debug.Log("끝!");
             SceneManager.LoadScene("SampleScene");
        }
    }

    public void Life()
    {
         // Debug.Log("악");
        this.maxHp -= 1;
         // Debug.Log($"현재 체력: {maxHp}");

        player.StartCoroutine(player.HitColor()); //다른 클래스에 포함된 코루틴 실행

        if (this.maxHp <= 2)
        {
            LifeCount.color = Color.yellow;
            StartCoroutine(RedFilter());
        }

        if (maxHp <= 0)
        {
           //  Debug.Log("주금");

            player.DeathColor();

            if (this.maxHp <= 0)
            {
                this.maxHp = 0;
            }
           Score.gameObject.SetActive(true); // txt를 게임 오브젝트로 선언해 제어함
            
            this.reStart.SetActive(true);
            this.Score.GetComponent<TMP_Text>().text = delta.ToString("Score: 0");

            Time.timeScale = 0f;
        }
    }

    IEnumerator RedFilter()
    {
        while (true) // 무한 반복
        {
            redFilter.gameObject.SetActive(true);
            yield return new WaitForSeconds(0.3f);

            redFilter.gameObject.SetActive(false);
            yield return new WaitForSeconds(0.3f);
        }
    }
}