You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.4 KiB
C#
93 lines
2.4 KiB
C#
using Google.Impl;
|
|
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
public class TimerManager : MonoBehaviour
|
|
{
|
|
public TimeSpan _remainingTime;
|
|
public TimeSpan _initialTime;
|
|
private bool _isTimerRunning;
|
|
public TextMeshProUGUI timer;
|
|
public GameObject timerObj;
|
|
public GameObject gameoverPanelObj;
|
|
public UnityEvent TimerEnded = new UnityEvent();
|
|
private float _levelStartTime;
|
|
private const string LevelTimeKey = "LastLevelTimeSpent";
|
|
private void Start()
|
|
{
|
|
_levelStartTime = Time.time; // Start tracking level time
|
|
|
|
if (GamePlayManager.isTimerLevel)
|
|
{
|
|
timerObj.SetActive(true);
|
|
InitializeTimer(0, 2, 0); // Default 2 mins, adjust if needed
|
|
}
|
|
else
|
|
{
|
|
timerObj.SetActive(false);
|
|
}
|
|
}
|
|
public void InitializeTimer(int hours, int minutes, int seconds)
|
|
{
|
|
_initialTime = new TimeSpan(hours, minutes, seconds);
|
|
_remainingTime = _initialTime;
|
|
StartTimer();
|
|
}
|
|
public void StartTimer()
|
|
{
|
|
if (_remainingTime.TotalSeconds > 0 && !_isTimerRunning)
|
|
{
|
|
_isTimerRunning = true;
|
|
}
|
|
}
|
|
public void StopTimer()
|
|
{
|
|
_isTimerRunning = false;
|
|
}
|
|
public void ResetTimer()
|
|
{
|
|
StopTimer();
|
|
_remainingTime = _initialTime;
|
|
}
|
|
public TimeSpan GetRemainingTime()
|
|
{
|
|
return _remainingTime;
|
|
}
|
|
void Update()
|
|
{
|
|
if (_isTimerRunning)
|
|
{
|
|
_remainingTime = _remainingTime.Subtract(TimeSpan.FromSeconds(Time.deltaTime));
|
|
timer.text = _remainingTime.ToString("m\\:ss");
|
|
|
|
if (_remainingTime.TotalSeconds <= 0)
|
|
{
|
|
_remainingTime = TimeSpan.Zero;
|
|
_isTimerRunning = false;
|
|
|
|
SaveTimeSpent(); // Save how much time was spent
|
|
TimerEnded?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SaveTimeSpent()
|
|
{
|
|
float timeSpent = Time.time - _levelStartTime;
|
|
PlayerPrefs.SetFloat(LevelTimeKey, timeSpent);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
public static string GetFormattedLastLevelTime()
|
|
{
|
|
float rawSeconds = PlayerPrefs.GetFloat(LevelTimeKey, 0f);
|
|
int minutes = Mathf.FloorToInt(rawSeconds / 60f);
|
|
int seconds = Mathf.FloorToInt(rawSeconds % 60f);
|
|
|
|
return $"{minutes}m and {seconds}s";
|
|
}
|
|
}
|