-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIManager.cs
137 lines (102 loc) · 3.13 KB
/
UIManager.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections.Generic;
public class UIManager : MonoBehaviour
{
GameObject[] finishObjects;
public GameController gameData;
private GameObject gameComponents;
private List<MonoBehaviour> gameScripts;
// Use this for initialization
void Start ()
{
gameComponents = GameObject.FindWithTag("GameController");
List<MonoBehaviour> gameScripts = new List<MonoBehaviour>();
Time.timeScale = 1;
//Populate list with all scripts attached to the GameController component
foreach (MonoBehaviour gameScript in gameComponents.GetComponents<MonoBehaviour>())
{
gameScripts.Add(gameScript);
}
//Populate arrays of UI objects and hide them
finishObjects = GameObject.FindGameObjectsWithTag("ShowOnFinish");
ShowFinished(false);
}
//Restart button not working. Timescale not reset to 1
// Update is called once per frame
void Update ()
{
if (gameData.winner != 0)
{
EndGame();
gameData.winner = 0;
}
}
//Enables or disables MonoBehaviour scripts attached to the GameController object
public void EnableGameScripts(bool enabled)
{
foreach (MonoBehaviour script in gameScripts)
{
script.enabled = enabled;
}
}
//Stop the game and display the winning player
public void EndGame()
{
Debug.Log("endgame script run");
if (gameData.winner == 1)
{
Debug.Log("\n Winner set \n");
ShowFinished(true);
SetWinText("Player 1 has won the game");
}
else if (gameData.winner == 2)
{
Debug.Log("\n Winner set \n");
ShowFinished(true);
SetWinText("Player 2 has won the game");
}
//EnableGameScripts(false);
}
//Controls pausing or unpausing the game and showing the game over menu
public void PauseControl()
{
if (Time.timeScale != 0)
{
Time.timeScale = 0;
ShowFinished(true);
}
else if (Time.timeScale == 0)
{
Time.timeScale = 1;
ShowFinished(false);
}
Debug.Log(Time.timeScale);
}
//Set the text on the finish screen to display the winner
public void SetWinText(string text)
{
Text win = GameObject.Find("WinText").GetComponent<Text>();
win.text = text;
}
//Show or hide objects with ShowOnFinish tag
public void ShowFinished(bool showFinishMenu)
{
foreach (GameObject g in finishObjects)
{
g.SetActive(showFinishMenu);
}
}
//Load level from function parameter
public void LoadLevel(string level)
{
SceneManager.LoadScene(level);
}
//Exit application
public void ExitGame()
{
Application.Quit();
}
}