-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
237 lines (189 loc) · 6.36 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
//Declaring all float variables
public float BallSpeed = 25f;
//Declaring all relevant GameObjects
public GameObject Ball;
public GameObject BallPrefab;
public GameObject Music;
public GameObject LoseGame;//Defeat message
public GameObject WinGame; //Ordinary victory message
public GameObject AdviceWin1; //Message that appears during an ordinary Win condition
public GameObject SpecialWinGame; //Special victory message
public GameObject AdviceWin2; //Message that appears during a special Win condition
public GameObject RestartGame; //Appears at the end of the game
public GameObject DeathNotice; //Appears after ball respawns
public GameObject BreakOut; //Game title
public Transform BallOrigin;
//Declaring all int variables
public int LivesCount = 3; //Counts the number of lives left
public int ScoreCount = 0; //Counts the total score achieved
//Declaring all UI Texts
public Text GameMsg;
public Text LoseMsg;
public Text WinMsg;
public Text AdviceMsg1;
public Text SpecialWinMsg;
public Text AdviceMsg2;
public Text RestartMsg;
public Text Lives;
public Text Tutorial;
public Text DeathMsg;
public Text BreakOutMsg;
public Text Score;
//Declaring all boolean variables
public bool GameStart = false;
public bool GameEnd = false;
public bool CanRestart = false;
public bool Death = false;
public bool DeathMsgActivate = false;
Rigidbody rb;
//Declaring all variables for Game Audio
public AudioClip SoundEffect;
private AudioSource source;
//Awake
void Awake(){
Screen.SetResolution (1024, 768, false);
}
//Initialization
void Start () {
rb = Ball.GetComponent<Rigidbody> ();
source = GetComponent<AudioSource> ();
Lives.text = "Lives: "+LivesCount;
GameMsg.text = "Destroy all blocks to win the game.\nGoodluck and Have fun!\n\n\nPress Space to Start";
LoseMsg.text = "GAME OVER\nTry again next time!";
WinMsg.text = "CONGRATULATIONS\nYou've won the game!";
AdviceMsg1.text = "Try aiming for 1000 score!\n(Hint: Lives give extra score points!)";
SpecialWinMsg.text = "CONGRATULATIONS\nYou've truly won the game!";
AdviceMsg2.text = "Now, no one will ever surpass your achievement!";
RestartMsg.text = "Press Space to Try Again";
Tutorial.text = "Controls:\n← to move Left\n→ to move Right\n\nSpacebar\nto Confirm";
DeathMsg.text = "Press Space to Launch Ball";
BreakOutMsg.text = "BREAKOUT";
Score.text = "Score: " +ScoreCount;
DeathNotice.SetActive (false);
LoseGame.SetActive (false);
WinGame.SetActive (false);
AdviceWin1.SetActive (false);
SpecialWinGame.SetActive (false);
AdviceWin2.SetActive (false);
RestartGame.SetActive (false);
Music.SetActive (false);
Debug.Log("Abdul Muizz bin Haji Kasim, Digital Media course, B20161128");
}
void Update () {
//Code used to begin the game at the beginning
if (GameStart == false && Input.GetKey (KeyCode.Space)) {
GameStart = true;
Destroy (GameMsg);
Music.SetActive (true);
StartGame ();
}
//Code used to tell player what to do after death
if (Death == true && Input.GetKey (KeyCode.Space)) {
DeathNotice.SetActive (false);
Death = false;
DeathMsgActivate = false;
}
if (Death == true && DeathMsgActivate == true) {
DeathNotice.SetActive (true);
Invoke ("RemoveDeathMsg", 0.5f);
}
//Code used to restart the game after it is over
if (GameEnd == true && Input.GetKey (KeyCode.Space) && CanRestart == true) {
Reset ();
}
}
//When player press Spacebar, game will start
void StartGame (){
//Randomise the initial trajectory of the ball
float randStart = Random.Range (0, 2);
Debug.Log (randStart);
if (randStart == 0) {
rb.velocity = new Vector3 (BallSpeed, 0, BallSpeed);
}else {
rb.velocity = new Vector3 (-BallSpeed , 0, BallSpeed);
}
}
//Method used to keep track of Player's lives
public void LoseLife(){
LivesCount -= 1;
Lives.text = "Lives: " + LivesCount;
if (LivesCount == 0) {
GameOver ();
} else {
Resetup ();
Death = true;
DeathMsgActivate = true;
}
}
//Method used to keep track of the Game's status (win/lose)
void GameOver(){
//Used as part as scoring system, each existing life gives extra 60 points
if (LivesCount == 3) {
ScoreCount += 180;
} else if (LivesCount == 2) {
ScoreCount += 120;
} else if (LivesCount == 1) {
ScoreCount += 60;
}
//Display Game Over message
if (LivesCount == 3 && ScoreCount >= 820) {
SpecialWinGame.SetActive (true); //Shows a special Winning screen if player never lost a life and beats the game (destroyed all cubes)
AdviceWin2.SetActive(true);
} else if (LivesCount != 3 && ScoreCount >= 820) {
WinGame.SetActive (true); //Shows an ordinary Winning screen if player beats the game (destroyed all cubes)
AdviceWin1.SetActive(true);
}else{
LoseGame.SetActive (true); //Shows a Losing screen if player doesn't beat the game or lost all lives
}
Score.text = "Score: " +ScoreCount; //Updates the score
rb.isKinematic = true; //Locks the ball in place, to prevent ball from falling again into Death Zone
GameEnd = true;
InvokeRepeating ("Restart", 2f, 1f);
}
//Method called after collision with a cube
public void DestroyCube(string type){
source.PlayOneShot (SoundEffect, 0.2f);
if (type == "Red") { //Red Cubes give 20 score points
ScoreCount += 25;
} else if (type == "Yellow") { //Yellow Cubes give 10 score points
ScoreCount += 15;
}
Score.text = "Score: " + ScoreCount;
if(ScoreCount == 820)
GameOver ();
}
//Method to create a blinking UI effect for game restart
void Restart(){
RestartGame.SetActive (true);
Invoke ("RemoveRestart", 0.5f);
CanRestart = true;
}
void RemoveRestart(){
RestartGame.SetActive (false);
}
//Method to create blinking UI effect after each respawn
void RemoveDeathMsg(){
DeathNotice.SetActive (false);
DeathMsgActivate = false;
Invoke ("ReactivateDeathMsg", 0.5f);
}
void ReactivateDeathMsg(){
DeathMsgActivate = true;
}
//Method used to respawn the ball after death
void Resetup(){
Ball = Instantiate (BallPrefab, BallOrigin.transform) as GameObject;
rb = Ball.GetComponent<Rigidbody> ();
GameStart = false;
}
//Method used to reset the game fully
void Reset(){
SceneManager.LoadScene ("Breakout");
}
}