GamingManager.cs 26 KB

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