GameMgr.cs 15 KB

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