MiniGameManager.cs 2.39 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public enum GameState
{
    Ready,
    Play,
    End
}

public class MiniGameManager : MonoBehaviour
{
    public List<Rabbit> m_rabbits;
    static public GameState gameState;
    public GameObject UIButton;
    public Text timeText;
    public Text scoreText;

    private int score = 0;
    private float time = 0f;
    private float sec = 0f;

    private int playTime = 30;

    void Start()
    {
        SetSceneReady();
    }

    public void SetSceneReady()
    {
        gameState = GameState.Ready;
        timeText.text = "00 : " + playTime.ToString("D2");
        ResetRabbits();
    }

    public void LoadMainScene()
    {
        // 로딩바?
        SceneManager.LoadScene("Main");
    }

    void ResetRabbits()
    {
        for (int i = 0; i < m_rabbits.Count; i++)
        {
            m_rabbits[i].None();
        }
    }

    void ExitRabbits()
    {
        for (int i = 0; i < m_rabbits.Count; i++)
        {
            if (m_rabbits[i].state != RabbitState.Die && m_rabbits[i].state != RabbitState.None)
                m_rabbits[i].Exit();
        }
    }

    public void ShowScore()
    {
        gameState = GameState.End;
        scoreText.text = score + " 마리를 잡았습니다!";
        SetGamePanel.Instance.SetPanelOn();
        scoreText.gameObject.SetActive(true);
        UIButton.SetActive(true);
    }


    void Update()
    {
        if (gameState == GameState.Play)
        {
            time += Time.deltaTime;
            sec = Mathf.Ceil(playTime - time);
            timeText.text = "00 : " + ((int)sec).ToString("D2");

            if (sec <= 0)
            {
                ExitRabbits();
                ShowScore();
            }

            if (Input.GetMouseButtonDown(0))
            {
                Vector2 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                RaycastHit2D hit = Physics2D.Raycast(touchPos, Vector2.zero);

                if (hit)
                {
                    int num = int.Parse(hit.collider.gameObject.name);
                    if (m_rabbits[num].state == RabbitState.Idle)
                    {
                        score += 1;
                        m_rabbits[num].Die();
                    }
                }
            }
        }
        else
            return;
    }
}