/
GameControl.cs
498 lines (416 loc) · 19.3 KB
/
GameControl.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using ZLibrary;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameControl : MonoBehaviour
{
public List<GameObject> mapsList; //префаби можливих мап, встановити в редакторі
private MapInfo map; //поточна мапа
public GameObject player; //об'єкт гравця, отримується з мапи
// private List<SphereRespawn> sphereRespawnsList; //точки генерації куль, береться з мапи
//public List<AccTrigger> accTriggers; //тригери прискорення
//Кулі гравця. Встановлюється в редакторі
public Transform ballTransform;
public Transform explosiveBallTransform;
public Transform multicolorBallTransform;
public Transform beaverBallTransform;
private GameObject newSphere;
private GameObject launchSphere;
public Transform moveTo; //змінити (костиль в якості пустого об'єкта до якого рухатись. ставиться в редакторі)
// SphereRespawn[] respawns;
SaveInfo save;
SaveLoadGame saveLoadManager;
//Ефекти
public GameObject explode;
public AudioSource destroyBallSound;
public AudioSource launchBallSound;
public AudioSource salutSound;
public GameObject winSalut;
public Text textCountExplosive; //текстові поля куль, встановлюються в редакторі
public Text textCountMulticolor;
public Text textCountBeaver;
public Text textCountTimestop;
public ProgressBar scoreProgressBar; //прогрес бар набраних балів. Встановити в редакторі
private int score; //поточні зароблені бали
private int scoreToFinal; //бали які необхідно заробити до перемоги в рівні
private int countColor; //Кількість кольорів у куль
public int CountColor //кількість колькорів, від 1 до 4.
{
get { return countColor; }
set
{
if (value > 4)
countColor = 4;
else if (value < 1)
countColor = 1;
else
countColor = value;
}
}
// private List<GameObject>[] destroyLists;
public float speed;
[SerializeField]
public float Speed
{
get
{
return speed + (float)save.skill[(int)PlayerSkill.SK_BONUS_SPEED] * 1.1f;
}
set
{
speed = value;
}
}
BallCreator ballCreator = new BallCreator();
void Awake()
{
// BallController.RedyToRunNewPlayerBall=true; //костиль, виклик конструктора
ballCreator = new BallCreator();
}
void Start()
{
// Debug.Log("-=3=-");
// BallController.RedyToRunNewPlayerBall = false;
if (explosiveBallTransform==null)
{
Debug.LogError("explosiveBallTransform==null");
}
//Завантажити збереження
saveLoadManager = new SaveLoadGame();
save = saveLoadManager.LoadSave();
Debug.Log(save.curLvl);
// створити мапу, відповідно до рівня та отримати її скрипт
map = createMapGameobject(save.curLvl).GetComponent<MapInfo>();
player = map.player;
TextUpdate();
LevelPreference(save.curLvl);
TypesSphere typeSphere = ballCreator.randomType(true, CountColor);
newSphere = ballCreator.getBall(ballTransform, map.pointToRespawnPlayersBall.position, typeSphere).gameObject;
// newSphere.GetComponent<BallBehaviour>().TypeSphere = typeSphere;
// destroyLists = BallController.BallsLists;
}
//BallController.BallsLists - списки з кулями.
// 1-4 списки (індекса 0-3) зарезервовані під респауни з тими ж індексами
// 5-тий список (індекс 4) - зарезервований під взаємодію куль (крайніх куль в локальних послідовностях)
// 6-тий список (індекс 5) - для безумовного знищення куль.
void Update()
{
if (map.CountDestroyBalls>4)
GameOverProcess();
if (score >= scoreToFinal)
{
scoreToFinal += 1000;
GameWinProcess();
}
TextUpdate();
//Очистка шостого списка
if (BallController.BallsLists[5] != null)
{
foreach (GameObject ball in BallController.BallsLists[5])
DestroyProcess(ball);
BallController.BallsLists[5].Clear();
}
if (BallController.BallsLists == null)
return;
if (!BallController.readyToDestroy)
{
// SafeBallDestroy();
return;
}
//return;
BallController.readyToDestroy = false; //закрити доступ до перевірки на знищення, до наступного відкриття в коді
//Очистка списка один-п'ять
for (int i = 0; i < 4; i++)
{
if (BallController.BallsLists[i] == null)
continue;
// Debug.Log("3_destroyList.count=" + destroyList.Count);
if (BallController.BallsLists[i].Count < 3)
{
BallController.BallsLists[i].Clear();
continue;
}
//Debug.Log(BallController.lastForwardBallIndex);
//Destroy(balls[BallController.lastForwardBallIndex], 0.2f);
// GameObject localLastBall = BallController.BallsLists[i][BallController.lastForwardBallIndex].GetComponent<BallBehaviour>().FrontBall;
for (int j = 0; j < BallController.BallsLists[i].Count; j++)
{
// if(destroyLists[i][j].GetComponent<BallBehaviour>().Health<1)
if (BallController.BallsLists[i][j] == null)
continue;
DestroyProcess(BallController.BallsLists[i][j]);
/*int AdvencedBallIndex = BallController.BallsLists[i][j].GetComponent<BallBehaviour>().AdvencedBallIndex;
if (AdvencedBallIndex>-1)
{
switch (AdvencedBallIndex)
{
case 0:
save.curCountMulticolor += save.curCountMulticolor < (save.maxCountMulticolor + save.skill[(int)PlayerSkill.SK_BONUS_MAX_MULTICOLOR]) ? 1 : 0;
break;
case 1:
save.curCountBeaver += save.curCountBeaver < (save.maxCountBeaver + save.skill[(int)PlayerSkill.SK_BONUS_MAX_BEAVER]) ? 1 : 0;
break;
case 2:
save.curCountExplosive += save.curCountExplosive < (save.maxCountExplosive + save.skill[(int)PlayerSkill.SK_BONUS_MAX_EXPLOSIVE]) ? 1 : 0;
break;
case 3:
save.curCountTimestop += save.curCountTimestop < (save.maxCountTimestop + save.skill[(int)PlayerSkill.SK_BONUS_MAX_TIMESTOP]) ? 1 : 0;
break;
default:
break;
}
}
Destroy(BallController.BallsLists[i][j]);*/
score++;
}
BallController.BallsLists[i].Clear();
}
if (launchSphere == null)
{
BallController.RedyToRunNewPlayerBall = true;
}
else if(launchSphere.tag!="player")
BallController.RedyToRunNewPlayerBall = true;
// SafeBallDestroy();
}
private void DestroyProcess(GameObject ball)
{
int AdvencedBallIndex = ball.GetComponent<BallBehaviour>().AdvencedBallIndex;
if (AdvencedBallIndex > -1)
{
switch (AdvencedBallIndex)
{
case 0:
save.curCountMulticolor += save.curCountMulticolor < (save.maxCountMulticolor + save.skill[(int)PlayerSkill.SK_BONUS_MAX_MULTICOLOR]) ? 1 : 0;
break;
case 1:
save.curCountBeaver += save.curCountBeaver < (save.maxCountBeaver + save.skill[(int)PlayerSkill.SK_BONUS_MAX_BEAVER]) ? 1 : 0;
break;
case 2:
save.curCountExplosive += save.curCountExplosive < (save.maxCountExplosive + save.skill[(int)PlayerSkill.SK_BONUS_MAX_EXPLOSIVE]) ? 1 : 0;
break;
case 3:
save.curCountTimestop += save.curCountTimestop < (save.maxCountTimestop + save.skill[(int)PlayerSkill.SK_BONUS_MAX_TIMESTOP]) ? 1 : 0;
break;
default:
break;
}
}
//Звук
if (destroyBallSound != null)
destroyBallSound.Play();
//Анімація
if (explode != null)
{
Vector3 particlePos = new Vector3(ball.transform.position.x, ball.transform.position.y, -2);
GameObject explodeParticleSystem = Transform.Instantiate(explode, particlePos, Quaternion.identity);
Destroy(explodeParticleSystem.gameObject, 0.5f);
}
Destroy(ball);
}
void OnMouseDown()
{
/*if (launchSphere == null)
{
BallController.RedyToRunNewPlayerBall = true;
}
else if (launchSphere.tag != "player")
BallController.RedyToRunNewPlayerBall = true;*/
if (!BallController.RedyToRunNewPlayerBall)
return;
//Debug.Log("-=2=-");
BallController.RedyToRunNewPlayerBall = false;
if (moveTo==null)
{
Debug.Log("не вказаний moveTo об'єкт в редакторі");
return;
}
// moveToPos.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
moveTo.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
moveTo.position = new Vector3(moveTo.position.x-map.player.transform.position.x, moveTo.position.y- map.player.transform.position.y, 0);
//Debug.Log("coord X=" + moveTo.position.x);
//Debug.Log("coord Y=" + moveTo.position.y);
//Vector3 relativePos = moveTo.position - player.transform.position;
// transform.rotation = Quaternion.LookRotation(player.transform.forward, relativePos);
// player.transform.LookAt((new Vector3(moveTo.position.x, moveTo.position.y, player.transform.position.z ));
player.transform.rotation = Quaternion.LookRotation(transform.forward, new Vector3(moveTo.position.x, moveTo.position.y, player.transform.position.z));
newSphere.transform.position = map.pointToRespawnPlayersBall.position;
//newSphere.tag = "player";
SphereBehaviour sb = newSphere.GetComponent<SphereBehaviour>();
sb.Speed = Speed;
sb.Move(moveTo);
launchSphere = newSphere;
//Звук
if (launchBallSound!=null)
launchBallSound.Play();
TypesSphere typeSphere = ballCreator.randomType(true, CountColor);
newSphere = ballCreator.getBall(ballTransform, map.pointToRespawnPlayersBall.position, typeSphere).gameObject;
sb = newSphere.GetComponent<SphereBehaviour>();
//sb.TypeSphere = typeSphere;
//Debug.Log(sb.TypeSphere);
// newSphere = null; //прибрати
}
/*void SafeBallDestroy()
{
if (destroyLists[5].Count > 0) //безпечне знищення куль, що підійшли до точки знищення
{
for (int i = 0; i < destroyLists[5].Count; i++)
if (destroyLists[5][i] != null)
Destroy(destroyLists[5][i]);
destroyLists[5].Clear();
}
}*/
//private Vector3 BallPosicion() => new Vector3(player.transform.position.x-0.2f, player.transform.position.y+3.1f, -2.0f);
//повертає мапу, відповідно до рівня
private GameObject createMapGameobject(int curMapLvl)
{
int count = mapsList.Count;
Debug.Log("curMapLvl=" + curMapLvl);
Debug.Log("mapsList.Count=" + mapsList.Count);
if (curMapLvl < 10)
count = mapsList.Count > 7 ? 7 : mapsList.Count;
Debug.Log("count=" + count);
int randomMapIndex = Random.Range(0, count);
Debug.Log("randomMapIndex=" + randomMapIndex);
//randomMapIndex = 0;
return GameObject.Instantiate(mapsList[randomMapIndex]);
}
private void TextUpdate() //оновлення текстових полів головного екрану
{
textCountExplosive.text = save.curCountExplosive.ToString();// + "/" + (save.maxCountExplosive + save.skill[(int)PlayerSkill.SK_BONUS_MAX_EXPLOSIVE]);
textCountMulticolor.text = save.curCountMulticolor.ToString() /*+ "/" + (save.maxCountMulticolor + save.skill[(int)PlayerSkill.SK_BONUS_MAX_MULTICOLOR])*/;
textCountBeaver.text = save.curCountBeaver.ToString() /*+ "/" + (save.maxCountBeaver + save.skill[(int)PlayerSkill.SK_BONUS_MAX_BEAVER])*/;
textCountTimestop.text = save.curCountTimestop.ToString() /*+ "/" + (save.maxCountTimestop + save.skill[(int)PlayerSkill.SK_BONUS_MAX_TIMESTOP])*/;
scoreProgressBar.currentValue = score;
//Debug.Log("scoreProgressBar.currentValue = "+scoreProgressBar.currentValue);
//Debug.Log("score = " + score);
}
private void LevelPreference(int curLvl)
{
if (curLvl < 15)
CountColor = 4;
else
countColor = 5;
foreach (var resp in map.sphereRespawnsList)
resp.CountColor = CountColor;
scoreToFinal = 100 + ((curLvl * curLvl)/3); //бали які необхідно заробити до перемоги в рівні
scoreProgressBar.currentValue = score;
scoreProgressBar.fullValue = scoreToFinal;
if (map.sphereRespawnsList != null)
foreach (var resp in map.sphereRespawnsList)
{
resp.BaseSpeed = 0.8f + (float)curLvl / 125.0f;
foreach (var triger in resp.accTrigger)
{
triger.currentMapLvl = curLvl;
}
}
}
public void AddScore(int count)
{
score += count > 0 ? count : 0;
Debug.Log("Score=" + score);
}
private void GameOverProcess()
{
SaveLoadGame.SaveGame(save);
SceneManager.LoadScene(0);
}
private void GameWinProcess()
{
if ((float)save.skill[(int)PlayerSkill.SK_BONUS_EXP] > 0)
score = (score * 110) / 100;
save.playerExp += (score * 8);
save.maxOpenLvl++;
SaveLoadGame.SaveGame(save);
StartCoroutine(GameWinProcessCoroutine());
//Debug.Log("-=1=-");
BallController.RedyToRunNewPlayerBall = false;
if (winSalut != null)
winSalut.SetActive(true);
//Звук
if (salutSound != null)
salutSound.Play();
if (map.sphereRespawnsList != null)
foreach (var resp in map.sphereRespawnsList)
resp.Speed = 0;
//Очистка 5-го списка. Знищення наявних на екрані куль
if (BallController.BallsLists[4] != null)
{
for (int i = 0; i < BallController.BallsLists[4].Count; i++)
{
if (BallController.BallsLists[4][i] != null)
DestroyProcess(BallController.BallsLists[4][i]);
}
}
}
public IEnumerator GameWinProcessCoroutine()
{
while (true)
{
yield return new WaitForSeconds(7.0f);
SceneManager.LoadScene(0);
}
}
#if UNITY_ANDROID && !UNITY_EDITOR
private void OnApplicationPause(bool pause)
{
if(pause) SaveLoadGame.SaveGame(save);
}
#endif
private void OnApplicationQuit()
{
SaveLoadGame.SaveGame(save);
}
public void OnButtonBeaver()
{
if (save.curCountBeaver < 1)
return;
Destroy(newSphere);
TypesSphere typeSphere = TypesSphere.BEAVER;
newSphere = ballCreator.getBall(beaverBallTransform, map.pointToRespawnPlayersBall.position, typeSphere).gameObject;
BallBeaver advencedBall = newSphere.GetComponent<BallBeaver>();
advencedBall.PowerBeaverSkill = save.skill[(int)PlayerSkill.SK_BEAVER_POWER];
advencedBall.scoreMessage += AddScore;
save.curCountBeaver--;
}
public void OnButtonMulticolor()
{
if (save.curCountMulticolor < 1)
return;
Destroy(newSphere);
TypesSphere typeSphere = TypesSphere.MULCOLOR;
newSphere = ballCreator.getBall(multicolorBallTransform, map.pointToRespawnPlayersBall.position, typeSphere).gameObject;
BallMulticolor advencedBall = newSphere.GetComponent<BallMulticolor>();
advencedBall.powerMulticolorSkill = save.skill[(int)PlayerSkill.SK_MULTICOLOR_POWER];
Debug.Log("newSphere type="+ newSphere.GetComponent<BallBehaviour>().TypeSphere);
save.curCountMulticolor--;
}
public void OnButtonTimestop()
{
if (save.curCountTimestop < 1)
return;
foreach (var resp in map.sphereRespawnsList)
{
if (resp.Speed <= resp.BaseSpeed)
resp.StartCoroutine(resp.TimestopBallsCoroutine(save.skill[(int)PlayerSkill.SK_BONUS_MAX_TIMESTOP]));
else
return;
}
save.curCountTimestop--;
}
public void OnButtonExplosive()
{
if (save.curCountExplosive < 1)
return;
Destroy(newSphere);
TypesSphere typeSphere = TypesSphere.EXPLOSIVE;
newSphere = ballCreator.getBall(explosiveBallTransform, map.pointToRespawnPlayersBall.position, typeSphere).gameObject;
BallExplosive advencedBall = newSphere.GetComponent<BallExplosive>();
advencedBall.rangExplosiveSkill = save.skill[(int)PlayerSkill.SK_BONUS_EXP];
advencedBall.scoreMessage += AddScore;
save.curCountExplosive--;
}
}