GameMgr.cs 13 KB

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