using UnityEngine; public class GameMgr : MonoBehaviour { public static int gameMode = 1; public GameMode1 gameMode1; public GameMode2 gameMode2; public bool gameOver = false; public static GameMgr ins; void Start() { ins = this; AudioMgr.init(); this.initGameMode(); } void FixedUpdate() { if (this.gameMode2 != null) { this.gameMode2.UpdateTime(this); } } void initGameMode() { if (gameMode == 1) { this.gameMode1 = new GameMode1(); this.gameMode1.init(this); } else if (gameMode == 2) { this.gameMode2 = new GameMode2(); this.gameMode2.init(this); } } public void hitTarget(int score) { if (gameMode == 1) { this.gameMode1.hitTarget(this, score); } else if (gameMode == 2) { this.gameMode2.hitTarget(this, score); } } public bool checkFinish() { if (gameMode == 1) { return this.gameMode1.checkFinish(this); } else if (gameMode == 2) { return this.gameMode2.checkFinish(this); } return false; } public GameWinResult GetGameWinResult() { if (gameMode == 1) { return gameMode1.getWinResult(); } else if (gameMode == 2) { return gameMode2.getWinResult(); } return null; } public void Pause() { Time.timeScale = 0; } public void Resume() { Time.timeScale = 1; } public void StopGame() { Destroy(GameObject.FindObjectOfType()); Arrow[] arrows = GameObject.FindObjectsOfType(); foreach(var arrow in arrows) { Destroy(arrow); } } public void OpenBluetoothDebug() { BluetoothHolder.ins.openDebug(); } } /**闯关模式 */ public class GameMode1 { public static int level; public int arrowCount = 3; public int arrowCountMax = 3; public int score = 0; public int oneStarScore = 8; public bool finish; public void init(GameMgr gameMgr) { GameObject gv1 = Resources.Load("Prefabs/Views/ChallengeGameView"); GameObject.Instantiate(gv1, Vector3.zero, new Quaternion()); //debug TargetBody tb = GameObject.Find("GameArea/010/TargetBody").GetComponent(); // TargetBody tb1 = GameObject.Find("GameArea/010 (1)/TargetBody").GetComponent(); // TargetBody tb2 = GameObject.Find("GameArea/010 (2)/TargetBody").GetComponent(); GameObject.Find("Main Camera/ArmBow").GetComponent().validTargets.Add(tb); // GameObject.Find("Main Camera/ArmBow").GetComponent().validTargets.Add(tb1); // GameObject.Find("Main Camera/ArmBow").GetComponent().validTargets.Add(tb2); } public void hitTarget(GameMgr gameMgr, int score) { // this.score += score; // this.arrowCount -= 1; } public bool checkFinish(GameMgr gameMgr) { if (this.finish) return true; if (this.arrowCount <= 0) { this.finish = true; gameMgr.gameOver = true; gameMgr.StopGame(); GameObject gwv1 = Resources.Load("Prefabs/Views/GameResultView"); GameObject.Instantiate(gwv1, Vector3.zero, new Quaternion()); return true; } return false; } public GameWinResult getWinResult() { GameWinResult result = new GameWinResult(); result.starCount = this.score / this.oneStarScore; result.score = this.score; string key = "Challenge_Star_"; int star = PlayerPrefs.GetInt(key + level, 0); if (result.starCount > star) { PlayerPrefs.SetInt(key + level, result.starCount); } return result; } } /**限时模式 */ public class GameMode2 { public int hitCount = 0; public int score = 0; public int oneStarScore = 10; public float time = 60; public bool finish; public void init(GameMgr gameMgr) { GameObject gv1 = Resources.Load("Prefabs/Views/TimeLimitGameView"); GameObject.Instantiate(gv1, Vector3.zero, new Quaternion()); TargetBody tb = GameObject.Find("GameArea/010/TargetBody").GetComponent(); GameObject.Find("Main Camera/ArmBow").GetComponent().validTargets.Add(tb); } public void hitTarget(GameMgr gameMgr, int score) { this.score += score; this.hitCount++; } public bool checkFinish(GameMgr gameMgr) { return this.finish; } public GameWinResult getWinResult() { GameWinResult result = new GameWinResult(); result.starCount = this.score / this.oneStarScore; result.score = this.score; string keyLowest = "TimeLimitGameLowestScore"; string keyHighest = "TimeLimitGameHighestScore"; string keyLowestHitCount = "TimeLimitGameLowestHitCount"; string keyHighestHitCount = "TimeLimitGameHighestHitCount"; int lowestScore = PlayerPrefs.GetInt(keyLowest, -1); int highestScore = PlayerPrefs.GetInt(keyHighest, -1); if (lowestScore == -1 || result.score < lowestScore) { PlayerPrefs.SetInt(keyLowest, result.score); PlayerPrefs.SetInt(keyLowestHitCount, hitCount); } if (highestScore == -1 || result.score > highestScore) { PlayerPrefs.SetInt(keyHighest, result.score); PlayerPrefs.SetInt(keyHighestHitCount, hitCount); } return result; } public int[] GetScores() { int[] scores = new int[4]; string keyLowest = "TimeLimitGameLowestScore"; string keyHighest = "TimeLimitGameHighestScore"; string keyLowestHitCount = "TimeLimitGameLowestHitCount"; string keyHighestHitCount = "TimeLimitGameHighestHitCount"; scores[0] = PlayerPrefs.GetInt(keyHighestHitCount, 0); scores[1] = PlayerPrefs.GetInt(keyHighest, 0); scores[2] = PlayerPrefs.GetInt(keyLowestHitCount, 0); scores[3] = PlayerPrefs.GetInt(keyLowest, 0); return scores; } public void UpdateTime(GameMgr gameMgr) { if (this.finish) return; if (this.time > 0) { this.time -= Time.deltaTime; } else { this.time = 0; this.finish = true; gameMgr.gameOver = true; gameMgr.StopGame(); GameObject gwv1 = Resources.Load("Prefabs/Views/GameResultView"); GameObject.Instantiate(gwv1, Vector3.zero, new Quaternion()); } } public string getTimeStr() { int seconds = Mathf.FloorToInt(this.time); string str = ""; int m = seconds / 60; if (m < 10) { str += 0; } str += m; str += " : "; int s = seconds % 60; if (s < 10) { str += 0; } str += s; return str; } } public class GameWinResult { public int starCount; public int score; }