-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelManager.cs
178 lines (144 loc) · 4.31 KB
/
LevelManager.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
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
public class LevelManager : MonoBehaviour {
static LevelManager instance;
const int MAX_LEVEL_TRIES = 3;
const float PERCENT_TO_NEXT_LEVEL = 60;
int currentLevel;
int currentLevelTries;
int countOfAquaEnemyToCreate = 1;
int countOfLandEnemyToCreate = 1;
float progressPercentage;
float percentOfGameForOneCell;
[SerializeField]
GameObject enemyAquaPrefab;
[SerializeField]
GameObject enemyLandPrefab;
[SerializeField]
GameObject mainHeroPrefab;
[SerializeField]
GameObject endGamePanel;
List<EnemyFigure> enemyFiguresOnScene;
private void Awake()
{
instance = this;
enemyFiguresOnScene = new List<EnemyFigure>();
currentLevel = 1;
currentLevelTries = MAX_LEVEL_TRIES;
}
private void Start()
{
int cellsToDiscover = (Board.BoardCellWidth * Board.INSTANCE.BoardCellHeight) -
(Board.BoardCellWidth * 2 + Board.INSTANCE.BoardCellHeight * 6);
percentOfGameForOneCell = 100f / cellsToDiscover;
}
public void GoToNextLevel()
{
if (currentLevel - 1 % 2 == 0)
countOfAquaEnemyToCreate++;
else if (currentLevel % 3 == 0)
countOfLandEnemyToCreate++;
DestroyAllFigures();
currentLevel++;
ResetAllAndSpawn();
}
public void ResetAllAndSpawn()
{
TextContoller.INSTANCE.ResetTexts();
Board.INSTANCE.ResetBoard();
SpawnFigures();
currentLevelTries = 3;
progressPercentage = 0;
}
public void ResetCurrentLevel()
{
currentLevel = 1;
countOfAquaEnemyToCreate = 1;
countOfLandEnemyToCreate = 1;
}
public void OnHeroDie()
{
if (currentLevelTries > 1)
{
DestroyAllFigures();
SpawnFigures();
currentLevelTries--;
TextContoller.INSTANCE.ShowLivesLeft(currentLevelTries);
}
else
EndGame();
}
public void EndGame()
{
DestroyAllFigures();
TextContoller.INSTANCE.MakeDarker();
TextContoller.INSTANCE.SetLevelsDone(currentLevel - 1);
TextContoller.INSTANCE.StopTimer();
endGamePanel.SetActive(true);
}
public void SpawnFigures()
{
if (SwipeController.gamePaused)
Time.timeScale = 0;
TextContoller.INSTANCE.StartTimer();
if (enemyFiguresOnScene.Count > 0)
enemyFiguresOnScene.Clear();
for(int i=0; i < countOfAquaEnemyToCreate; i++)
SpawnFigure(enemyAquaPrefab);
for(int i=0; i < countOfLandEnemyToCreate; i++)
SpawnFigure(enemyLandPrefab);
SpawnFigure(mainHeroPrefab);
}
void SpawnFigure(GameObject figurePrefab)
{
GameObject enemyGO = Instantiate(figurePrefab);
Figure figure = enemyGO.GetComponent<Figure>();
if (enemyGO.GetComponent<EnemyFigure>())
enemyFiguresOnScene.Add((EnemyFigure)figure);
else if (enemyGO.GetComponent<MainHero>())
SwipeController.mainHero = (MainHero)figure;
}
void DestroyAllFigures()
{
foreach (var figure in enemyFiguresOnScene)
Destroy(figure.gameObject);
Destroy(SwipeController.mainHero.gameObject);
}
public List<EnemyFigure> GetEnemiesOnScene()
{
return enemyFiguresOnScene;
}
public void DestroyEnemy(EnemyFigure figure)
{
enemyFiguresOnScene.Remove(figure);
Destroy(figure.gameObject);
}
public void IncreaseScore(int cellDestroyed)
{
progressPercentage += cellDestroyed * percentOfGameForOneCell;
TextContoller.INSTANCE.ShowProgress(progressPercentage);
}
public void CheckIfEnemiesDied()
{
if (AquaEnemiesCount <= 0 || progressPercentage >= PERCENT_TO_NEXT_LEVEL)
GoToNextLevel();
}
public int CurrentLevel
{
get { return currentLevel; }
}
public int AquaEnemiesCount
{
get
{
return enemyFiguresOnScene.Select(cell => cell)
.Where(cell => cell.CellMoveType == BoardCellType.AQUA).Count();
}
}
static public LevelManager INSTANCE
{
get { return instance; }
}
}