GamingManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. using o0.Num;
  2. using o0.Wave.RealTime.Old;
  3. using Org.BouncyCastle.Security;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net.WebSockets;
  8. using TMPro;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. public enum EndGameReason
  12. {
  13. UserExit,
  14. GameOver,
  15. }
  16. public class GamingManager : MonoBehaviour
  17. {
  18. private SmartBowManager bowManager;
  19. private AudioManager audioManager;
  20. [SerializeField] private AudioSource BowFireAudio;
  21. [SerializeField] private AudioSource LooseLifeAudio;
  22. [SerializeField] private GameObject FruitPrefab;
  23. [SerializeField] private GameObject ArrowPrefab;
  24. [SerializeField] private GameObject ComboTextPrefab;
  25. [SerializeField] private GameObject CriticalTextPrefab;
  26. [SerializeField] private GameObject AddLifeTextPrefab;
  27. [SerializeField] private GameObject GamingUIContainer;
  28. [SerializeField] private Image AimingCross_img;
  29. [SerializeField] private GameObject Resume_btn;
  30. [SerializeField] private GameObject Pause_btn;
  31. [SerializeField] private GameObject Exit_btn;
  32. [SerializeField] private Image Life_0_img;
  33. [SerializeField] private Image Life_1_img;
  34. [SerializeField] private Image Life_2_img;
  35. [SerializeField] private TextMeshProUGUI ScoreText;
  36. [SerializeField] private CannonController Cannon;
  37. [SerializeField] private GameObject FullscreenClearBubble;
  38. [SerializeField] private int StartScores;
  39. [SerializeField] private float AimingCrossPosMultiplier = 1f;
  40. [SerializeField] private Camera Cam;
  41. public const float ArrowSpeedMultiplier = 8.0f;
  42. public const float GravityMultiplier = 2.5f;
  43. private Quaternion CurrentRotation;
  44. private int Scores;
  45. private int Combos;
  46. private int Life;
  47. private bool bGameStarted;
  48. private bool bGamePaused;
  49. // FrozenBanana related variables
  50. private bool bFreeze;
  51. private bool bFreezing;
  52. private float FreezeTimeEclipsed;
  53. // ComboPomegarante related variables
  54. private bool bEnterComboTime;
  55. private bool bComboTime;
  56. private float ComboTimeEclipsed;
  57. FruitBehavior ActiveComboPomegarante;
  58. List<Vector3> FruitVelocityCache_Linear = new List<Vector3>();
  59. List<Vector3> FruitVelocityCache_Angular = new List<Vector3>();
  60. private float MakeNewWaveCountdown;
  61. private List<GameObject> FruitsInField = new List<GameObject>();
  62. private List<FruitType> FruitsPendingSpawn = new List<FruitType>();
  63. private float TimeSinceLastFruitSpawn = 0f;
  64. public delegate void GameEndDelegate(EndGameReason reason, int scores);
  65. public event GameEndDelegate OnGameEnd;
  66. private int FruitWaves = 0;
  67. private Vector2 ScreenMidPoint;
  68. private float FireCooldownTime_Current = 0f;
  69. private const float FireCooldownTime = 0.5f;
  70. private void Awake()
  71. {
  72. audioManager = gameObject.GetComponent<AudioManager>();
  73. Debug.Assert(audioManager);
  74. bowManager = gameObject.GetComponent<SmartBowManager>();
  75. Debug.Assert(bowManager);
  76. Debug.Assert(Cannon);
  77. ScreenMidPoint = new Vector2(Screen.width / 2, Screen.height / 2);
  78. }
  79. // Start is called before the first frame update
  80. void Start()
  81. {
  82. Resume_btn.GetComponentInChildren<Button>().onClick.AddListener(ResumeGame);
  83. Pause_btn.GetComponentInChildren<Button>().onClick.AddListener(PauseGame);
  84. Exit_btn.GetComponentInChildren<Button>().onClick.AddListener(ExitGame);
  85. if (ShootCheck.ins) ShootCheck.ins.OnGameShoot += SmartBowFireArrow;
  86. AimingCross_img.gameObject.SetActive(false);
  87. Resume_btn.gameObject.SetActive(false);
  88. ShowGamingUI(false);
  89. Physics.gravity = new Vector3(0.0f, -9.8f * GravityMultiplier, 0.0f);
  90. // Audio track start point correction only
  91. BowFireAudio.timeSamples = 3000;
  92. // Play background music
  93. AudioSource backgroundMusic = gameObject.GetComponent<AudioSource>();
  94. backgroundMusic.clip = audioManager.GetBackgroundMusic();
  95. backgroundMusic.Play();
  96. UserGameAnalyse1.CreateWhenGameStart(15);
  97. }
  98. private void OnDestroy()
  99. {
  100. if (ShootCheck.ins) ShootCheck.ins.OnGameShoot -= SmartBowFireArrow;
  101. }
  102. // Update is called once per frame
  103. void Update()
  104. {
  105. if (FireCooldownTime_Current > 0f)
  106. {
  107. FireCooldownTime_Current = Mathf.Clamp(FireCooldownTime_Current - Time.deltaTime, 0f, FireCooldownTime);
  108. }
  109. if (bGameStarted && !bGamePaused)
  110. {
  111. // listen to mouse firing arrow
  112. if (Input.GetMouseButtonDown(0) && BowCamera.isTouchMode)
  113. {
  114. AimingCross_img.gameObject.GetComponent<RectTransform>().position = Input.mousePosition;
  115. MouseFireArrow(Input.mousePosition);
  116. }
  117. if (FruitsPendingSpawn.Count > 0)
  118. {
  119. if (TimeSinceLastFruitSpawn > GamingValues.SpawnFruitInterval)
  120. {
  121. if (!Cannon.IsFireAnimating())
  122. {
  123. Cannon.TriggerShot();
  124. // Skip reseting fire interval when this is the last pending fruit
  125. // since we need it to fire immediately when firing the first fruit of a new wave
  126. if (FruitsPendingSpawn.Count == 1)
  127. {
  128. TimeSinceLastFruitSpawn = 0f;
  129. }
  130. }
  131. else
  132. {
  133. // do nothing ,wait for cannon fire animation to finish
  134. }
  135. }
  136. else
  137. {
  138. TimeSinceLastFruitSpawn += Time.deltaTime;
  139. }
  140. }
  141. else // No pending spawning fruit
  142. {
  143. if (FruitsInField.Count == 0)
  144. {
  145. if (MakeNewWaveCountdown > 0)
  146. {
  147. MakeNewWaveCountdown -= Time.deltaTime;
  148. }
  149. else
  150. {
  151. MakeFruitsWave();
  152. MakeNewWaveCountdown = GamingValues.MakeNewWaveCountdown;
  153. }
  154. }
  155. }
  156. // update aiming cross position
  157. if (BowCamera.isTouchMode == false)
  158. {
  159. Vector3 raw_screen_pos = Cam.WorldToScreenPoint(Cam.transform.position + (CurrentRotation * Vector3.forward));
  160. float dist_to_mid_x = (raw_screen_pos.x - ScreenMidPoint.x) * AimingCrossPosMultiplier;
  161. float dist_to_mid_y = (raw_screen_pos.y - ScreenMidPoint.y) * AimingCrossPosMultiplier;
  162. Vector3 scrren_pos = new Vector3(Mathf.Clamp((dist_to_mid_x + ScreenMidPoint.x), 0f, 2 * ScreenMidPoint.x), Mathf.Clamp((dist_to_mid_y + ScreenMidPoint.y), 0f, 2 * ScreenMidPoint.y), 1f);
  163. AimingCross_img.gameObject.GetComponent<RectTransform>().position = scrren_pos;
  164. }
  165. // Freeze functionality
  166. if (bFreeze)
  167. {
  168. Time.timeScale = 0.5f;
  169. bFreezing = true;
  170. bFreeze = false;
  171. foreach (var fruit in FruitsInField)
  172. {
  173. fruit.GetComponent<FruitBehavior>().Frost();
  174. }
  175. }
  176. if (bFreezing)
  177. {
  178. // Freeze visual effect
  179. float freeze_alpha;
  180. if (FreezeTimeEclipsed < 0.1f)
  181. {
  182. freeze_alpha = Mathf.Lerp(0f, 0.5f, FreezeTimeEclipsed / 0.1f);
  183. }
  184. else
  185. {
  186. freeze_alpha = Mathf.Lerp(0.5f, 0f, (FreezeTimeEclipsed - 0.1f) / (GamingValues.FreezeTime - 0.1f));
  187. }
  188. //MeshRenderer renderer = IceCube.GetComponent<MeshRenderer>();
  189. //renderer.material.SetFloat("_Cutoff", freeze_alpha);
  190. Cam.GetComponent<FrostEffect>().FrostAmount = freeze_alpha;
  191. FreezeTimeEclipsed += (Time.deltaTime / Time.timeScale); // deltaTime is scaled by timeScale, divide it by timeScale will fix it
  192. if (FreezeTimeEclipsed >= GamingValues.FreezeTime)
  193. {
  194. Time.timeScale = 1.0f;
  195. FreezeTimeEclipsed = 0f;
  196. bFreezing = false;
  197. foreach (var fruit in FruitsInField)
  198. {
  199. fruit.GetComponent<FruitBehavior>().UnFrost();
  200. }
  201. }
  202. }
  203. // ComboTime functionality
  204. if (bEnterComboTime)
  205. {
  206. bComboTime = true;
  207. bEnterComboTime = false;
  208. ComboTimeEclipsed = 0f;
  209. }
  210. if (bComboTime)
  211. {
  212. ComboTimeEclipsed += Time.deltaTime;
  213. if (ComboTimeEclipsed > GamingValues.ComboHitTime)
  214. {
  215. bComboTime = false;
  216. ComboTimeEclipsed = 0f;
  217. foreach (var f in FruitsInField)
  218. {
  219. FruitBehavior fr = f.GetComponent<FruitBehavior>();
  220. fr.bMoveAlongTrajectory = true;
  221. }
  222. FruitVelocityCache_Linear.Clear();
  223. FruitVelocityCache_Angular.Clear();
  224. DestroyFruit(ActiveComboPomegarante);
  225. ActiveComboPomegarante = null;
  226. }
  227. else
  228. {
  229. foreach (var f in FruitsInField)
  230. {
  231. FruitBehavior fr = f.GetComponent<FruitBehavior>();
  232. fr.bMoveAlongTrajectory = false;
  233. }
  234. }
  235. }
  236. }
  237. }
  238. public void StartGame()
  239. {
  240. Debug.Assert(FruitsInField.Count == 0);
  241. CameraToLook.ins.onParseRotation += UpdateRotation;
  242. Scores = StartScores;
  243. ScoreText.text = "Scores : " + Scores;
  244. Combos = 0;
  245. MakeNewWaveCountdown = GamingValues.MakeNewWaveCountdown;
  246. Life = GamingValues.LifeCount;
  247. UpdateLife(false);
  248. bGameStarted = true;
  249. if (true/*bowManager.IsSmartBowConnected()*/)
  250. {
  251. AimingCross_img.gameObject.SetActive(true);
  252. }
  253. ShowGamingUI(true);
  254. GamingValues.ResetPomegranateSpawnedCount();
  255. Cannon.OnCannonFire += SpawnAFruit;
  256. FruitWaves = 0;
  257. }
  258. public void PauseGame()
  259. {
  260. bGamePaused = true;
  261. AimingCross_img.gameObject.SetActive(false);
  262. Pause_btn.gameObject.SetActive(false);
  263. Resume_btn.gameObject.SetActive(true);
  264. Time.timeScale = 0f;
  265. }
  266. public void ResumeGame()
  267. {
  268. bGamePaused = false;
  269. AimingCross_img.gameObject.SetActive(true);
  270. Pause_btn.gameObject.SetActive(true);
  271. Resume_btn.gameObject.SetActive(false);
  272. Time.timeScale = 1f;
  273. }
  274. public bool IsGameStarted()
  275. {
  276. return bGameStarted;
  277. }
  278. public bool IsGamePaused()
  279. {
  280. return bGamePaused;
  281. }
  282. private void MakeFruitsWave()
  283. {
  284. // Reset Normal fruit unique funcionality
  285. FruitTypeList fruit_list = GetComponent<FruitTypeList>();
  286. Debug.Assert(fruit_list != null);
  287. fruit_list.ResetUniqueNormalIndex();
  288. Debug.Assert(FruitsPendingSpawn.Count == 0);
  289. if (FruitWaves < 5)
  290. {
  291. switch (FruitWaves)
  292. {
  293. case 0:
  294. FruitsPendingSpawn.Add(FruitType.Normal);
  295. break;
  296. case 1:
  297. FruitsPendingSpawn.Add(FruitType.Normal);
  298. FruitsPendingSpawn.Add(FruitType.Bomb);
  299. break;
  300. case 2:
  301. FruitsPendingSpawn.Add(FruitType.Normal);
  302. FruitsPendingSpawn.Add(FruitType.Normal);
  303. break;
  304. case 3:
  305. FruitsPendingSpawn.Add(FruitType.Normal);
  306. FruitsPendingSpawn.Add(FruitType.Normal);
  307. FruitsPendingSpawn.Add(FruitType.Normal);
  308. FruitsPendingSpawn.Add(FruitType.FrozenBanana);
  309. break;
  310. case 4:
  311. FruitsPendingSpawn.Add(FruitType.Normal);
  312. FruitsPendingSpawn.Add(FruitType.Normal);
  313. FruitsPendingSpawn.Add(FruitType.Normal);
  314. FruitsPendingSpawn.Add(FruitType.Normal);
  315. FruitsPendingSpawn.Add(FruitType.RainbowApple);
  316. break;
  317. default:
  318. break;
  319. }
  320. }
  321. else
  322. {
  323. int normal_fruit_count = GamingValues.GetSpawnNum(Scores);
  324. bool bContain_bomb = GamingValues.DoesSpawnBomb(Scores);
  325. bool doesSpawnPome = GamingValues.DoesSpawnComboPomegranate(Scores);
  326. // if spawn FrozenBanana or RainbowApple, 0 - no spawn, 1 - frozen banana, 2 - rainbow apple
  327. int doesSpawnSpecial = 0;
  328. int specialAddition = 0;
  329. if (normal_fruit_count == 3)
  330. {
  331. specialAddition++;
  332. doesSpawnSpecial = 1;
  333. }
  334. else if (normal_fruit_count >= 4)
  335. {
  336. specialAddition++;
  337. doesSpawnSpecial = 2;
  338. }
  339. else
  340. {
  341. doesSpawnSpecial = 0;
  342. }
  343. if (doesSpawnPome)
  344. {
  345. specialAddition++;
  346. }
  347. for (int i = 0; i < normal_fruit_count + specialAddition; i++)
  348. {
  349. if (i == 0) // for the first fruit, spawn bomb if a bomb is included
  350. {
  351. FruitsPendingSpawn.Add(bContain_bomb ? FruitType.Bomb : FruitType.Normal);
  352. }
  353. else if (i == 1 && doesSpawnPome) // for the second, we see if a combo pomegranate is included, since if there is one, the minimum count of fruits is 2
  354. {
  355. FruitsPendingSpawn.Add(FruitType.ComboPomegranate);
  356. }
  357. else if (i == 2 && doesSpawnSpecial > 0) // the third one is last chance for special fruits, check for frozen banana and rainbow apple, they do not appear at the same time, and if there is one, the minimum count must be above 3
  358. {
  359. FruitsPendingSpawn.Add(doesSpawnSpecial == 1 ? FruitType.FrozenBanana : FruitType.RainbowApple);
  360. }
  361. else // all special fruits spawning posibility is checked when spawning the first 3 fruits, the rest must be normal
  362. {
  363. FruitsPendingSpawn.Add(FruitType.Normal);
  364. }
  365. }
  366. }
  367. FruitWaves++;
  368. }
  369. private void SpawnAFruit()
  370. {
  371. float speed_x = Random.Range(-100f, 100f);
  372. float speed_y = Random.Range(350f, 450f);
  373. Debug.Assert(FruitsPendingSpawn.Count > 0);
  374. GameObject newFruit = Instantiate(FruitPrefab, Cannon.GetFiringPoint(), Quaternion.identity);
  375. FruitBehavior fb = newFruit.GetComponent<FruitBehavior>();
  376. // Get a random target position with a small random range along x axis and lower half y axis
  377. Vector3 target = Cam.transform.position + new Vector3(Random.Range(-7f, 7f), Random.Range(-10f, -2f), 0f);
  378. float fruit_lifetime = GamingValues.GetFruitLifetime(FruitWaves, Scores);
  379. fb.Init(FruitsPendingSpawn[0], this, Cannon.GetFiringPoint(), target, fruit_lifetime);
  380. FruitsPendingSpawn.RemoveAt(0);
  381. fb.OnArrive += AFruitArrived;
  382. fb.OnHitEvent += OnFruitHit;
  383. FruitsInField.Add(newFruit);
  384. }
  385. private void AFruitArrived(FruitBehavior fb)
  386. {
  387. if (fb.GetFruitType() != FruitType.Bomb)
  388. {
  389. if (UpdateLife(true))
  390. {
  391. DelayEndGame(EndGameReason.GameOver, 2f);
  392. }
  393. FruitsInField.Remove(fb.gameObject);
  394. DestroyFruit(fb);
  395. SpawnStain(fb.JuiceStain, fb.gameObject.transform.position);
  396. }
  397. else
  398. {
  399. fb.gameObject.transform.position = new Vector3(0f, 0f, 10f);
  400. DestroyFruit(fb);
  401. DelayEndGame(EndGameReason.GameOver, 2f);
  402. }
  403. }
  404. private void EndGame(EndGameReason reason)
  405. {
  406. Cannon.ResetPos();
  407. Cannon.OnCannonFire -= SpawnAFruit;
  408. CameraToLook.ins.onParseRotation -= UpdateRotation;
  409. // clear all fruits in the field
  410. foreach (var fruit in FruitsInField)
  411. {
  412. Destroy(fruit);
  413. }
  414. FruitsInField.Clear();
  415. if (IsGamePaused())
  416. {
  417. ResumeGame();
  418. }
  419. bGameStarted = false;
  420. ShowGamingUI(false);
  421. OnGameEnd.Invoke(reason, Scores);
  422. }
  423. private void DelayEndGame(EndGameReason reason, float wait_secs)
  424. {
  425. StartCoroutine(DelayEndGame_CR(reason, wait_secs));
  426. }
  427. private IEnumerator DelayEndGame_CR(EndGameReason reason, float wait_secs)
  428. {
  429. yield return new WaitForSeconds(wait_secs);
  430. EndGame(reason);
  431. }
  432. public static void DelayDestroy(MonoBehaviour excutor, GameObject to_destroy, float wait_secs)
  433. {
  434. excutor.StartCoroutine(DelayDestroy_CR(to_destroy, wait_secs));
  435. }
  436. private static IEnumerator DelayDestroy_CR(GameObject to_destroy, float wait_secs)
  437. {
  438. yield return new WaitForSeconds(wait_secs);
  439. if (to_destroy)
  440. Destroy(to_destroy);
  441. }
  442. public void ExitGame()
  443. {
  444. EndGame(EndGameReason.UserExit);
  445. }
  446. private void FireArrow(float speed, Vector3 dir)
  447. {
  448. if (bGameStarted && !bGamePaused && FireCooldownTime_Current == 0f)
  449. {
  450. FireCooldownTime_Current = FireCooldownTime;
  451. Vector3 spawnPos = Cam.transform.position + dir;
  452. GameObject arrow = Instantiate(ArrowPrefab, spawnPos, Quaternion.FromToRotation(-Vector3.right, dir));
  453. arrow.GetComponent<ArrowBehavior>().Init(speed, dir, true);
  454. arrow.GetComponent<ArrowBehavior>().OnMissShoot += MissShoot;
  455. BowFireAudio.clip = audioManager.GetRandomSound(SoundCategory.ArrowFire);
  456. BowFireAudio.Play();
  457. }
  458. }
  459. private void SmartBowFireArrow(float speed)
  460. {
  461. if (Time.time == 0) return;
  462. if (IsGameStarted())
  463. {
  464. if (IsGamePaused()) // if game is paused, fire the smart bow will resume game
  465. {
  466. ResumeGame();
  467. }
  468. else // If game is started and not paused, shoot a arrow
  469. {
  470. Vector3 direction = Cam.ScreenToWorldPoint(AimingCross_img.GetComponent<RectTransform>().position);
  471. FireArrow(speed * ArrowSpeedMultiplier, direction);
  472. }
  473. }
  474. else
  475. {
  476. gameObject.GetComponent<OverallLogics>().StartGame();
  477. }
  478. }
  479. private void UpdateRotation(Quaternion q)
  480. {
  481. CurrentRotation = q;
  482. }
  483. private void MouseFireArrow(Vector3 screenPos)
  484. {
  485. Vector3 worldPos = Cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, 10.0f));
  486. FireArrow(20.0f, worldPos - Cam.transform.position);
  487. }
  488. private void OnFruitHit(FruitBehavior fb, Vector3 hitPoint, bool bCritical)
  489. {
  490. Combos++;
  491. // if this is a ComboPomegranate, ignore critical and combo hit, only gain 1 score per hit
  492. if (fb.GetFruitType() == FruitType.ComboPomegranate)
  493. {
  494. if (GainScore(1))
  495. {
  496. UpdateLife(false);
  497. }
  498. }
  499. else if (bCritical)
  500. {
  501. if (GainScore(10))
  502. {
  503. UpdateLife(false);
  504. }
  505. // display a critical text
  506. GameObject critical = Instantiate(CriticalTextPrefab, GamingUIContainer.transform);
  507. critical.transform.position = Cam.WorldToScreenPoint(hitPoint);
  508. }
  509. else if (Combos > 1) // This combo section handles combo scores addition, text displaying is handled after
  510. {
  511. if (GainScore(5))
  512. {
  513. UpdateLife(false);
  514. }
  515. }
  516. else // A very bland hit, no critical, no combo
  517. {
  518. if (GainScore(5))
  519. {
  520. UpdateLife(false);
  521. }
  522. }
  523. if (Combos > 1) // This combo section handles combo text display
  524. {
  525. // display a combo text
  526. GameObject combo = Instantiate(ComboTextPrefab, GamingUIContainer.transform);
  527. combo.GetComponent<ComboTextBehavior>().SetComboNumber(Combos);
  528. }
  529. if (fb.GetFruitType() == FruitType.FrozenBanana)
  530. {
  531. // enter freeze state
  532. bFreeze = true;
  533. FreezeTimeEclipsed = 0.0f;
  534. DestroyFruit(fb);
  535. }
  536. else if (fb.GetFruitType() == FruitType.ComboPomegranate)
  537. {
  538. ComboPomegaranteBehavior cpb = fb.gameObject.GetComponent<ComboPomegaranteBehavior>();
  539. if (cpb.GetHitCount() == 0)
  540. {
  541. bEnterComboTime = true;
  542. ActiveComboPomegarante = fb;
  543. foreach (var f in FruitsInField)
  544. {
  545. FruitVelocityCache_Linear.Add(f.gameObject.GetComponent<Rigidbody>().velocity);
  546. FruitVelocityCache_Angular.Add(f.gameObject.GetComponent<Rigidbody>().angularVelocity);
  547. }
  548. }
  549. cpb.Hit();
  550. if (cpb.GetHitCount() >= GamingValues.ComboHitLimit)
  551. {
  552. bComboTime = false;
  553. DestroyFruit(fb);
  554. ActiveComboPomegarante = null;
  555. ComboTimeEclipsed = 0f;
  556. foreach (var f in FruitsInField)
  557. {
  558. FruitBehavior fr = f.GetComponent<FruitBehavior>();
  559. fr.bMoveAlongTrajectory = true;
  560. }
  561. }
  562. else
  563. {
  564. // do nothing
  565. }
  566. }
  567. else if (fb.GetFruitType() == FruitType.RainbowApple)
  568. {
  569. Instantiate(FullscreenClearBubble, fb.gameObject.transform.position, Quaternion.identity);
  570. DestroyFruit(fb);
  571. }
  572. else
  573. {
  574. DestroyFruit(fb);
  575. }
  576. }
  577. /// <summary>
  578. /// Gain some scores
  579. /// </summary>
  580. /// <returns> If exceeded another 100 scores, return true, for life recovering test </returns>
  581. private bool GainScore(int score)
  582. {
  583. int last_scores = Scores;
  584. Scores += score;
  585. ScoreText.text = "Scores : " + Scores; // update socres display
  586. return ((Scores / 100) == (last_scores / 100) + 1);
  587. }
  588. /// <summary>
  589. /// Add or substract life point by 1
  590. /// </summary>
  591. /// <param name="bDecrease">true for decrease, false for add</param>
  592. /// <returns>return true if life reaches 0</returns>
  593. private bool UpdateLife(bool bDecrease)
  594. {
  595. if (bDecrease)
  596. {
  597. Life--;
  598. if (Life < 0)
  599. {
  600. Life = 0;
  601. }
  602. LooseLifeAudio.Play();
  603. }
  604. else
  605. {
  606. if (Life < 3)
  607. {
  608. Life++;
  609. GameObject addLife = Instantiate(AddLifeTextPrefab, GamingUIContainer.transform);
  610. }
  611. }
  612. switch (Life)
  613. {
  614. case 0:
  615. Life_0_img.gameObject.SetActive(true);
  616. Life_1_img.gameObject.SetActive(true);
  617. Life_2_img.gameObject.SetActive(true);
  618. return true;
  619. case 1:
  620. Life_0_img.gameObject.SetActive(true);
  621. Life_1_img.gameObject.SetActive(true);
  622. Life_2_img.gameObject.SetActive(false);
  623. return false;
  624. case 2:
  625. Life_0_img.gameObject.SetActive(true);
  626. Life_1_img.gameObject.SetActive(false);
  627. Life_2_img.gameObject.SetActive(false);
  628. return false;
  629. case 3:
  630. Life_0_img.gameObject.SetActive(false);
  631. Life_1_img.gameObject.SetActive(false);
  632. Life_2_img.gameObject.SetActive(false);
  633. return false;
  634. default:
  635. return false;
  636. }
  637. }
  638. private void MissShoot()
  639. {
  640. Combos = 0;
  641. }
  642. private void ShowGamingUI(bool b)
  643. {
  644. GamingUIContainer.SetActive(b);
  645. }
  646. public void DestroyFruit(FruitBehavior fb)
  647. {
  648. FruitsInField.Remove(fb.gameObject);
  649. fb.SmashEffect();
  650. }
  651. public AudioManager GetAudioManager()
  652. {
  653. return audioManager;
  654. }
  655. public void SpawnStain(Sprite stain, Vector3 position3d_ws)
  656. {
  657. GameObject stain_go = Instantiate(new GameObject(), GamingUIContainer.transform);
  658. stain_go.AddComponent<Image>().sprite = stain;
  659. stain_go.transform.position = Cam.WorldToScreenPoint(position3d_ws);
  660. stain_go.transform.localScale = Vector3.one * 8f;
  661. stain_go.AddComponent<StainBehaviour>();
  662. }
  663. }