| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- 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<BowCamera>());
- Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
- 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<GameObject>("Prefabs/Views/TimeLimitGameView");
- GameObject.Instantiate(view);
- //记录可射击的靶子
- TargetBody targetBody = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
- GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().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<GameObject>("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;
- }
- }
|