-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.cs
176 lines (150 loc) · 4.28 KB
/
GameController.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
// Make this class a singleton
public static GameController Instance { get; private set; }
public bool isAIEnabled;
public bool isPaused;
public bool isPauseScreenOpen;
public bool startWithTutorialInfo;
public int gameDifficulty;
private CanvasGroup pauseScreen;
private GameObject startText;
private GameObject pausedScreenData;
private PlayerController playerController;
public Dictionary<string, bool> conditions = new Dictionary<string, bool>();
public Texture2D cursorTexture;
private CursorMode cursorMode = CursorMode.Auto;
private Vector2 hotSpot = Vector2.zero;
void Start()
{
Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
}
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
InitConditions();
pauseScreen = GameObject.FindGameObjectWithTag("PauseScreen").GetComponent<CanvasGroup>();
pausedScreenData = GameObject.FindGameObjectWithTag("PausedScreenData");
startText = GameObject.FindGameObjectWithTag("StartButton");
playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
if (startWithTutorialInfo)
{
OpenPauseMenu();
startText.SetActive(true);
pausedScreenData.SetActive(false);
}
else
{
pauseScreen.alpha = 0;
startText.gameObject.SetActive(true);
pausedScreenData.SetActive(true);
}
}
void Update()
{
if (Input.GetButtonDown("Menu"))
{
if (!isPauseScreenOpen)
{
if (playerController.GetCanMove())
OpenPauseMenu();
}
else if(isPauseScreenOpen)
{
ClosePauseMenu();
}
}
}
private void InitConditions()
{
conditions.Add("hasKey", false);
conditions.Add("beatBoss", false);
conditions.Add("bossLives", true);
}
public void EnableAI()
{
isAIEnabled = true;
}
public void DisableAI()
{
isAIEnabled = false;
}
public void SetDifficulty(int difficulty)
{
gameDifficulty = difficulty;
}
public void AddCondition (string label, bool value)
{
conditions.Add(label, value);
}
public void UpdateCondition(string label, bool value)
{
conditions[label] = value;
}
public void RemoveCondition(string label)
{
conditions.Remove(label);
}
public void PauseGame()
{
Time.timeScale = 0f;
isPaused = true;
}
public void OpenPauseMenu()
{
PauseGame();
isPauseScreenOpen = true;
pauseScreen.alpha = 1;
pauseScreen.interactable = true;
pauseScreen.blocksRaycasts = true;
playerController.EnterUIMode();
}
public void ResumeGame()
{
Time.timeScale = 1f;
isPaused = false;
}
public void ClosePauseMenu()
{
StartCoroutine(FadePausePanel(false));
startText.SetActive(false);
pausedScreenData.SetActive(true);
ResumeGame();
isPauseScreenOpen = false;
playerController.SetCanMove(true);
}
private IEnumerator FadePausePanel(bool fadeIn)
{
float elapsedTime = 0f;
float fadeDuration = .3f;
while (elapsedTime < fadeDuration)
{
elapsedTime += Time.deltaTime;
// If fadeIn is true, we fade in. Otherwise, we fade out
if (fadeIn)
{
pauseScreen.alpha = Mathf.Lerp(0, 1, elapsedTime / fadeDuration);
}
else
{
pauseScreen.alpha = Mathf.Lerp(1, 0, elapsedTime / fadeDuration);
}
yield return null;
}
// If fadeIn is true, we make the UI interactable. Otherwise, we make it non-interactable
pauseScreen.interactable = fadeIn;
pauseScreen.blocksRaycasts = fadeIn;
}
}