ZoeAppClass/Assets/Jogos/Jogo Aula 02/Timer/TimerScript.cs

103 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class TimerScript : MonoBehaviour
{
[DllImport("__Internal")]
public static extern void FailedGame();
public Animator lose;
public float limitTime = 60;
public bool startTimer = false;
private float initialTimer = 0;
private float minutes;
private float seconds;
public float displayTime;
public bool pauseTime = false;
public Text display;
public CanvasGroup hud;
public Image circleLoader;
// Start is called before the first frame update
void Start()
{
startTimer = true;
lose = GameObject.Find("GameOverPanel").GetComponent<Animator>();
initialTimer = limitTime;
minutes = Mathf.Floor(limitTime / 60) ;
seconds = limitTime % 60;
display.text = 0 + minutes.ToString("00") + " " + Mathf.RoundToInt(seconds).ToString("00");
}
// Update is called once per frame
void Update()
{
if (ControllerQuestions.ended)
{
pauseTime = true;
}
if (limitTime <= 0)
{
lose.SetBool("done", true); ControllerQuestions.ended = true; FailedGame();
}
if (startTimer && !pauseTime)
{
Timer();
if (limitTime > 0)
{
limitTime -= Time.deltaTime;
seconds -= Time.deltaTime;
}
display.text = 0 + minutes.ToString("00") + " " + Mathf.RoundToInt(seconds).ToString("00");
CircleAnimation();
}
}
public void StartTimer()
{
startTimer = true;
}
public void PauseTimer(bool change = true)
{
pauseTime = change;
}
public void ResetTimer()
{
limitTime = initialTimer;
}
void Timer()
{
if (seconds <= 0)
{
if (minutes > 0)
{
seconds = 59;
minutes--;
}
else
{
minutes = 00;
seconds = 00;
}
}
}
private void CircleAnimation()
{
if (circleLoader != null)
{
circleLoader.fillAmount = limitTime / initialTimer;
}
}
}