-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
187 lines (163 loc) · 4.51 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public static GameManager instance = null;
[Header("Challenge Options... /Skip if not a challenge")]
[SerializeField] bool isChallenge;
public int challengeID;
public int maxRoundsForLevel;
GameObject ballShooter;
[SerializeField] GameObject loseFx;
[Header("Set These Items")]
[SerializeField] GameObject activeBallPool;
[SerializeField] GameObject shredder;
[Space]
[Header("Time Options")]
[SerializeField] int fastTimeScale = 3;
[SerializeField] int timeToShowFastButton = 5;
float timePastShoot;
[HideInInspector] public bool hasStarted = false;
[HideInInspector] public int level = 1;
[HideInInspector] public bool fastTimer;
[HideInInspector] public int newBalls;
[HideInInspector] public bool specialBallOn;
bool checkForTurn = true;
bool lastChance = true;
bool isWinner;
Vector3 moveDist = new Vector3 (0,-1,0);
GameObject[] onScreenObj;
public delegate void NextTurn();
public static event NextTurn toNextTurn;
void Awake () {
if (instance == null){
instance = this;
}else if (instance !=this){
Destroy(gameObject);
}
ballShooter = GameObject.FindGameObjectWithTag("Shooter");
toNextTurn += BoolReset;
toNextTurn += SetNewBallsToShooter;
toNextTurn += ResumeTime;
}
void Update () {
CheckForPanel();
if(checkForTurn){
CheckForTurn();
}
if(isChallenge){
StartCoroutine("CheckForWin");
CheckForChallengeDeath();
}
if (hasStarted && !fastTimer) {
timePastShoot += Time.deltaTime;
if (timePastShoot >= timeToShowFastButton) {
UiCanvas.instance.FastButton(true);
}
}
}
void BoolReset(){
level++;
fastTimer = false;
hasStarted = false;
specialBallOn = false;
timePastShoot = 0;
}
public void LowerScreenObjects(){
onScreenObj = GameObject.FindGameObjectsWithTag("Moovable");
foreach (GameObject thing in onScreenObj){
thing.transform.position += moveDist;
}
if (SpawnerGroups.instance != null){
SpawnerGroups.instance.isTimeToSpawn = true;
}
}
IEnumerator CheckForWin(){
GameObject[] bricks;
bricks = GameObject.FindGameObjectsWithTag("Breakable");
if (bricks.Length == 0){
isWinner = true;
yield return new WaitForSeconds(0.2f);
UiCanvas.instance.Win();
checkForTurn = false;
SaveChallengeButtons.instance.SetCompleted(challengeID);
SaveChallengeButtons.instance.Save();
StopCoroutine("CheckForWin");
}
}
void CheckForChallengeDeath(){
if (level > maxRoundsForLevel && !isWinner){
BallShooter.instance.dead = true;
loseFx.SetActive(true);
Invoke("Death",3f);
}
}
public void Death(){
UiCanvas.instance.Death(true);
GooglePlay.AddScoreToLeaderboard(EBGPSIds.leaderboard_elemental_ballz, level);
}
void SetNewBallsToShooter (){
int i = 0;
while (i < newBalls) {
BallShooter.instance.SetExtraBall ();
i++;
}
newBalls = 0;
}
void CheckForTurn(){
int ballsToShooter = ballShooter.transform.childCount;
int ballsInList = BallShooter.instance.stockBalls.Count;
if (ballsToShooter >= ballsInList && hasStarted || ballsToShooter == 1 && hasStarted && specialBallOn ){
if(toNextTurn != null){
toNextTurn();
LoseCollider.instance.SetPositionForShoot();
BallShooter.instance.SetBalls();
BallShooter.instance.SetFrames();
LowerScreenObjects();
UiManager.instance.MakeUpdates();
SaveManager.instance.Save();
}
}
}
public void OneMoreTry(){
if (!isChallenge){
if (lastChance){
LoseCollider.instance.ParticleReset();
UiCanvas.instance.Death(false);
shredder.SetActive(true);
Destroy(shredder,0.1f);
lastChance = false;
}
}else if(isChallenge){
if (lastChance){
UiCanvas.instance.Death(false);
lastChance = false;
}
}
}
void CheckForPanel(){
UiPanel panel = GameObject.FindObjectOfType<UiPanel>();
if (panel == null && !fastTimer){
ResumeTime();
}else if(fastTimer || panel.gameObject.tag == "WinPanel"){
return;
}
else if(panel.isActiveAndEnabled){
Invoke("PauseTime", 0.3f);
}
}
public void FastTime(){
fastTimer = true;
Time.timeScale = fastTimeScale;
UiCanvas.instance.FastButton(false);
}
public void PauseTime(){
Time.timeScale = 0;
}
public void ResumeTime(){
Time.timeScale = 1;
fastTimer = false;
UiCanvas.instance.FastButton(false);
}
}