using UnityEngine; public class GameMgr : MonoBehaviour { public static int gameType = 0; public GameMode gameMode; public bool gameOver = false; public static GameMgr ins; void Start() { ins = this; AudioMgr.init(); this.InitGameMode(); } void FixedUpdate() { gameMode.Update(); } void InitGameMode() { if (gameType == 0) gameMode = new GameModeTest(this); if (gameType == 1) gameMode = new GameMode1(this); } public void StopGame() { Destroy(GameObject.FindObjectOfType()); Arrow[] arrows = GameObject.FindObjectsOfType(); foreach(var arrow in arrows) { Destroy(arrow); } } public void OpenBluetoothDebug() { BluetoothHolder.ins.openDebug(); } } public abstract class GameMode { public GameMgr gameMgr; public GameMode(GameMgr gameMgr) { this.gameMgr = gameMgr; } public abstract void HitTarget(int score); public abstract object[] Settle(); public virtual void Update() {} } public class GameModeTest : GameMode { public GameModeTest(GameMgr gameMgr) : base(gameMgr) {} public override void HitTarget(int score) {} public override object[] Settle() { return null; } } /**单人限时模式 */ public class GameMode1 : GameMode { public int score = 0; int oneStarScore = 10; float time = 60; public GameMode1(GameMgr gameMgr) : base(gameMgr) { //添加游戏界面 GameObject view = Resources.Load("Prefabs/Views/TimeLimitGameView"); GameObject.Instantiate(view); //记录可射击的靶子 TargetBody targetBody = GameObject.Find("GameArea/010/TargetBody").GetComponent(); GameObject.Find("Main Camera/ArmBow").GetComponent().validTargets.Add(targetBody); } public override void HitTarget(int score) { this.score += score; } public override object[] Settle() { int starCount = this.score / this.oneStarScore; string highestScoreKey = "TimeLimitGameHighestScore"; int highestScore = PlayerPrefs.GetInt(highestScoreKey, 0); if (this.score > highestScore) { PlayerPrefs.SetInt(highestScoreKey, this.score); } return new object[]{starCount, this.score}; } public override void Update() { if (gameMgr.gameOver) return; if (this.time > 0) { this.time -= Time.deltaTime; } else { this.time = 0; gameMgr.gameOver = true; gameMgr.StopGame(); //添加结算界面 GameObject view = Resources.Load("Prefabs/Views/GameSettleView1"); GameObject.Instantiate(view); } } public int GetHighestScore() { string highestScoreKey = "TimeLimitGameHighestScore"; return PlayerPrefs.GetInt(highestScoreKey, 0); } 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; } }