GameManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace DuckHunter
  6. {
  7. public class GameManager : MonoBehaviour
  8. {
  9. public static GameManager Instance;
  10. [SerializeField] GameObject dogObject;
  11. [SerializeField] GameObject dogSideObject;
  12. [SerializeField] GameObject duckPrefab;
  13. [SerializeField] RectTransform duckBox;
  14. [System.NonSerialized] public bool isGameStarted;
  15. [System.NonSerialized] public bool isGameOver;
  16. public bool isGamePlaying { get => isGameStarted && !isGameOver; }
  17. public System.Action onGameStart;
  18. void Awake()
  19. {
  20. Instance = this;
  21. dogObject.SetActive(true);
  22. if (!AutoNextLevel)
  23. {
  24. DefaultLevel = 1;
  25. _CumulativeScore = 0;
  26. }
  27. level = DefaultLevel;
  28. }
  29. void OnDestroy()
  30. {
  31. if (Instance == this) Instance = null;
  32. }
  33. void Start()
  34. {
  35. TextGameScreenCenter.Instance.ShowText(TextGameScreenCenter.TextName.GAME_START);
  36. if (AutoNextLevel)
  37. {
  38. AutoNextLevel = false;
  39. StartGame(true);
  40. }
  41. }
  42. void onDeviceShoot(float speed) {
  43. OnModuleShooting((int)speed);
  44. }
  45. void Update()
  46. {
  47. UpdateCheckMouseHit();
  48. UpdateForAutoCreateDucks();
  49. SettleGame();
  50. #if UNITY_EDITOR
  51. if (Input.GetKeyDown(KeyCode.A))
  52. {
  53. Time.timeScale = 10;
  54. }
  55. if (Input.GetKeyUp(KeyCode.A))
  56. {
  57. Time.timeScale = 1;
  58. }
  59. #endif
  60. }
  61. void UpdateCheckMouseHit()
  62. {
  63. if (Input.GetMouseButtonDown(0) && BowCamera.isTouchMode)
  64. {
  65. if (EventSystem.current.currentSelectedGameObject == null)
  66. {
  67. CrossHair.Instance.transform.position = Input.mousePosition;
  68. OnModuleShooting(10);
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// 基础初速度
  74. /// </summary>
  75. public static float BaseFlySpeed = 50;
  76. /// <summary>
  77. /// 下次从多少分开始
  78. /// </summary>
  79. public static int DefaultLevel = 1;
  80. private static bool AutoNextLevel = false;
  81. [System.NonSerialized] public int hitScore; //得分
  82. [System.NonSerialized] public int level = 1;
  83. /// <summary>
  84. /// 通关需要击落的鸭子数量
  85. /// </summary>
  86. int passNeedHitCount;
  87. int needCreateDuckCount;
  88. bool canCreateDuck;
  89. [System.NonSerialized] public int theDuckCountWaitingForDogHandle;
  90. float _createDuckInterval;
  91. void UpdateForAutoCreateDucks()
  92. {
  93. if (isGameOver) return;
  94. if (!canCreateDuck) return;
  95. _createDuckInterval = 0;
  96. if (level <= 60)
  97. {
  98. if (theDuckCountWaitingForDogHandle > 0) return;
  99. }
  100. else if (level <= 80)
  101. {
  102. if (theDuckCountWaitingForDogHandle > 0) _createDuckInterval = 5;
  103. if (theDuckCountWaitingForDogHandle > 1) return;
  104. }
  105. else //<=100
  106. {
  107. if (theDuckCountWaitingForDogHandle > 0) _createDuckInterval = 5;
  108. if (theDuckCountWaitingForDogHandle > 2) return;
  109. }
  110. if (!Dog.Instance || Dog.Instance.isShowing) return;
  111. if (Time.time - _lastCreateDuckTime < _createDuckInterval) return;
  112. CreateDuck();
  113. }
  114. List<int> _ductTypeList = null;
  115. void InitDuckTypeList(int greenCount, int blueCount, int redCount)
  116. {
  117. if (_ductTypeList != null) return;
  118. _ductTypeList = new List<int>();
  119. for (int i = 0; i < greenCount; i++) _ductTypeList.Add(1);
  120. for (int i = 0; i < blueCount; i++) _ductTypeList.Add(2);
  121. for (int i = 0; i < redCount; i++) _ductTypeList.Add(3);
  122. _ductTypeList.Sort((a, b) => Random.value < 0.5 ? -1 : 1);
  123. needCreateDuckCount = _ductTypeList.Count;
  124. }
  125. static float[] _FlyAngles1 = { 75, 180 - 75 };
  126. static float[] _FlyAngles2 = { 75, 60, 180 - 75, 180 - 60 };
  127. static float[] _FlyAngles3 = { 45, 60, 180 - 45, 180 - 60 };
  128. static float[] _FlyAngles4 = { 30, 45, 60, 180 - 30, 180 - 45, 180 - 60 };
  129. static float RangeFlyAngle(float[] angles)
  130. {
  131. return angles[Random.Range(0, angles.Length)];
  132. }
  133. DuckConfig GetDuckConfig()
  134. {
  135. DuckConfig duckConfig = new DuckConfig();
  136. if (level <= 20) duckConfig.positionX = Random.Range(duckBox.rect.width / 2 - 200, duckBox.rect.width / 2 + 50);
  137. else duckConfig.positionX = Random.Range(250, duckBox.rect.width - 350);
  138. if (level <= 10)
  139. {
  140. InitDuckTypeList(10, 0, 0);
  141. passNeedHitCount = 6;
  142. duckConfig.touchBoundCount = 4;
  143. duckConfig.flyAngle = RangeFlyAngle(_FlyAngles1);
  144. }
  145. else if (level <= 15)
  146. {
  147. InitDuckTypeList(5, 5, 0);
  148. passNeedHitCount = 7;
  149. duckConfig.touchBoundCount = 4;
  150. duckConfig.flyAngle = RangeFlyAngle(_FlyAngles1);
  151. }
  152. else if (level <= 18)
  153. {
  154. InitDuckTypeList(5, 3, 2);
  155. passNeedHitCount = 8;
  156. duckConfig.touchBoundCount = 4;
  157. duckConfig.flyAngle = RangeFlyAngle(_FlyAngles1);
  158. }
  159. else if (level <= 20)
  160. {
  161. InitDuckTypeList(4, 4, 2);
  162. passNeedHitCount = 9;
  163. duckConfig.touchBoundCount = 4;
  164. duckConfig.flyAngle = RangeFlyAngle(_FlyAngles1);
  165. }
  166. else if (level <= 40)
  167. {
  168. InitDuckTypeList(3, 4, 3);
  169. passNeedHitCount = 10;
  170. duckConfig.touchBoundCount = 5;
  171. duckConfig.flyAngle = RangeFlyAngle(_FlyAngles2);
  172. }
  173. else if (level <= 60)
  174. {
  175. InitDuckTypeList(3, 3, 4);
  176. passNeedHitCount = 10;
  177. duckConfig.touchBoundCount = 6;
  178. duckConfig.flyAngle = RangeFlyAngle(_FlyAngles3);
  179. }
  180. else if (level <= 80)
  181. {
  182. InitDuckTypeList(2, 4, 4);
  183. passNeedHitCount = 10;
  184. duckConfig.touchBoundCount = 7;
  185. duckConfig.flyAngle = RangeFlyAngle(_FlyAngles4);
  186. }
  187. else if (level <= 100)
  188. {
  189. InitDuckTypeList(2, 3, 5);
  190. passNeedHitCount = 10;
  191. duckConfig.touchBoundCount = 8;
  192. duckConfig.flyAngle = RangeFlyAngle(_FlyAngles4);
  193. }
  194. duckConfig.type = _ductTypeList[0]; _ductTypeList.RemoveAt(0);
  195. //duckConfig.flySpeed = BaseFlySpeed * (1 + (level - 1) * 0.05f);
  196. //2023-7-25修改:速度还是以80关的做为初始速度,后面的关卡再按原来的公式增加
  197. duckConfig.flySpeed = BaseFlySpeed * (1 + (level + 79 - 1) * 0.05f);
  198. if (duckConfig.type == 1) duckConfig.flySpeed *= 1f;
  199. else if (duckConfig.type == 2) duckConfig.flySpeed *= 1.2f;
  200. else if (duckConfig.type == 3) duckConfig.flySpeed *= 1.5f;
  201. return duckConfig;
  202. }
  203. float _lastCreateDuckTime = -1000;
  204. void CreateDuck()
  205. {
  206. if (_ductTypeList != null && _ductTypeList.Count == 0) return;
  207. theDuckCountWaitingForDogHandle++;
  208. _lastCreateDuckTime = Time.time;
  209. DuckConfig duckConfig = GetDuckConfig();
  210. Duck duck = Instantiate(duckPrefab, duckBox).GetComponent<Duck>();
  211. duck.config = duckConfig;
  212. duck.onExit += HandleOnDuckExit;
  213. duck.onFallDonwEnd += HandleOnDuckFallDown;
  214. duck.onHitDead += HandleOnHitDead;
  215. duck.onStartFlyAway += () => TextGameScreenCenter.Instance.ShowText(TextGameScreenCenter.TextName.FLY_AWAY);
  216. //恢复箭
  217. ResumeArrows(duck);
  218. }
  219. int exitDuckCount = 0;
  220. void HandleOnDuckExit(Duck duck)
  221. {
  222. if (duck.dead) Dog.Instance?.OnEvent(Dog.SensitiveEventType.DuckHit, duck);
  223. else
  224. {
  225. RemoveArrows(duck);
  226. GameUI.Instance.RenderHitDuckCount(0);
  227. Dog.Instance?.OnEvent(Dog.SensitiveEventType.DuckGetAway);
  228. }
  229. exitDuckCount++;
  230. }
  231. float _lastFallDownTime = 0;
  232. void HandleOnDuckFallDown(Duck duck)
  233. {
  234. _lastFallDownTime = Time.time;
  235. }
  236. [System.NonSerialized] public int hitCount = 0;
  237. void HandleOnHitDead(Duck duck)
  238. {
  239. hitCount++;
  240. int scoreToPlus = duck.config.type * 500;
  241. hitScore += scoreToPlus;
  242. GameUI.Instance.RenderHitDuckCount(duck.config.type);
  243. // GameUI.Instance.RenderHitScore(hitScore, GetBestScore());
  244. _CumulativeScore += scoreToPlus;
  245. GameUI.Instance.RenderHitScore(_CumulativeScore, GetBestScore());
  246. GameUI.Instance.ShowTextHitScore(scoreToPlus, duck.transform.position);
  247. RemoveArrows(duck);
  248. }
  249. public void OnModuleRotationUpdate(Quaternion rotation)
  250. {
  251. CrossHair.Instance?.UpdatePositionByModuleRotation(rotation);
  252. }
  253. public void OnModuleShooting(float speed, bool bAddCount = false)
  254. {
  255. if (CrossHair.Instance)
  256. {
  257. if (isGamePlaying)
  258. {
  259. if (UseOneArrow(bAddCount))
  260. {
  261. CrossHair.Instance.Shoot();
  262. CheckShootPointHitDuck();
  263. CheckNotifyFlyAway();
  264. }
  265. }
  266. else if (!isGameStarted)
  267. {
  268. //CrossHair.Instance.Shoot();
  269. if (GameUI.Instance.CheckHitDuckForGameStart(CrossHair.Instance.transform.position))
  270. {
  271. OnClick_GameStart();
  272. }
  273. }
  274. }
  275. }
  276. void CheckShootPointHitDuck()
  277. {
  278. bool hitDuck = false;
  279. Vector2 aimPos = CrossHair.Instance.transform.position;
  280. for (int i = Duck.DuckList.Count - 1; i >= 0; i--)
  281. {
  282. Duck duck = Duck.DuckList[i];
  283. RectTransform duckRTF = duck.transform as RectTransform;
  284. bool intersect = RectTransformUtility.RectangleContainsScreenPoint(duckRTF, aimPos);
  285. bool hitFrontBG = false;
  286. Collider2D[] hitColliders = Physics2D.OverlapPointAll(aimPos);
  287. foreach (var item in hitColliders)
  288. {
  289. if (item.name.StartsWith("BGFront"))
  290. {
  291. hitFrontBG = true;
  292. break;
  293. }
  294. }
  295. if (intersect && !hitFrontBG && duck.Hit())
  296. {
  297. hitDuck = true;
  298. break;
  299. }
  300. }
  301. if (!hitDuck)
  302. {
  303. GameUI.Instance.AddArrowOnScreen(aimPos);
  304. }
  305. }
  306. void OnClick_GameStart()
  307. {
  308. AudioManager.Instance.PlayGameStart();
  309. StartGame(false);
  310. }
  311. void StartGame(bool startImmediate)
  312. {
  313. if (isGameStarted) return;
  314. isGameStarted = true;
  315. onGameStart?.Invoke();
  316. GameUI.Instance.HandleGameStart(() =>
  317. {
  318. dogSideObject.SetActive(true);
  319. dogSideObject.GetComponent<DogSide>().onExit = () => canCreateDuck = true;
  320. TextGameScreenCenter.Instance.ShowText(TextGameScreenCenter.TextName.ROUND_X, new object[] { level });
  321. }, startImmediate);
  322. NoArrows();
  323. GameUI.Instance.RenderLevel(level);
  324. // GameUI.Instance.RenderHitScore(hitScore, GetBestScore());
  325. GameUI.Instance.RenderHitScore(_CumulativeScore, GetBestScore());
  326. }
  327. //重新加载游戏
  328. public void onReLoadGame()
  329. {
  330. string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
  331. UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
  332. }
  333. public void onBackHome()
  334. {
  335. UnityEngine.SceneManagement.SceneManager.LoadScene("EnterTheInfraredScene",UnityEngine.SceneManagement.LoadSceneMode.Single);
  336. }
  337. void SettleGame()
  338. {
  339. if (isGameOver) return;
  340. if (_ductTypeList == null) return;
  341. if (Dog.Instance != null && Dog.Instance.isShowing) return;
  342. if (needCreateDuckCount != exitDuckCount) return;
  343. isGameOver = true;
  344. Debug.Log("Game Over");
  345. GameUI.Instance.RenderHitDuckCount(-1);
  346. if (hitCount < passNeedHitCount)
  347. {
  348. //通关失败
  349. TextGameScreenCenter.Instance.ShowText(TextGameScreenCenter.TextName.GAME_OVER, null, () =>
  350. {
  351. string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
  352. UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
  353. //UnityEngine.SceneManagement.SceneManager.LoadScene("DuckHunter");
  354. });
  355. AudioManager.Instance.PlayGameOver();
  356. Debug.Log("通关失败");
  357. }
  358. else
  359. {
  360. //完美通关
  361. if (needCreateDuckCount == hitCount)
  362. {
  363. //奖励额外积分
  364. int plusScore = 10000;
  365. hitScore += plusScore;
  366. // GameUI.Instance.RenderHitScore(hitScore, GetBestScore());
  367. _CumulativeScore += plusScore;
  368. GameUI.Instance.RenderHitScore(_CumulativeScore, GetBestScore());
  369. TextGameScreenCenter.Instance.ShowText(TextGameScreenCenter.TextName.SUPER_ARCHER, new object[] { plusScore }, ShowGamePass);
  370. AudioManager.Instance.PlayFullScore();
  371. }
  372. else
  373. {
  374. ShowGamePass();
  375. }
  376. }
  377. SaveBestScore();
  378. GameOverInterface.OnGameOver(GameMgr.gameType);
  379. }
  380. void ShowGamePass()
  381. {
  382. //通关成功
  383. TextGameScreenCenter.Instance.ShowText(TextGameScreenCenter.TextName.GAME_COMPLETED, null, () =>
  384. {
  385. //自动进入下一关
  386. if (level < 100)
  387. {
  388. DefaultLevel = level + 1;
  389. AutoNextLevel = true;
  390. }
  391. string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
  392. UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
  393. //UnityEngine.SceneManagement.SceneManager.LoadScene("DuckHunter");
  394. });
  395. AudioManager.Instance.PlayGamePass();
  396. Debug.Log("通关成功");
  397. }
  398. int _arrowCount;
  399. int arrowCount
  400. {
  401. get
  402. {
  403. // int count = 0;
  404. // _duckCanShootCountList.ForEach(e => count += e.shootCount);
  405. // return count;
  406. return _arrowCount;
  407. }
  408. }
  409. List<DuckCanShootCount> _duckCanShootCountList = new List<DuckCanShootCount>();
  410. class DuckCanShootCount
  411. {
  412. public Duck duck;
  413. public int shootCount;
  414. public DuckCanShootCount(Duck duck, int shootCount)
  415. {
  416. this.duck = duck;
  417. this.shootCount = shootCount;
  418. }
  419. }
  420. void ResumeArrows(Duck duck)
  421. {
  422. // _duckCanShootCountList.Add(new DuckCanShootCount(duck, 3));
  423. _arrowCount = 3;
  424. GameUI.Instance.RenderArrowCount(arrowCount);
  425. }
  426. void RemoveArrows(Duck duck)
  427. {
  428. // _duckCanShootCountList.RemoveAll(e => e.duck == duck);
  429. // GameUI.Instance.RenderArrowCount(arrowCount);
  430. }
  431. bool UseOneArrow(bool bAddCount = false)
  432. {
  433. if (arrowCount > 0)
  434. {
  435. // foreach (var e in _duckCanShootCountList)
  436. // {
  437. // if (e.shootCount > 0)
  438. // {
  439. // e.shootCount--;
  440. // break;
  441. // }
  442. // }
  443. _arrowCount--;
  444. GameUI.Instance.RenderArrowCount(arrowCount);
  445. return true;
  446. }
  447. return false;
  448. }
  449. void NoArrows()
  450. {
  451. // _duckCanShootCountList = new List<DuckCanShootCount>();
  452. _arrowCount = 0;
  453. GameUI.Instance.RenderArrowCount(arrowCount);
  454. }
  455. void CheckNotifyFlyAway()
  456. {
  457. // foreach (var e in _duckCanShootCountList)
  458. // if (e.shootCount == 0)
  459. // e.duck.NotifyFlyAway();
  460. if (arrowCount <= 0)
  461. foreach (var duck in Duck.DuckList)
  462. duck.NotifyFlyAway();
  463. }
  464. //累计得分
  465. private static int _CumulativeScore = 0;
  466. private static int _BestScoreVersion = 1;
  467. string GetBestScoreKey()
  468. {
  469. // return "BestScore_Level" + level + "_V" + _BestScoreVersion;
  470. return "DuckHunter_" + LoginMgr.myUserInfo.id + "_" + _BestScoreVersion;
  471. }
  472. void SaveBestScore()
  473. {
  474. string k = GetBestScoreKey();
  475. int s = PlayerPrefs.GetInt(k, 0);
  476. // if (hitScore > s) PlayerPrefs.SetInt(k, hitScore);
  477. if (_CumulativeScore > s) PlayerPrefs.SetInt(k, _CumulativeScore);
  478. }
  479. int GetBestScore()
  480. {
  481. string k = GetBestScoreKey();
  482. int s = PlayerPrefs.GetInt(k, 0);
  483. return s;
  484. }
  485. }
  486. }