GameMgr.cs 15 KB

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