GameMgr.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. /* 游戏管理者(游戏模式:单人模式、PK模式) */
  8. public class GameMgr : MonoBehaviour
  9. {
  10. public static bool debugInEditor = false;
  11. public static int gameType = 0;
  12. public GameMode gameMode;
  13. [System.NonSerialized] public bool gameOver = false;
  14. public static GameMgr ins;
  15. void Awake()
  16. {
  17. ins = this;
  18. if (Application.platform == RuntimePlatform.WindowsEditor)
  19. {
  20. debugInEditor = true;
  21. }
  22. AudioMgr.Init();
  23. this.InitGameMode();
  24. if (debugInEditor) {
  25. guideFinish = true;
  26. gameMode.Start();
  27. } else {
  28. if (!BluetoothStatus.IsAllConnected()) {
  29. GameObject view = DeviceReconnectView.Show();
  30. if (view) {
  31. view.GetComponent<DeviceReconnectView>().onComplete = CheckGuide;
  32. }
  33. } else {
  34. this.CheckGuide();
  35. }
  36. }
  37. // GameSceneLoadBtns.Create();
  38. }
  39. void Start()
  40. {
  41. if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
  42. }
  43. void OnDestroy() {
  44. AimHandler.ins?.BanControlObjRotate(false);
  45. }
  46. void FixedUpdate()
  47. {
  48. gameMode.Update();
  49. }
  50. void InitGameMode() {
  51. if (gameType == 0) gameMode = new GameModeTest(this);
  52. if (gameType == 1) gameMode = new TimeLimitGameMode(this);
  53. if (gameType == 2) gameMode = new PKGameMode(this);
  54. if (gameType == 3) gameMode = new RabbitHuntGameMode(this);
  55. }
  56. public void StopGame() {
  57. if (BowCamera.ins) BowCamera.ins.enabled = false;
  58. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  59. foreach(var arrow in arrows)
  60. {
  61. Destroy(arrow);
  62. }
  63. }
  64. bool guideFinish = false;
  65. public void CheckGuide() {
  66. if (gameType > 0) {
  67. if (!LoginMgr.myUserInfo.deviceCalibrateGuideFinish) {
  68. DeviceCalibrateView.Create();
  69. return;
  70. }
  71. bool gameRuleGuideFinish = (bool)LoginMgr.myUserInfo.GetType().GetField($"gameRule{GameMgr.gameType}GuideFinish").GetValue(LoginMgr.myUserInfo);
  72. if (!gameRuleGuideFinish) {
  73. GameRuleView.Create();
  74. return;
  75. }
  76. }
  77. guideFinish = true;
  78. gameMode.Start();
  79. }
  80. public void FinishGameRuleGuide() {
  81. if (guideFinish) return;
  82. LoginMgr.myUserInfo.GetType().GetField($"gameRule{GameMgr.gameType}GuideFinish").SetValue(LoginMgr.myUserInfo, true);
  83. LoginMgr.myUserInfo.Save();
  84. CheckGuide();
  85. }
  86. public void FinishDeviceCalibrateGuide() {
  87. if (guideFinish) return;
  88. LoginMgr.myUserInfo.deviceCalibrateGuideFinish = true;
  89. LoginMgr.myUserInfo.Save();
  90. CheckGuide();
  91. }
  92. HashSet<Object> gamePauseLockers = new HashSet<Object>();
  93. public bool gamePause {
  94. get {
  95. return gamePauseLockers.Count > 0;
  96. }
  97. }
  98. public void addLockerForGamePause(Object o)
  99. {
  100. gamePauseLockers.Add(o);
  101. if (gamePauseLockers.Count > 0) {
  102. Time.timeScale = 0;
  103. }
  104. }
  105. public void removeLockerForGamePause(Object o)
  106. {
  107. gamePauseLockers.Remove(o);
  108. if (gamePauseLockers.Count == 0) {
  109. Time.timeScale = 1;
  110. }
  111. }
  112. //现实的计量值转游戏场景的计量值(米)
  113. public static float RealSizeToGameSize(float realSize)
  114. {
  115. // return realSize * 0.413966f;
  116. return realSize;
  117. }
  118. //游戏场景的计量值转现实的计量值(米)
  119. public static float GameSizeToRealSize(float gameSize)
  120. {
  121. // return gameSize / 0.413966f;
  122. return gameSize;
  123. }
  124. }
  125. public abstract class GameMode
  126. {
  127. public GameMgr gameMgr;
  128. public bool pauseTimeCounting {
  129. get {
  130. return timeCountingPauseLockers.Count > 0;
  131. }
  132. }
  133. HashSet<System.Object> timeCountingPauseLockers = new HashSet<System.Object>();
  134. public void PauseTimeCounting(System.Object o)
  135. {
  136. timeCountingPauseLockers.Add(o);
  137. }
  138. public void ResumeTimeCounting(System.Object o)
  139. {
  140. timeCountingPauseLockers.Remove(o);
  141. }
  142. public GameMode(GameMgr gameMgr) {
  143. this.gameMgr = gameMgr;
  144. }
  145. public virtual void HitTarget(float score) {}
  146. public virtual bool DoNextShoot() { return true; }
  147. public virtual object[] Settle() {return null; }
  148. public virtual void Start() {}
  149. public virtual void Update() {}
  150. public virtual void onBowReady() {}
  151. public virtual void onBowShoot() {}
  152. public void BanBowReady() {
  153. PauseTimeCounting(this);
  154. ArmBow armBow = GameObject.FindObjectOfType<ArmBow>();
  155. armBow.banReady = true;
  156. armBow.banShoot = true;
  157. }
  158. public void UnbanBowReady() {
  159. ResumeTimeCounting(this);
  160. ArmBow armBow = GameObject.FindObjectOfType<ArmBow>();
  161. armBow.banReady = false;
  162. armBow.banShoot = false;
  163. GameObject.FindObjectOfType<ArmBow>().readyShoot();
  164. }
  165. }
  166. public class GameModeTest : GameMode {
  167. public GameModeTest(GameMgr gameMgr) : base(gameMgr) {
  168. //记录可射击的靶子
  169. if (SceneManager.GetActiveScene().name == "Game") {
  170. TargetBody targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  171. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  172. }
  173. }
  174. public override void HitTarget(float score) {
  175. HitTargetNumber.Create(score);
  176. }
  177. }
  178. /**单人限时模式 */
  179. public class TimeLimitGameMode : GameMode {
  180. public static int[] distanceCanSelected = {10, 20, 30, 50, 70};
  181. public static int distance = 10;
  182. public float score = 0;
  183. int oneStarScore = 10;
  184. float time = 60;
  185. TargetBody targetBody;
  186. public TimeLimitGameMode(GameMgr gameMgr) : base(gameMgr) {
  187. //记录可射击的靶子
  188. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  189. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  190. //添加游戏界面
  191. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameView");
  192. GameObject.Instantiate(view);
  193. BanBowReady();
  194. }
  195. public override void Start()
  196. {
  197. UnbanBowReady();
  198. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameDistanceSelectView"));
  199. }
  200. public void ConfirmSelectedTargetDistance()
  201. {
  202. targetBody.SetDistance(distance);
  203. if (TimeLimitGameView.ins) TimeLimitGameView.ins.RenderHighestScoreByDistance(distance);
  204. TargetView.ins.Show(true);
  205. }
  206. public override void HitTarget(float score) {
  207. this.score += score;
  208. HitTargetNumber.Create(score);
  209. }
  210. public override bool DoNextShoot() {
  211. return !GameMgr.ins.gameOver;
  212. }
  213. public override object[] Settle() {
  214. int starCount = Mathf.FloorToInt(this.score / this.oneStarScore);
  215. float highestScore = 0;
  216. string distanceStr = distance.ToString();
  217. System.Object highestScoreObj = LoginMgr.myUserInfo.timeLimitGameHighestScores[distanceStr];
  218. if (highestScoreObj != null) highestScore = float.Parse(highestScoreObj.ToString());
  219. if (this.score > highestScore) {
  220. LoginMgr.myUserInfo.timeLimitGameHighestScores.Remove(distanceStr);
  221. LoginMgr.myUserInfo.timeLimitGameHighestScores.Add(distanceStr, this.score);
  222. LoginMgr.myUserInfo.Save();
  223. }
  224. return new object[]{starCount, this.score};
  225. }
  226. public override void Update() {
  227. if (gameMgr.gameOver || pauseTimeCounting) return;
  228. if (this.time > 0) {
  229. this.time -= Time.deltaTime;
  230. } else {
  231. this.time = 0;
  232. gameMgr.gameOver = true;
  233. gameMgr.StopGame();
  234. //添加结算界面
  235. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  236. GameObject.Instantiate(view);
  237. }
  238. }
  239. public string GetTimeStr()
  240. {
  241. int seconds = (int) Mathf.Ceil(this.time);
  242. string str = "";
  243. int m = seconds / 60;
  244. if (m < 10) {
  245. str += 0;
  246. }
  247. str += m;
  248. str += " : ";
  249. int s = seconds % 60;
  250. if (s < 10)
  251. {
  252. str += 0;
  253. }
  254. str += s;
  255. return str;
  256. }
  257. }
  258. /**双人PK模式 */
  259. public class PKGameMode : GameMode {
  260. public int currentPlayerIndex = 0;
  261. public int[] totalScores = {0, 0};
  262. public float[] currentScores = {0, 0};
  263. public int round = 1;
  264. public int showRoundValue = 0; //回合开始提示值,如果已经提示,则showRoundValue == round
  265. float[] targetDistancesOnRound = {10, 20, 30, 50, 70, 70};
  266. int maxRound = 5;
  267. int[] shootCount = {0, 0};
  268. int maxShootCount = 3;
  269. public float singleShootReadyTime = 20;
  270. public float singleShootReadyMaxTime = 20;
  271. bool singleShootTimeRunning = false;
  272. public static int[] playerRoleIDs = {1, 2};
  273. string[] gameRes = {"平局", "平局"};
  274. //玩家出场顺序
  275. Queue<int> appearPlayerIndexes = new Queue<int>();
  276. //记录可射击的靶子
  277. TargetBody targetBody;
  278. public PKGameMode(GameMgr gameMgr) : base(gameMgr) {
  279. InitAppearPlayerIndexes();
  280. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  281. //记录可射击的靶子
  282. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  283. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  284. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  285. //添加游戏界面
  286. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameView");
  287. GameObject.Instantiate(view);
  288. //禁止动作-相机和手臂
  289. BanBowReady();
  290. }
  291. public override void Start() {
  292. TargetView.ins.Show(true);
  293. //添加预备界面
  294. AddReadyView();
  295. }
  296. int[] sequencePlayerIndexes = new int[]{0, 1};
  297. void InitAppearPlayerIndexes()
  298. {
  299. if (round >= 2)
  300. {
  301. if (totalScores[0] < totalScores[1])
  302. {
  303. sequencePlayerIndexes = new int[]{0, 1};
  304. }
  305. else if (totalScores[1] < totalScores[0])
  306. {
  307. sequencePlayerIndexes = new int[]{1, 0};
  308. }
  309. }
  310. for (int i = 0; i < maxShootCount; i++) {
  311. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[0]);
  312. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[1]);
  313. }
  314. }
  315. void AddReadyView()
  316. {
  317. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView");
  318. GameObject.Instantiate(view);
  319. }
  320. public override void HitTarget(float score) {
  321. currentScores[currentPlayerIndex] += score;
  322. shootCount[currentPlayerIndex]++;
  323. HitTargetNumber.Create(score);
  324. }
  325. public override bool DoNextShoot() {
  326. if (gameMgr.gameOver) return false;
  327. bool nextRound = false;
  328. bool gameEnd = false; //游戏是否结束
  329. if (shootCount[0] == maxShootCount && shootCount[1] == maxShootCount ) {
  330. shootCount = new int[]{0, 0};
  331. nextRound = true;
  332. //更新总比分
  333. if (currentScores[0] == currentScores[1]) {
  334. totalScores[0] += 1;
  335. totalScores[1] += 1;
  336. } else if (currentScores[0] > currentScores[1]) {
  337. totalScores[0] += 2;
  338. } else if (currentScores[0] < currentScores[1]) {
  339. totalScores[1] += 2;
  340. }
  341. //根据总比分判断游戏是否结束
  342. if (totalScores[0] == totalScores[1]) {
  343. if (round == maxRound) {
  344. if (round == 5) {
  345. maxShootCount = 1;
  346. maxRound = 6;
  347. } else {
  348. gameEnd = true;
  349. gameRes = new string[]{"平局", "平局"};
  350. }
  351. }
  352. } else if (totalScores[0] >= 6 && totalScores[0] > totalScores[1]) {
  353. gameEnd = true;
  354. gameRes = new string[]{"胜利", "失败"};
  355. } else if (totalScores[1] >= 6 && totalScores[1] > totalScores[0]) {
  356. gameEnd = true;
  357. gameRes = new string[]{"失败", "胜利"};
  358. }
  359. }
  360. if (gameEnd) {
  361. gameMgr.gameOver = true;
  362. gameMgr.StopGame();
  363. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameSettleView");
  364. GameObject.Instantiate(view);
  365. return false;
  366. } else {
  367. //进入下一回合?
  368. if (nextRound) {
  369. round++;
  370. currentScores[0] = currentScores[1] = 0;
  371. InitAppearPlayerIndexes();
  372. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  373. }
  374. //本轮玩家登记
  375. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  376. //准备切换玩家
  377. BanBowReady();
  378. AddReadyView();
  379. //清除箭矢
  380. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  381. foreach (var arrow in arrows)
  382. {
  383. try {
  384. GameObject.Destroy(arrow.gameObject);
  385. } catch (UnityException e) {
  386. Debug.Log("Delete Arrow Error\n" + e.Message);
  387. }
  388. }
  389. }
  390. return true;
  391. }
  392. public override object[] Settle() {
  393. return gameRes;
  394. }
  395. public override void Update() {
  396. if (singleShootTimeRunning && !pauseTimeCounting) {
  397. singleShootReadyTime -= Time.deltaTime;
  398. if (singleShootReadyTime <= 0) {
  399. singleShootReadyTime = 0;
  400. singleShootTimeRunning = false;
  401. HitTarget(0);
  402. BanBowReady();
  403. //超时显示
  404. Text timeoutText = PKGameView.ins.transform.Find("TimeoutText").GetComponent<Text>();
  405. Sequence seq = DOTween.Sequence();
  406. seq.Append(timeoutText.DOFade(1, 0.5f));
  407. seq.AppendInterval(1);
  408. seq.Append(timeoutText.DOFade(0, 0.5f));
  409. seq.AppendCallback(delegate(){
  410. if (DoNextShoot()) {
  411. UnbanBowReady();
  412. }
  413. });
  414. }
  415. }
  416. }
  417. public string GetTimeStr()
  418. {
  419. int seconds = (int) Mathf.Ceil(this.singleShootReadyTime);
  420. string str = "";
  421. int m = seconds / 60;
  422. if (m < 10) {
  423. str += 0;
  424. }
  425. str += m;
  426. str += " : ";
  427. int s = seconds % 60;
  428. if (s < 10)
  429. {
  430. str += 0;
  431. }
  432. str += s;
  433. return str;
  434. }
  435. public override void onBowReady() {
  436. singleShootReadyTime = singleShootReadyMaxTime;
  437. singleShootTimeRunning = true;
  438. }
  439. public override void onBowShoot() {
  440. singleShootTimeRunning = false;
  441. }
  442. }