GameManager.cs 19 KB

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