EchoTrio
 
Loading...
Searching...
No Matches
GameManager.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.SceneManagement;
3using UnityEngine.InputSystem;
4using GameEvent;
5
6namespace EchoTrio.Gameplay {
7 /// GameManager controls the gameplay animation and audio.
8 public class GameManager : MonoBehaviour {
9 /// GameManager states.
10 private enum State {
11 /// Wait for the player to start the game.
12 Idle,
13 /// Game is in progress.
14 Play,
15 /// Game has finished.
16 Finish,
17 Num,
18 }
19
20 /// Helper class to group references needed for animation.
21 [System.Serializable] public class AnimationReferences {
22 [Header("References")]
23 public Animator animator = null;
24 public AudioSource audioSource = null;
25 public AudioSource[] sfxVariants = new AudioSource[0];
26
27 [Header("Animator Parameters")]
28 public string pushToTalkStartedBool = "IsCtrlPressed";
29 public string pushToTalkCancelledBool = "IsCtrlReleased";
30 public string actorIsTalkingBool = "<Insert Animation Parameter Here>";
31
32 // Public Interface
33 public AudioSource GetRandomSFXVariant() { return sfxVariants[UnityEngine.Random.Range(0, sfxVariants.Length)]; }
34 }
35
36 [Header("Prefabs & References")]
37 [SerializeField] private EchoTrio.UI.FadeEffect[] clouds = new EchoTrio.UI.FadeEffect[0];
38 [SerializeField] private Spelunx.Orbbec.BodyTrackerManager bodyTrackerManager = null;
39 [SerializeField] private AudioSource waitBGM = null;
40 [SerializeField] private AudioSource playBGM = null;
41 [SerializeField] private AnimationReferences[] animationReferences = new AnimationReferences[0];
42
43 [Header("Settings")]
44 [SerializeField, Min(1)] private int numDisplays = 3;
45
46 // Internal Variables
47 GameInputActions gameInputActions = null;
49
50 // Finish State Variables
51 private float fadeInDelay = 3.0f;
52 private float fadeInTimer = 0.0f;
53 private bool hasFadedIn = false;
54
55 // Internal Functions
56 private void Awake() {
57 gameInputActions = new GameInputActions();
58
59 // Initialise FSM.
60 fsm.SetStateEntry((int)State.Idle, OnEnterIdle);
61 fsm.SetStateUpdate((int)State.Idle, OnUpdateIdle);
62 fsm.SetStateExit((int)State.Idle, OnExitIdle);
63
64 fsm.SetStateEntry((int)State.Play, OnEnterPlay);
65 fsm.SetStateUpdate((int)State.Play, OnUpdatePlay);
66 fsm.SetStateExit((int)State.Play, OnExitPlay);
67
68 fsm.SetStateEntry((int)State.Finish, OnEnterFinish);
69 fsm.SetStateUpdate((int)State.Finish, OnUpdateFinish);
70 fsm.SetStateExit((int)State.Finish, OnExitFinish);
71 }
72
73 private void OnEnable() {
74 // Enable inputs.
75 gameInputActions.Enable();
76 gameInputActions.Game.Start.performed += OnStart;
77 gameInputActions.Game.Restart.performed += OnRestart;
78 gameInputActions.Game.Continue.performed += OnContinue;
79 gameInputActions.Game.Quit.performed += OnQuit;
80 gameInputActions.VoiceChat.PushToTalk.started += OnPushToTalkStarted;
81 gameInputActions.VoiceChat.PushToTalk.canceled += OnPushToTalkCancelled;
82
83 // Subscribe to game events.
85 }
86
87 private void OnDisable() {
88 // Disable inputs.
89 gameInputActions.Disable();
90 gameInputActions.Game.Start.performed -= OnStart;
91 gameInputActions.Game.Restart.performed -= OnRestart;
92 gameInputActions.Game.Continue.performed -= OnContinue;
93 gameInputActions.Game.Quit.performed -= OnQuit;
94 gameInputActions.VoiceChat.PushToTalk.started -= OnPushToTalkStarted;
95 gameInputActions.VoiceChat.PushToTalk.canceled -= OnPushToTalkCancelled;
96
97 // Unsubscribe from game events.
99 }
100
101 private void Start() {
102 gameInputActions.Game.Start.Disable();
103 gameInputActions.Game.Restart.Enable(); // Panic button should always be enabled.
104 gameInputActions.Game.Continue.Disable();
105 gameInputActions.VoiceChat.PushToTalk.Disable();
106
107 // Set the default state.
108 fsm.ChangeState((int)State.Idle);
109 }
110
111 private void Update() { fsm.Update(); }
112
113 private void LateUpdate() { fsm.LateUpdate(); }
114
115 // Wait State (You may not have to use all of these functions. I am just creating a template for you.)
116 private void OnEnterIdle() {
117 gameInputActions.Game.Start.Enable();
118
119 // Activate the displays
120 Debug.Log("Connected displays: " + Display.displays.Length);
121 for (int i = 1; i < Mathf.Min(Display.displays.Length, numDisplays); i++)
122 {
123 Display.displays[i].Activate();
124 }
125
126 // Play wait BGM.
127 waitBGM.Play();
128 }
129
130 private void OnUpdateIdle() {
131 if (bodyTrackerManager.HasDetectedBodies()) {
132 fsm.ChangeState((int)State.Play);
133 }
134 }
135
136 private void OnExitIdle() {
137 gameInputActions.Game.Start.Disable();
138 }
139
140 // Play State (You may not have to use all of these functions. I am just creating a template for you.)
141 private void OnEnterPlay() {
142 gameInputActions.VoiceChat.PushToTalk.Enable();
143
144 foreach (var cloud in clouds)
145 {
146 cloud.FadeOut();
147 }
148
149 // Tell the voice chat system to start.
151
152 // Switch BGMs.
153 waitBGM.Stop();
154 playBGM.Play();
155 }
156
157 private void OnUpdatePlay() {
158 bool isATalking = animationReferences[0].audioSource != null && animationReferences[0].audioSource.isPlaying;
159 bool isPTalking = animationReferences[1].audioSource != null && animationReferences[1].audioSource.isPlaying;
160
161 if (animationReferences[0].animator != null)
162 {
163 animationReferences[0].animator.SetBool(animationReferences[0].actorIsTalkingBool, isATalking);
164 animationReferences[0].animator.SetBool(animationReferences[1].actorIsTalkingBool, isPTalking);
165 }
166 if (animationReferences[1].animator != null)
167 {
168 animationReferences[1].animator.SetBool(animationReferences[1].actorIsTalkingBool, isPTalking);
169 animationReferences[1].animator.SetBool(animationReferences[0].actorIsTalkingBool, isATalking);
170 }
171 }
172
173 private void OnExitPlay() {
174 gameInputActions.VoiceChat.PushToTalk.Disable();
175 }
176
177 // Finish State (You may not have to use all of these functions. I am just creating a template for you.)
178 private void OnEnterFinish() {
179 gameInputActions.Game.Continue.Enable();
180 hasFadedIn = false;
181 }
182
183 private void OnUpdateFinish() {
184 // Fade the clouds back in.
185 fadeInTimer += Time.deltaTime;
187 {
188 foreach (var cloud in clouds)
189 {
190 cloud.FadeIn();
191 }
192 hasFadedIn = true;
193 }
194 }
195
196 private void OnExitFinish() {
197 gameInputActions.Game.Continue.Disable();
198 }
199
200 // Input Callbacks
201 private void OnStart(InputAction.CallbackContext context) {
202 // Start the game by transiting to the Play state.
203 fsm.ChangeState((int)State.Play);
204 }
205
206 private void OnRestart(InputAction.CallbackContext context) {
207 SceneManager.LoadScene(SceneManager.GetActiveScene().name);
208 }
209
210 private void OnContinue(InputAction.CallbackContext context) {
211 // return to play state from finish state
212 fsm.ChangeState((int)State.Play);
213
214 // Trigger the Game Continue event.
216 }
217
218 private void OnQuit(InputAction.CallbackContext context) { Application.Quit(); }
219
220 private void OnPushToTalkStarted(InputAction.CallbackContext context) {
221 foreach (var animRef in animationReferences)
222 {
223 if (animRef.animator != null)
224 {
225 animRef.animator.SetBool(animRef.pushToTalkStartedBool, true);
226 animRef.animator.SetBool(animRef.pushToTalkCancelledBool, false);
227 }
228 }
229 }
230
231 private void OnPushToTalkCancelled(InputAction.CallbackContext context) {
232 foreach (var animRef in animationReferences)
233 {
234 // play the thinking SFX
235 AudioSource sfx = animRef.GetRandomSFXVariant();
236 if (sfx != null)
237 {
238 sfx.Play();
239 }
240 // update animator
241 if (animRef.animator != null)
242 {
243 animRef.animator.SetBool(animRef.pushToTalkStartedBool, false);
244 animRef.animator.SetBool(animRef.pushToTalkCancelledBool, true);
245 }
246 }
247 }
248
249 // Game Event Callbacks
250 private void OnGameFinish() {
251 // Transit to the finish state when we receive the GameFinish event.
252 fsm.ChangeState((int)State.Finish);
253 }
254 }
255}
Helper class to group references needed for animation.
Definition: GameManager.cs:21
GameManager controls the gameplay animation and audio.
Definition: GameManager.cs:8
Spelunx.Orbbec.BodyTrackerManager bodyTrackerManager
Definition: GameManager.cs:38
FSM.FiniteStateMachine fsm
Definition: GameManager.cs:48
void OnPushToTalkCancelled(InputAction.CallbackContext context)
Definition: GameManager.cs:231
void OnQuit(InputAction.CallbackContext context)
Definition: GameManager.cs:218
void OnContinue(InputAction.CallbackContext context)
Definition: GameManager.cs:210
GameInputActions gameInputActions
Definition: GameManager.cs:47
State
GameManager states.
Definition: GameManager.cs:10
@ Idle
Wait for the player to start the game.
void OnPushToTalkStarted(InputAction.CallbackContext context)
Definition: GameManager.cs:220
AnimationReferences[] animationReferences
Definition: GameManager.cs:41
void OnRestart(InputAction.CallbackContext context)
Definition: GameManager.cs:206
void OnStart(InputAction.CallbackContext context)
Definition: GameManager.cs:201
EchoTrio.UI.FadeEffect[] clouds
Definition: GameManager.cs:37
Finite state machine class to handle state transitions and updates.
void UnsubscribeFromEvent(string eventName, UnityAction unityAction)
void TriggerEvent(string eventName)
void SubscribeToEvent(string eventName, UnityAction unityAction)
static GameEventSystem GetInstance()