GameMgr.cs 15 KB

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