| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 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<BowCamera>());
- Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
- foreach(var arrow in arrows)
- {
- Destroy(arrow);
- }
- }
- }
- /**闯关模式 */
- 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<GameObject>("Prefabs/Views/ChallengeGameView");
- GameObject.Instantiate(gv1, Vector3.zero, new Quaternion());
- //debug
- TargetBody tb = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
- // TargetBody tb1 = GameObject.Find("GameArea/010 (1)/TargetBody").GetComponent<TargetBody>();
- // TargetBody tb2 = GameObject.Find("GameArea/010 (2)/TargetBody").GetComponent<TargetBody>();
- GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb);
- // GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb1);
- // GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().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<GameObject>("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<GameObject>("Prefabs/Views/TimeLimitGameView");
- GameObject.Instantiate(gv1, Vector3.zero, new Quaternion());
- TargetBody tb = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
- GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().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<GameObject>("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;
- }
|