GamingManager.cs 27 KB

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