using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /**单人限时模式 */ public class TimeLimitGameMode : GameMode { public static int insCount; //被实例化的次数 public static int[] distanceCanSelected = {10, 20, 30, 50, 70}; public static int distance = 10; public static int insCountWillTryAgain; //哪一次的实例化会以再次挑战进行----用于再次挑战按钮 public float score = 0; int oneStarScore = 10; float time = 60; TargetBody targetBody; public Action onHitTargetEvent; public TimeLimitGameMode(GameMgr gameMgr) : base(gameMgr) { insCount++; //记录可射击的靶子 targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent(); GameObject.FindObjectOfType().validTargets.Add(targetBody); //添加游戏界面 GameObject view = Resources.Load("Prefabs/Views/TimeLimitGameView"); GameObject.Instantiate(view); BanBowReady(); } public override void Start() { UnbanBowReady(); if (insCount == insCountWillTryAgain) { ConfirmSelectedTargetDistance(); } else { if (GameMgr.bShowDistance) { GameObject.Instantiate(Resources.Load("Prefabs/Views/TimeLimitGameDistanceSelectView")); } } } public void ConfirmSelectedTargetDistance() { targetBody.SetDistance(distance); TargetView.ins.Show(true); gameMgr.StartCoroutine(RenderHighestScoreByDistance()); } private IEnumerator RenderHighestScoreByDistance() { yield return null; yield return null; if (TimeLimitGameView.ins) TimeLimitGameView.ins.RenderHighestScoreByDistance(distance); } public override void HitTarget(float score) { this.score += score; HitTargetNumber.Create(score); onHitTargetEvent?.Invoke(); if(score != 0) gameMgr.StartCoroutine(NavDeviceView()); } //跳回homeView 后,跳转到deviceview IEnumerator NavDeviceView() { yield return new WaitForSeconds(1.0f); if (GameMgr.bNavBack) { InfraredGuider _infraredGuider = UnityEngine.Object.FindObjectOfType(); if (_infraredGuider != null) { //红外光引导射击,如果是显示这个状态 if (_infraredGuider.mTipStep == TipStep.Finish) { GameMgr.bNavBack = false; //直接回到主页 UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single); } } else { GameMgr.bNavBack = false; //普通引导时候射击 HomeView.bOpenOtherView = true; HomeView.openName = "DeviceView"; UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single); } } } public override bool DoNextShoot() { return !GameMgr.ins.gameOver; } public override object[] Settle() { int starCount = Mathf.FloorToInt(this.score / this.oneStarScore); float highestScore = 0; string distanceStr = distance.ToString(); if (LoginMgr.myUserInfo.timeLimitGameScores.ContainsKey(distanceStr)) { highestScore = LoginMgr.myUserInfo.timeLimitGameScores[distanceStr]; } if (this.score > highestScore) { LoginMgr.myUserInfo.timeLimitGameScores.Remove(distanceStr); LoginMgr.myUserInfo.timeLimitGameScores.Add(distanceStr, this.score); LoginMgr.myUserInfo.Save(); } return new object[]{starCount, this.score}; } public override void Update() { #if UNITY_EDITOR if (Input.GetKey(KeyCode.W) && this.time > 0) { //win Debug.LogWarning("debug-win"); this.score = 100; this.time = 0; } if (Input.GetKey(KeyCode.F) && this.time > 0) { //fail Debug.LogWarning("debug-fail"); this.score = 10; this.time = 0; } #endif if (gameMgr.gameOver || pauseTimeCounting) return; //不进入计时器 if (GameMgr.turnOffTimer) return; if (this.time > 0) { if (GlobalData.pkMatchType == PKMatchType.None && UserSettings.ins.trainMode) { //单人且为训练模式,就不要倒计时了 } else { this.time -= Time.deltaTime; } if (Billboard.ins && Billboard.ins.bulletManager.NumberOfShotsFired()) { //超过射击次数,设置运行时间0,等待结束游戏 this.time = 0; } } else { this.time = 0; gameMgr.StopGame(); //添加结算界面 GameObject view = Resources.Load("Prefabs/Views/TimeLimitGameSettleView"); GameObject.Instantiate(view); } } public string GetTimeStr() { int seconds = (int) Mathf.Ceil(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; } }