using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.UI; /**静止靶-本地pk模式 */ public class PKGameMode : GameMode { public int currentPlayerIndex = 0; public int[] totalScores = {0, 0}; public float[] 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 appearPlayerIndexes = new Queue(); //记录可射击的靶子 TargetBody targetBody; public PKGameMode(GameMgr gameMgr) : base(gameMgr) { playerRoleIDs = GlobalData.localPK_playerRoleIDs; InitAppearPlayerIndexes(); currentPlayerIndex = appearPlayerIndexes.Dequeue(); //记录可射击的靶子 targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent(); GameObject.FindObjectOfType().validTargets.Add(targetBody); targetBody.SetDistance(targetDistancesOnRound[round - 1]); //添加游戏界面 GameObject view = Resources.Load("Prefabs/Views/PKGameView"); GameObject.Instantiate(view); //禁止动作-相机和手臂 BanBowReady(); } public override void Start() { TargetView.ins.Show(true); //添加预备界面 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("Prefabs/Views/PKGameReadyView"); GameObject.Instantiate(view); } public override void HitTarget(float 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.StopGame(); GameObject view = Resources.Load("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(); 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(); 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; } }