GameMgr.cs 15 KB

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