-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.cs
318 lines (275 loc) · 8.96 KB
/
Sound.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(AudioSource))]
public class Sound : MonoBehaviour
{
#region Variables
private AudioSource OpeningTheme;
private AudioSource GameSound;
private AudioSource PointSound;
private AudioSource[] GameSounds;
private int lives;
private int rewardPoints;
private int rand;
private int points;
private int streak;
private int countToStreak;
private bool isLeft;
private bool playing;
private float pan;
private float TimerPlaceholder;
private List<GameObject> Lives;
private List<GameObject> Panels;
private GameObject OpeningPanel;
private GameObject InstructionsPanel;
private GameObject GamePanel;
private GameObject PausePanel;
private GameObject SummaryPanel;
private Slider TimeSlider;
private Color GamePanelColor;
public float Timer;
public GameObject PointsPrefab;
#endregion
#region Methods
//Start method
private void Start()
{
TimerPlaceholder = Timer;
OpeningPanel = GameObject.Find("Opening");
InstructionsPanel = GameObject.Find("Instructions");
GamePanel = GameObject.Find("Game");
GamePanelColor = GamePanel.GetComponent<Image>().color;
PausePanel = GameObject.Find("Pause");
SummaryPanel = GameObject.Find("Summary");
TimeSlider = GamePanel.transform.Find("Slider").GetComponent<Slider>();
Panels = new List<GameObject>();
foreach (Transform item in GameObject.Find("Canvas").transform)
{
Panels.Add(item.gameObject);
}
GameSounds = GetComponents<AudioSource>();
OpeningTheme = GameObject.Find("Canvas").GetComponent<AudioSource>();
MainMenu();
}
//Go back to main menu
public void MainMenu()
{
ShowPanel(OpeningPanel);
if (!OpeningTheme.isPlaying)
OpeningTheme.Play();
}
//Checks if you should get a point or lose
private void Point(bool score)
{
playing = false;
GamePanel.GetComponent<Image>().color = GamePanelColor;
StopAllCoroutines();
//Correct
if (score)
{
PointSound = GamePanel.GetComponents<AudioSource>()[0];
PointSound.Play();
rewardPoints = streak * (int)(Mathf.Ceil((TimeSlider. value) * 10));
points += rewardPoints;
PopupPoints();
if (points > PlayerPrefs.GetInt("Highscore"))
{
PlayerPrefs.SetInt("Highscore", points);
}
countToStreak++;
if (countToStreak == 6)
{
streak++;
countToStreak = 0;
Timer += 0.0012f;
}
}
//Wrong
else
{
lives--;
if (lives <= 0)
{
Summary();
return;
}
PointSound = GamePanel.GetComponents<AudioSource>()[1];
PointSound.Play();
streak = 1;
countToStreak = 0;
GameObject lifeToKill = Lives[Lives.Count - 1];
lifeToKill.GetComponent<RawImage>().enabled = false;
Lives.Remove(lifeToKill);
}
//Start another game in 0.25 seconds
Invoke("Play", 0.25f);
}
//After answering correctly, popup the number of points given
private void PopupPoints()
{
PointsPrefab.GetComponent<Text>().text = "+" + rewardPoints.ToString();
Vector3 prefabLocation = new Vector3(Random.Range(-350, 333), Random.Range(-80, 160), 1);
GameObject popup = Instantiate(PointsPrefab, GameObject.Find("Canvas").transform, true) as GameObject;
popup.transform.localPosition = prefabLocation;
}
//Controls
void Update()
{
//Makes sure you're in game and not menu
if (playing)
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if (isLeft == true)
{
Point(true);
}
else
{
Point(false);
}
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (isLeft == false)
{
Point(true);
}
else
{
Point(false);
}
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Pause();
}
}
//The player clicks the start button on the menu
public void StartGame()
{
points = 0;
countToStreak = 0;
streak = 1;
Timer = TimerPlaceholder;
lives = 4;
ShowPanel(GamePanel);
//Refresh lives
Lives = new List<GameObject>() { GameObject.Find("Life1"), GameObject.Find("Life2"), GameObject.Find("Life3") };
foreach (var item in Lives)
{
item.GetComponent<RawImage>().enabled = true;
}
Play();
}
//Play the sound + Mechanics
private void Play()
{
playing = true;
if (OpeningTheme.isPlaying)
OpeningTheme.Stop();
if (GameSound != null)
GameSound.pitch = 1;
//Update text fields
GamePanel.transform.Find("Highscore").GetComponent<Text>().text = "Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
GamePanel.transform.Find("Multiplier").GetComponent<Text>().text = "Multiplier: x" + streak.ToString();
GameObject.Find("Points").GetComponent<Text>().text = points.ToString();
//Refresh time
TimeSlider.value = TimeSlider.maxValue;
//Mechanics of the game
rand = Random.Range(1, 3);
if (rand == 1)
isLeft = true;
else isLeft = false;
if (isLeft == true)
pan = Random.Range(-1f, -0.66f);
else
pan = Random.Range(1f, 0.66f);
GameSound = GameSounds[Random.Range(0, GameSounds.Length)];
GameSound.panStereo = pan;
foreach (var item in GameSounds)
{
item.Stop();
}
#region OPPOSITE MODE
//Opposite mode:
//Pick a random number between 1 and 6.
if (points >= 100)
{
rand = Random.Range(1, 7);
//If isLeft is true make it false, and vice versa, also change color of background for player's clarity
//Thus, changing the direction, if the sound is coming from the left, you need to click right to win
if (rand == 1)
{
if (isLeft)
isLeft = false;
else
isLeft = true;
GamePanel.GetComponent<Image>().color = new Color(0.525f, 0.572f, 0.572f);
GameSound.pitch = 1.6f;
}
}
#endregion
GameSound.Play();
//StopAllCoroutines();
StartCoroutine(SliderMechanics(Timer));
}
//Shows a specific panel and disables the rest of the array
private void ShowPanel(GameObject panel)
{
foreach (var item in Panels)
{
item.SetActive(false);
}
panel.SetActive(true);
}
//Instructions button
public void Instructions()
{
ShowPanel(InstructionsPanel);
}
//Returns to the main menu
public void Return()
{
ShowPanel(OpeningPanel);
}
//Pause
private void Pause()
{
playing = false;
StopAllCoroutines();
ShowPanel(PausePanel);
}
//User has lost his 4 lives and goes to game summary
private void Summary()
{
playing = false;
ShowPanel(SummaryPanel);
OpeningTheme.Play();
SummaryPanel.transform.Find("Text_endScore").gameObject.GetComponent<Text>().text = "Score: " + points.ToString();
//Show "new highscore" or "nice try.." depends on points relative to old highscore
if (points >= PlayerPrefs.GetInt("Highscore") && points != 0)
SummaryPanel.transform.Find("Text_endTitle").gameObject.GetComponent<Text>().text = "NEW HIGHSCORE!";
else
SummaryPanel.transform.Find("Text_endTitle").gameObject.GetComponent<Text>().text = "NICE TRY..";
}
//Exits the game
public void Quit()
{
Application.Quit();
}
//IEnums to control time with 'WaitForSeconds'
IEnumerator SliderMechanics(float time)
{
while (TimeSlider.value > 0)
{
yield return new WaitForSeconds(0.001f);
TimeSlider.value -= time;
}
Point(false);
}
#endregion
}