-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreAndTimeController.cs
103 lines (93 loc) · 3.19 KB
/
ScoreAndTimeController.cs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using UnityEngine;
using UnityEngine.UI;
using TMPro;
//ScoreAndTimeController : counter and score management / detect if lost or win
public class ScoreAndTimeController : MonoBehaviour
{
[SerializeField] private float mainTimer;
[SerializeField] private GameObject goRubbish;
//Counter
private float timer;
private bool canCount = true;
private bool doOnce = false;
private int minutesFinal;
private int secondsFinal;
//Scores
private int count;
public TextMeshProUGUI timerText;
public TextMeshProUGUI countText;
private int goRubbishCount;
public GameObject gameController;
void Start()
{
timer = mainTimer;
//Cound the number of gans un the GO Rubbish
goRubbishCount = goRubbish.GetComponentsInChildren<Transform>().GetLength(0) - 1;
goRubbishCount = goRubbishCount / 2; //divided by 2 because all prefabs have a children with icon
//Displays counter of can grabbed
count = 0;
countText.text = "cans: 0" + count.ToString() + "/" + goRubbishCount;
}
void Update()
{
//Launches coundown
if (timer > 1.0f && canCount)
{
timer -= Time.deltaTime;
//int hours = Mathf.FloorToInt(timer / 3600F);
int minutes = Mathf.FloorToInt((timer % 3600) / 60);
int seconds = Mathf.FloorToInt(timer % 60);
//string formattedTime = string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
string formattedTime = string.Format("{0:00}:{1:00}", minutes, seconds);
timerText.text = "time: " + formattedTime;
if (timer < 60.0f)
{
timerText.color = Color.red;
}
}
//Stops coundown => player has lost if zero
else if (timer <= 1.0f && !doOnce)
{
canCount = false;
doOnce = true;
//timerText.text = "Time: 00:00";
timer = 0.0f;
GameOver();
}
}
//Update score and detect if the player has won
public void SetCountText()
{
count++;
if (count<10)
{
countText.text = "cans: 0" + count.ToString() + "/" + goRubbishCount;
}
else
{
countText.text = "cans: " + count.ToString() + "/" + goRubbishCount;
}
if (goRubbishCount == count)
{
float finalTime = mainTimer - timer;
minutesFinal = Mathf.FloorToInt((finalTime % 3600) / 60);
secondsFinal = Mathf.FloorToInt(finalTime % 60);
Invoke("DelayWin", 2);
}
}
//Send the information of win in GameController.cs
void DelayWin()
{
gameController.GetComponent<GameController>().WinGame(count, minutesFinal, secondsFinal);
}
//Send the information of lost in GameController.cs
void GameOver()
{
//wait 2 seconds to leave the last animation of picking up
Invoke("DelayLost", 2);
}
void DelayLost()
{
gameController.GetComponent<GameController>().LostGame(count);
}
}