ZoeAppClass/Assets/Jogos/MemoryGame/Scripts/TimerBehaviour.cs

59 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TimerBehaviour : MonoBehaviour
{
public float defaultTimer = 30;
public float timeRemaining = 30;
public bool runTimer = false;
public Slider timeSlide;
public static TimerBehaviour instance;
private Coroutine running;
private void Awake()
{
instance = this;
}
public void StartTimer()
{
running = StartCoroutine(RunTimer());
}
public void StopTimer()
{
runTimer = false;
StopCoroutine(running);
}
public void ResetTimer()
{
timeRemaining = defaultTimer;
UpdateTimer(1);
}
public void UpdateTimer(float i)
{
timeSlide.value = i;
}
IEnumerator RunTimer()
{
ResetTimer();
runTimer = true;
for(float i = timeRemaining; i >= 0 ; i-= 1)
{
if (runTimer)
{
yield return new WaitForSecondsRealtime(1);
timeRemaining--;
UpdateTimer(timeRemaining / defaultTimer);
}
else
{
break;
}
}
MemoryManager.instance.GameOver();
}
}