GameMgr.cs 16 KB

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