GameMgr.cs 13 KB

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