GamingManager.cs 25 KB

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