-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cs
More file actions
85 lines (72 loc) · 2.46 KB
/
Timer.cs
File metadata and controls
85 lines (72 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour
{
[HideInInspector] public float timer = 0f;
[HideInInspector] public bool isTimerZero = false;
[HideInInspector] public int timerTweenId;
public TextMeshProUGUI timerUI;
[SerializeField] private LeanTweenType tweenType;
[SerializeField] private float animDuration = 0.5f;
private bool isTimerStopped = false;
[SerializeField] private GameObject winPanel;
[SerializeField] private GameObject losePanel;
private float currentLevelDurationTime= 0f;
private bool isCriticalFlag = false;
private void Start()
{
timerUI = GetComponent<TextMeshProUGUI>();
timer = GameManager.Instance.GetCurrentCricketLevelData().time;
Debug.Log($"Current timer: {timer}");
currentLevelDurationTime = GameManager.Instance.GetCurrentCricketLevelData().time;
}
private void Update()
{
if(isTimerCriticalLow(currentLevelDurationTime) && !isTimerStopped && !isCriticalFlag)
{
AnimateTimer();
ChangeTimerColor(Color.red);
isCriticalFlag = true;
}
if (timer > 0 && !isTimerStopped)
{
timer -= Time.deltaTime;
UpdateTimerUI();
}
else if (timer <= 0 && !isTimerStopped)
{
Debug.Log("You lose");
UIManager.Instance.PopUpAnim(losePanel, Vector3.one);
SoundManager.PlaySound(SoundType.LOSE, 10f);
LeanTween.cancel(timerTweenId);
ChangeTimerColor(Color.white);
UpdateTimerUI();
StopTimer();
}
}
public void UpdateTimerUI()
{
string timeString = string.Format("{0}:{1:00}", (int)timer / 60, (int)timer % 60);
timerUI.text = timeString;
}
public void AnimateTimer()
{
timerTweenId = LeanTween.scale(timerUI.gameObject, new Vector3(1.5f, 1.5f, 1.5f), animDuration).setEase(tweenType).setLoopPingPong().id;
}
public void ChangeTimerColor(Color color)
{
timerUI.color = color;
}
public bool isTimerCriticalLow(float levelDuration)
{
return timer < levelDuration / 5f; // less than 20%
}
public void StopTimer()
{
isTimerStopped = true;
isCriticalFlag = false;
}
}