GameManager.cs 16 KB

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