GameManager.cs 16 KB

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