GameMgr.cs 15 KB

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