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

public class TimeController : MonoBehaviour
{
    // 카운트다운 & 카운트 업
    public bool isCountDown = true; // 카운트다운으로 시간 측정(true)
    // 게임 최대시간(초), 카운트다운->이 값~0까지/카운트업->0~이 값까지
    public float gameTime = 0;
    public bool isTimeOver = false; // 타이머 정지
    public float displayTime = 0;   // 화면에 표시되는 시간

    float times = 0;     // 현재 시간

    void Start()
    {
        // 카운트 다운이면, 시간이 감소돼야 되므로 게임최대시간(gameTime)이 표시돼야
        if (isCountDown)
        {
            displayTime = gameTime;
        }
    }

    // 시간 종료가 아닐때만 실행
    void Update()
    {
        if (isTimeOver==false)
        {
            // 시작시간부터 경과시간 구하기
            times += Time.deltaTime; // times = times + Time.deltaTime;
            if (isCountDown) // 카운트 다운일 때
            {
                displayTime = gameTime - times;
                if (displayTime <= 0.0f)
                {
                    displayTime = 0.0f;
                    isTimeOver = true;
                }

            }
            else // 카운트 업일 때
            {
                displayTime = times;
                if (displayTime >= gameTime)
                {
                    displayTime = gameTime;
                    isTimeOver = true;
                }
            }
            Debug.Log("현재시간(times): " + displayTime);
        }
    }
}