-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTimer.cs
61 lines (51 loc) · 1.36 KB
/
GameTimer.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
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameTimer : MonoBehaviour {
public float levelTime;
private Slider slider;
private AudioSource audioSource;
private bool isEndOfLevel = false;
private LevelManager levelManager;
private GameObject winLabel;
// Use this for initialization
void Start () {
slider = GetComponent<Slider>();
audioSource = GetComponent<AudioSource>();
levelManager = GameObject.FindObjectOfType<LevelManager>();
FindYouWin();
}
void FindYouWin(){
winLabel = GameObject.Find("You Win");
if(!winLabel){
Debug.LogWarning("Add You Win Object");
}
winLabel.SetActive(false);
}
// Update is called once per frame
void Update () {
DecrementLevelTime();
WinScenario();
}
void LoadNextLevel(){
levelManager.LoadNextLevel();
}
void DecrementLevelTime(){
slider.value = Time.timeSinceLevelLoad / levelTime;
}
void WinScenario(){
if(Time.timeSinceLevelLoad >= levelTime && !isEndOfLevel){
DestroyAllTaggedObjects();
audioSource.Play();
winLabel.SetActive(true);
Invoke ("LoadNextLevel", audioSource.clip.length);
isEndOfLevel = true;
}
}
void DestroyAllTaggedObjects(){
GameObject[] destroyableObjectArray = GameObject.FindGameObjectsWithTag("destroyable");
foreach(GameObject taggedObject in destroyableObjectArray){
Destroy(taggedObject);
}
}
}