123

 avatar
user_8384735
csharp
2 years ago
2.1 kB
2
Indexable
Never
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class GameControl : MonoBehaviour
{
    [SerializeField] private GameObject player;
    [SerializeField] private GameObject pickups;
    [SerializeField] private TextMeshProUGUI scoreText;
    [SerializeField] private Canvas resultUI;

    private int playerScore = 0;
    private int playerHp = 3;
    private List<GameObject> pickupList = new List<GameObject>();

    private void Start()
    {
        // 將 pickups 中的所有道具物件存儲到 pickupList 中
        foreach (Transform child in pickups.transform)
        {
            if (child.CompareTag("pickup"))
            {
                pickupList.Add(child.gameObject);
            }
        }

        resultUI.enabled = false;
    }

    private void Update()
    {
        ScoreTextChange();
        CheckWin();
    }

    private void ScoreTextChange()
    {
        scoreText.text = "Score:" + playerScore.ToString();
    }

    private void CheckWin()
    {
        if (playerScore >= pickupList.Count)
        {
            resultUI.enabled = true;
            resultUI.GetComponentInChildren<TextMeshProUGUI>().text = "You Win!!!";
            PauseGame();
        }
    }

    private void PauseGame()
    {
        Time.timeScale = 0;
    }

    private void ResumeGame()
    {
        Time.timeScale = 1;
    }

    public void RestartGame()
    {
        playerScore = 0;
        playerHp = 3;
        resultUI.enabled = false;
        ResumeGame();
    }

    public void QuitGame()
    {
        Application.Quit();
    }

    public void AddScore(int score)
    {
        playerScore += score;
    }

    public int GetPlayerHp()
    {
        return playerHp;
    }

    public void SetPlayerHp(int hp)
    {
        playerHp = hp;
    }

    public int GetPlayerScore()
    {
        return playerScore;
    }

    public void SetPlayerScore(int score)
    {
        playerScore = score;
    }
}