GameMgr.cs 12 KB

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