| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using DG.Tweening;
- using UnityEngine.UI;
- public class GameMgr : MonoBehaviour
- {
- public static bool debugInEditor = false;
- public static int gameType = 0;
- public GameMode gameMode;
- public bool gameOver = false;
- public static GameMgr ins;
- void Awake()
- {
- ins = this;
- if (Application.platform == RuntimePlatform.WindowsEditor)
- {
- debugInEditor = true;
- }
- AudioMgr.Init();
- this.InitGameMode();
- // if (debugInEditor) {
- // guideFinish = true;
- // gameMode.Start();
- // } else {
- // if (!BluetoothStatus.IsAllConnected()) {
- // GameObject view = DeviceReconnectView.Show();
- // if (view) {
- // view.GetComponent<DeviceReconnectView>().onComplete = CheckGuide;
- // }
- // } else {
- // this.CheckGuide();
- // }
- // }
- guideFinish = true;
- gameMode.Start();
- }
- void Start()
- {
- if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
- }
- void FixedUpdate()
- {
- gameMode.Update();
- }
- void InitGameMode() {
- if (gameType == 0) gameMode = new GameModeTest(this);
- if (gameType == 1) gameMode = new TimeLimitGameMode(this);
- if (gameType == 2) gameMode = new PKGameMode(this);
- }
- public void StopGame() {
- Destroy(GameObject.FindObjectOfType<BowCamera>());
- Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
- foreach(var arrow in arrows)
- {
- Destroy(arrow);
- }
- }
- bool guideFinish = false;
- public void CheckGuide() {
- if (gameType > 0) {
- if (!LoginMgr.myUserInfo.deviceCalibrateGuideFinish) {
- DeviceCalibrateView.Create();
- return;
- }
- bool gameRuleGuideFinish = (bool)LoginMgr.myUserInfo.GetType().GetField($"gameRule{GameMgr.gameType}GuideFinish").GetValue(LoginMgr.myUserInfo);
- if (!gameRuleGuideFinish) {
- GameRuleView.Create();
- return;
- }
- }
- guideFinish = true;
- gameMode.Start();
- }
- public void FinishGameRuleGuide() {
- if (guideFinish) return;
- LoginMgr.myUserInfo.GetType().GetField($"gameRule{GameMgr.gameType}GuideFinish").SetValue(LoginMgr.myUserInfo, true);
- LoginMgr.myUserInfo.Save();
- CheckGuide();
- }
-
- public void FinishDeviceCalibrateGuide() {
- if (guideFinish) return;
- LoginMgr.myUserInfo.deviceCalibrateGuideFinish = true;
- LoginMgr.myUserInfo.Save();
- CheckGuide();
- }
- HashSet<Object> gamePauseLockers = new HashSet<Object>();
- public bool gamePause {
- get {
- return gamePauseLockers.Count > 0;
- }
- }
- public void addLockerForGamePause(Object o)
- {
- gamePauseLockers.Add(o);
- if (gamePauseLockers.Count > 0) {
- Time.timeScale = 0;
- }
- }
- public void removeLockerForGamePause(Object o)
- {
- gamePauseLockers.Remove(o);
- if (gamePauseLockers.Count == 0) {
- Time.timeScale = 1;
- }
- }
- //现实的计量值转游戏场景的计量值(米)
- public static float RealSizeToGameSize(float realSize)
- {
- return realSize * 0.413966f;
- }
- //游戏场景的计量值转现实的计量值(米)
- public static float GameSizeToRealSize(float gameSize)
- {
- return gameSize / 0.413966f;
- }
- }
- public abstract class GameMode
- {
- public GameMgr gameMgr;
- public bool pauseTimeCounting {
- get {
- return timeCountingPauseLockers.Count > 0;
- }
- }
- HashSet<System.Object> timeCountingPauseLockers = new HashSet<System.Object>();
- public void PauseTimeCounting(System.Object o)
- {
- timeCountingPauseLockers.Add(o);
- }
- public void ResumeTimeCounting(System.Object o)
- {
- timeCountingPauseLockers.Remove(o);
- }
- public GameMode(GameMgr gameMgr) {
- this.gameMgr = gameMgr;
- }
- public abstract void HitTarget(int score);
- public abstract bool DoNextShoot();
- public abstract object[] Settle();
- public virtual void Start() {}
- public virtual void Update() {}
- public virtual void onBowReady() {}
- public virtual void onBowShoot() {}
- public void BanBowReady() {
- PauseTimeCounting(this);
- ArmBow armBow = GameObject.FindObjectOfType<ArmBow>();
- armBow.banReady = true;
- armBow.banShoot = true;
- }
- public void UnbanBowReady() {
- ResumeTimeCounting(this);
- ArmBow armBow = GameObject.FindObjectOfType<ArmBow>();
- armBow.banReady = false;
- armBow.banShoot = false;
- GameObject.FindObjectOfType<ArmBow>().readyShoot();
- }
- }
- public class GameModeTest : GameMode {
- public GameModeTest(GameMgr gameMgr) : base(gameMgr) {
- //记录可射击的靶子
- 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) {
- HitTargetNumber.Create(score);
- }
- public override bool DoNextShoot() { return true; }
- public override object[] Settle() { return null; }
- }
- /**单人限时模式 */
- public class TimeLimitGameMode : GameMode {
- public static int[] distanceCanSelected = {10, 20, 30, 50, 70};
- public static int distance = 10;
- public int score = 0;
- int oneStarScore = 10;
- float time = 60;
- TargetBody targetBody;
- public TimeLimitGameMode(GameMgr gameMgr) : base(gameMgr) {
- //记录可射击的靶子
- targetBody = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
- GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(targetBody);
- //添加游戏界面
- GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameView");
- GameObject.Instantiate(view);
- BanBowReady();
- }
- public override void Start()
- {
- UnbanBowReady();
- GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameDistanceSelectView"));
- }
- public void RefreshTargetDistance()
- {
- targetBody.SetDistance(distance);
- }
- public override void HitTarget(int score) {
- this.score += score;
- HitTargetNumber.Create(score);
- }
- public override bool DoNextShoot() {
- return !GameMgr.ins.gameOver;
- }
- public override object[] Settle() {
- int starCount = this.score / this.oneStarScore;
- int highestScore = LoginMgr.myUserInfo.timeLimitGameHighestScore;
- if (this.score > highestScore) {
- LoginMgr.myUserInfo.timeLimitGameHighestScore = this.score;
- LoginMgr.myUserInfo.Save();
- }
- return new object[]{starCount, this.score};
- }
- public override void Update() {
- if (gameMgr.gameOver || pauseTimeCounting) 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/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;
- }
- }
- /**双人PK模式 */
- public class PKGameMode : GameMode {
- public int currentPlayerIndex = 0;
- public int[] totalScores = {0, 0};
- public int[] currentScores = {0, 0};
- public int round = 1;
- public int showRoundValue = 0; //回合开始提示值,如果已经提示,则showRoundValue == round
- float[] targetDistancesOnRound = {10, 20, 30, 50, 70, 70};
- int maxRound = 5;
- int[] shootCount = {0, 0};
- int maxShootCount = 3;
- public float singleShootReadyTime = 20;
- public float singleShootReadyMaxTime = 20;
- bool singleShootTimeRunning = false;
- public static int[] playerRoleIDs = {1, 2};
- string[] gameRes = {"平局", "平局"};
- //玩家出场顺序
- Queue<int> appearPlayerIndexes = new Queue<int>();
- //记录可射击的靶子
- TargetBody targetBody;
- public PKGameMode(GameMgr gameMgr) : base(gameMgr) {
- InitAppearPlayerIndexes();
- currentPlayerIndex = appearPlayerIndexes.Dequeue();
- //记录可射击的靶子
- targetBody = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
- GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(targetBody);
- targetBody.SetDistance(targetDistancesOnRound[round - 1]);
- //添加游戏界面
- GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameView");
- GameObject.Instantiate(view);
- //禁止动作-相机和手臂
- BanBowReady();
- }
- public override void Start() {
- //添加预备界面
- AddReadyView();
- }
- int[] sequencePlayerIndexes = new int[]{0, 1};
- void InitAppearPlayerIndexes()
- {
- if (round >= 2)
- {
- if (totalScores[0] < totalScores[1])
- {
- sequencePlayerIndexes = new int[]{0, 1};
- }
- else if (totalScores[1] < totalScores[0])
- {
- sequencePlayerIndexes = new int[]{1, 0};
- }
- }
- for (int i = 0; i < maxShootCount; i++) {
- appearPlayerIndexes.Enqueue(sequencePlayerIndexes[0]);
- appearPlayerIndexes.Enqueue(sequencePlayerIndexes[1]);
- }
- }
- void AddReadyView()
- {
- GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView");
- GameObject.Instantiate(view);
- }
- public override void HitTarget(int score) {
- currentScores[currentPlayerIndex] += score;
- shootCount[currentPlayerIndex]++;
- HitTargetNumber.Create(score);
- }
- public override bool DoNextShoot() {
- if (gameMgr.gameOver) return false;
- bool nextRound = false;
- bool gameEnd = false; //游戏是否结束
- if (shootCount[0] == maxShootCount && shootCount[1] == maxShootCount ) {
- shootCount = new int[]{0, 0};
- nextRound = true;
- //更新总比分
- if (currentScores[0] == currentScores[1]) {
- totalScores[0] += 1;
- totalScores[1] += 1;
- } else if (currentScores[0] > currentScores[1]) {
- totalScores[0] += 2;
- } else if (currentScores[0] < currentScores[1]) {
- totalScores[1] += 2;
- }
- //根据总比分判断游戏是否结束
- if (totalScores[0] == totalScores[1]) {
- if (round == maxRound) {
- if (round == 5) {
- maxShootCount = 1;
- maxRound = 6;
- } else {
- gameEnd = true;
- gameRes = new string[]{"平局", "平局"};
- }
- }
- } else if (totalScores[0] >= 6 && totalScores[0] > totalScores[1]) {
- gameEnd = true;
- gameRes = new string[]{"胜利", "失败"};
- } else if (totalScores[1] >= 6 && totalScores[1] > totalScores[0]) {
- gameEnd = true;
- gameRes = new string[]{"失败", "胜利"};
- }
- }
- if (gameEnd) {
- gameMgr.gameOver = true;
- gameMgr.StopGame();
- GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameSettleView");
- GameObject.Instantiate(view);
- return false;
- } else {
- //进入下一回合?
- if (nextRound) {
- round++;
- currentScores[0] = currentScores[1] = 0;
- InitAppearPlayerIndexes();
- targetBody.SetDistance(targetDistancesOnRound[round - 1]);
- }
- //本轮玩家登记
- currentPlayerIndex = appearPlayerIndexes.Dequeue();
- //准备切换玩家
- BanBowReady();
- AddReadyView();
- //清除箭矢
- Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
- foreach (var arrow in arrows)
- {
- try {
- GameObject.Destroy(arrow.gameObject);
- } catch (UnityException e) {
- Debug.Log("Delete Arrow Error\n" + e.Message);
- }
- }
- }
- return true;
- }
- public override object[] Settle() {
- return gameRes;
- }
- public override void Update() {
- if (singleShootTimeRunning && !pauseTimeCounting) {
- singleShootReadyTime -= Time.deltaTime;
- if (singleShootReadyTime <= 0) {
- singleShootReadyTime = 0;
- singleShootTimeRunning = false;
- HitTarget(0);
- BanBowReady();
- //超时显示
- Text timeoutText = PKGameView.ins.transform.Find("TimeoutText").GetComponent<Text>();
- Sequence seq = DOTween.Sequence();
- seq.Append(timeoutText.DOFade(1, 0.5f));
- seq.AppendInterval(1);
- seq.Append(timeoutText.DOFade(0, 0.5f));
- seq.AppendCallback(delegate(){
- if (DoNextShoot()) {
- UnbanBowReady();
- }
- });
- }
- }
- }
- public string GetTimeStr()
- {
- int seconds = (int) Mathf.Ceil(this.singleShootReadyTime);
- 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 override void onBowReady() {
- singleShootReadyTime = singleShootReadyMaxTime;
- singleShootTimeRunning = true;
- }
- public override void onBowShoot() {
- singleShootTimeRunning = false;
- }
- }
|