-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.cs
242 lines (206 loc) · 6.44 KB
/
GameManager.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*using System.Collections;
using UnityEngine;
/// <summary>
/// Manages game logic and controls the UI
/// </summary>
public class GameManager : MonoBehaviour
{
[Tooltip("Game ends when an agent succeeds this many tasks")]
public float maxIce = 8f;
[Tooltip("Game ends after this many seconds have elapsed")]
public float timerAmount = 60f;
[Tooltip("The UI Controller")]
public UIController uiController;
[Tooltip("The player drone")]
public DroneAgent player;
[Tooltip("The ML-Agent opponent drone")]
public DroneAgent opponent;
[Tooltip("The Dickinson Quad")]
public QuadArea quadArea;
// [Tooltip("The main camera for the scene")]
// public Camera mainCamera;
// When the game timer started
private float gameTimerStartTime;
/// <summary>
/// All possible game states
/// </summary>
public enum GameState
{
Default,
MainMenu,
Preparing,
Playing,
Gameover
}
/// <summary>
/// The current game state
/// </summary>
public GameState State { get; private set; } = GameState.Default;
/// <summary>
/// Gets the time remaining in the game
/// </summary>
public float TimeRemaining
{
get
{
if (State == GameState.Playing)
{
float timeRemaining = timerAmount - (Time.time - gameTimerStartTime);
return Mathf.Max(0f, timeRemaining);
}
else
{
return 0f;
}
}
}
/// <summary>
/// Handles a button click in different states
/// </summary>
public void ButtonClicked()
{
if (State == GameState.Gameover)
{
// In the Gameover state, button click should go to the main menu
MainMenu();
}
else if (State == GameState.MainMenu)
{
// In the MainMenu state, button click should start the game
StartCoroutine(StartGame());
}
else
{
Debug.LogWarning("Button clicked in unexpected state: " + State.ToString());
}
}
/// <summary>
/// Called when the game starts
/// </summary>
private void Start()
{
// Subscribe to button click events from the UI
uiController.OnButtonClicked += ButtonClicked;
// Start the main menu
MainMenu();
}
/// <summary>
/// Called on destroy
/// </summary>
private void OnDestroy()
{
// Unsubscribe from button click events from the UI
uiController.OnButtonClicked -= ButtonClicked;
}
/// <summary>
/// Shows the main menu
/// </summary>
private void MainMenu()
{
// Set the state to "main menu"
State = GameState.MainMenu;
// Update the UI
uiController.ShowBanner("");
uiController.ShowButton("Start");
// // Use the main camera, disable agent cameras
// mainCamera.gameObject.SetActive(true);
// player.agentCamera.gameObject.SetActive(false);
// opponent.agentCamera.gameObject.SetActive(false); // Never turn this back on
// Reset the ice patches
quadArea.ResetIce();
// Reset the agents
player.OnEpisodeBegin();
opponent.OnEpisodeBegin();
// Freeze the agents
player.FreezeAgent();
opponent.FreezeAgent();
}
/// <summary>
/// Starts the game with a countdown
/// </summary>
/// <returns>IEnumerator</returns>
private IEnumerator StartGame()
{
// Set the state to "preparing"
State = GameState.Preparing;
// Update the UI (hide it)
uiController.ShowBanner("");
uiController.HideButton();
// // Use the player camera, disable the main camera
// mainCamera.gameObject.SetActive(false);
// player.agentCamera.gameObject.SetActive(true);
// Show countdown
uiController.ShowBanner("3");
yield return new WaitForSeconds(1f);
uiController.ShowBanner("2");
yield return new WaitForSeconds(1f);
uiController.ShowBanner("1");
yield return new WaitForSeconds(1f);
uiController.ShowBanner("Go!");
yield return new WaitForSeconds(1f);
uiController.ShowBanner("");
// Set the state to "playing"
State = GameState.Playing;
// Start the game timer
gameTimerStartTime = Time.time;
// Unfreeze the agents
player.UnfreezeAgent();
opponent.UnfreezeAgent();
}
/// <summary>
/// Ends the game
/// </summary>
private void EndGame()
{
// Set the game state to "game over"
State = GameState.Gameover;
// Freeze the agents
player.FreezeAgent();
opponent.FreezeAgent();
// Update banner text depending on win/lose
if (player.iceObtained >= opponent.iceObtained )
{
uiController.ShowBanner("You win!");
}
else
{
uiController.ShowBanner("ML-Agent wins!");
}
// Update button text
uiController.ShowButton("Main Menu");
}
/// <summary>
/// Called every frame
/// </summary>
private void Update()
{
if (State == GameState.Playing)
{
// Check to see if time has run out or either agent got the max Ice amount
if (TimeRemaining <= 0f ||
player.iceObtained >= maxIce ||
opponent.iceObtained >= maxIce)
{
EndGame();
}
// Update the timer and Ice progress bars
uiController.SetTimer(TimeRemaining);
uiController.SetPlayerIce(player.iceObtained / maxIce);
uiController.SetOpponentIce(opponent.iceObtained / maxIce);
}
else if (State == GameState.Preparing || State == GameState.Gameover)
{
// Update the timer
uiController.SetTimer(TimeRemaining);
}
else
{
// Hide the timer
uiController.SetTimer(-1f);
// Update the progress bars
uiController.SetPlayerIce(0f);
uiController.SetOpponentIce(0f);
}
}
}
*/