PKGameMode_LocalPK.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using UnityEngine.UI;
  6. /**静止靶-本地pk模式 */
  7. public class PKGameMode : GameMode {
  8. public int currentPlayerIndex = 0;
  9. public int[] totalScores = {0, 0};
  10. public float[] currentScores = {0, 0};
  11. public int round = 1;
  12. public int showRoundValue = 0; //回合开始提示值,如果已经提示,则showRoundValue == round
  13. float[] targetDistancesOnRound = {10, 20, 30, 50, 70, 70};
  14. int maxRound = 5;
  15. int[] shootCount = {0, 0};
  16. int maxShootCount = 3;
  17. public float singleShootReadyTime = 20;
  18. public float singleShootReadyMaxTime = 20;
  19. bool singleShootTimeRunning = false;
  20. public static int[] playerRoleIDs = {1, 2};
  21. string[] gameRes = {"平局", "平局"};
  22. //玩家出场顺序
  23. Queue<int> appearPlayerIndexes = new Queue<int>();
  24. //记录可射击的靶子
  25. TargetBody targetBody;
  26. public PKGameMode(GameMgr gameMgr) : base(gameMgr) {
  27. playerRoleIDs = GlobalData.localPK_playerRoleIDs;
  28. InitAppearPlayerIndexes();
  29. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  30. //记录可射击的靶子
  31. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  32. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  33. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  34. //添加游戏界面
  35. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameView");
  36. GameObject.Instantiate(view);
  37. //禁止动作-相机和手臂
  38. BanBowReady();
  39. }
  40. public override void Start() {
  41. TargetView.ins.Show(true);
  42. //添加预备界面
  43. AddReadyView();
  44. }
  45. int[] sequencePlayerIndexes = new int[]{0, 1};
  46. void InitAppearPlayerIndexes()
  47. {
  48. if (round >= 2)
  49. {
  50. if (totalScores[0] < totalScores[1])
  51. {
  52. sequencePlayerIndexes = new int[]{0, 1};
  53. }
  54. else if (totalScores[1] < totalScores[0])
  55. {
  56. sequencePlayerIndexes = new int[]{1, 0};
  57. }
  58. }
  59. for (int i = 0; i < maxShootCount; i++) {
  60. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[0]);
  61. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[1]);
  62. }
  63. }
  64. void AddReadyView()
  65. {
  66. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView");
  67. GameObject.Instantiate(view);
  68. }
  69. public override void HitTarget(float score) {
  70. currentScores[currentPlayerIndex] += score;
  71. shootCount[currentPlayerIndex]++;
  72. HitTargetNumber.Create(score);
  73. }
  74. public override bool DoNextShoot() {
  75. if (gameMgr.gameOver) return false;
  76. bool nextRound = false;
  77. bool gameEnd = false; //游戏是否结束
  78. if (shootCount[0] == maxShootCount && shootCount[1] == maxShootCount ) {
  79. shootCount = new int[]{0, 0};
  80. nextRound = true;
  81. //更新总比分
  82. if (currentScores[0] == currentScores[1]) {
  83. totalScores[0] += 1;
  84. totalScores[1] += 1;
  85. } else if (currentScores[0] > currentScores[1]) {
  86. totalScores[0] += 2;
  87. } else if (currentScores[0] < currentScores[1]) {
  88. totalScores[1] += 2;
  89. }
  90. //根据总比分判断游戏是否结束
  91. if (totalScores[0] == totalScores[1]) {
  92. if (round == maxRound) {
  93. if (round == 5) {
  94. maxShootCount = 1;
  95. maxRound = 6;
  96. } else {
  97. gameEnd = true;
  98. gameRes = new string[]{"平局", "平局"};
  99. }
  100. }
  101. } else if (totalScores[0] >= 6 && totalScores[0] > totalScores[1]) {
  102. gameEnd = true;
  103. gameRes = new string[]{"胜利", "失败"};
  104. } else if (totalScores[1] >= 6 && totalScores[1] > totalScores[0]) {
  105. gameEnd = true;
  106. gameRes = new string[]{"失败", "胜利"};
  107. }
  108. }
  109. if (gameEnd) {
  110. gameMgr.StopGame();
  111. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameSettleView");
  112. GameObject.Instantiate(view);
  113. return false;
  114. } else {
  115. //进入下一回合?
  116. if (nextRound) {
  117. round++;
  118. currentScores[0] = currentScores[1] = 0;
  119. InitAppearPlayerIndexes();
  120. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  121. }
  122. //本轮玩家登记
  123. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  124. //准备切换玩家
  125. BanBowReady();
  126. AddReadyView();
  127. //清除箭矢
  128. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  129. foreach (var arrow in arrows)
  130. {
  131. try {
  132. GameObject.Destroy(arrow.gameObject);
  133. } catch (UnityException e) {
  134. Debug.Log("Delete Arrow Error\n" + e.Message);
  135. }
  136. }
  137. }
  138. return true;
  139. }
  140. public override object[] Settle() {
  141. return gameRes;
  142. }
  143. public override void Update() {
  144. if (singleShootTimeRunning && !pauseTimeCounting) {
  145. singleShootReadyTime -= Time.deltaTime;
  146. if (singleShootReadyTime <= 0) {
  147. singleShootReadyTime = 0;
  148. singleShootTimeRunning = false;
  149. HitTarget(0);
  150. BanBowReady();
  151. //超时显示
  152. Text timeoutText = PKGameView.ins.transform.Find("TimeoutText").GetComponent<Text>();
  153. Sequence seq = DOTween.Sequence();
  154. seq.Append(timeoutText.DOFade(1, 0.5f));
  155. seq.AppendInterval(1);
  156. seq.Append(timeoutText.DOFade(0, 0.5f));
  157. seq.AppendCallback(delegate(){
  158. if (DoNextShoot()) {
  159. UnbanBowReady();
  160. }
  161. });
  162. }
  163. }
  164. }
  165. public string GetTimeStr()
  166. {
  167. int seconds = (int) Mathf.Ceil(this.singleShootReadyTime);
  168. string str = "";
  169. int m = seconds / 60;
  170. if (m < 10) {
  171. str += 0;
  172. }
  173. str += m;
  174. str += " : ";
  175. int s = seconds % 60;
  176. if (s < 10)
  177. {
  178. str += 0;
  179. }
  180. str += s;
  181. return str;
  182. }
  183. public override void onBowReady() {
  184. singleShootReadyTime = singleShootReadyMaxTime;
  185. singleShootTimeRunning = true;
  186. }
  187. public override void onBowShoot() {
  188. singleShootTimeRunning = false;
  189. }
  190. }