PKGameMode_OnlinePK.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using DG.Tweening;
  6. using Newtonsoft.Json;
  7. /* 静止靶-联机pk模式 */
  8. public class PKGameMode_OnlinePK : GameMode {
  9. public int myPlayerIndex = -111;
  10. public bool IsMyPlayerInited() {
  11. return myPlayerIndex >= 0;
  12. }
  13. public bool IsMyPlayerRunning() {
  14. return myPlayerIndex == gameLogic.currentPlayerIndex;
  15. }
  16. public PKGameMode_OnlinePK(GameMgr gameMgr) : base(gameMgr) {
  17. GlobalData.pkMatchType = PKMatchType.OnlinePK;
  18. gameLogic.InitAppearPlayerIndexes();
  19. gameLogic.SetCurrentPlayerIndexToNext();
  20. //记录可射击的靶子
  21. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  22. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  23. //禁止动作-相机和手臂
  24. BanBowReady();
  25. AutoSwitchBanUserControlBow();
  26. }
  27. void ToInitGameView() {
  28. //添加游戏界面
  29. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameView");
  30. GameObject.Instantiate(view);
  31. }
  32. Quaternion bowTargetQua;
  33. bool hasBowTargBtQua;
  34. void onReceivePKGameData(string key, string data) {
  35. if (key == "logic") {
  36. GameLogic gameLogic = JsonConvert.DeserializeObject<GameLogic>(data);
  37. if (
  38. (gameLogic.nextPlayerExcuteID == this.gameLogic.nextPlayerExcuteID && myPlayerIndex != gameLogic.currentPlayerIndex) ||
  39. (gameLogic.nextPlayerExcuteID > this.gameLogic.nextPlayerExcuteID)
  40. ) {
  41. this.gameLogic = gameLogic;
  42. AutoSwitchBanUserControlBow();
  43. }
  44. }
  45. if (key == "bow") {
  46. if (!IsMyPlayerRunning()) {
  47. string[] quaStr = data.Split(',');
  48. if (quaStr.Length == 6) {
  49. bowTargetQua.x = float.Parse(quaStr[0]);
  50. bowTargetQua.y = float.Parse(quaStr[1]);
  51. bowTargetQua.z = float.Parse(quaStr[2]);
  52. bowTargetQua.w = float.Parse(quaStr[3]);
  53. hasBowTargBtQua = true;
  54. ArmBow.ins.phase = int.Parse(quaStr[4]);
  55. GameAssistUI.ins.playerScaleAimValue_OnlinePK = int.Parse(quaStr[5]);
  56. }
  57. }
  58. }
  59. if (key == "arrow") {
  60. List<ArrowSync.SyncData> arrowSyncDataList = JsonConvert.DeserializeObject<List<ArrowSync.SyncData>>(data);
  61. foreach (var item in arrowSyncMap) {
  62. item.Value.hasSetSyncData = false;
  63. }
  64. GameObject arrowPrefab = ArmBow.ins.arrow;
  65. foreach (var item in arrowSyncDataList) {
  66. ArrowSync arrowSync;
  67. arrowSyncMap.TryGetValue(item.id, out arrowSync);
  68. if (arrowSync == null) {
  69. GameObject arrowObj = GameObject.Instantiate(arrowPrefab);
  70. arrowSync = arrowObj.AddComponent<ArrowSync>();
  71. arrowSync.SetSyncData(item, true);
  72. arrowSyncMap[item.id] = arrowSync;
  73. } else {
  74. arrowSync.SetSyncData(item);
  75. }
  76. arrowSync.hasSetSyncData = true;
  77. }
  78. List<int> removeIDs = null;
  79. foreach (var item in arrowSyncMap) {
  80. if (!item.Value.hasSetSyncData) {
  81. if (removeIDs == null) removeIDs = new List<int>();
  82. removeIDs.Add(item.Key);
  83. }
  84. }
  85. if (removeIDs != null) {
  86. foreach (var id in removeIDs) {
  87. ArrowSync arrowSync = arrowSyncMap[id];
  88. arrowSyncMap.Remove(id);
  89. if (arrowSync && arrowSync.gameObject) {
  90. GameObject.Destroy(arrowSync.gameObject);
  91. }
  92. }
  93. }
  94. }
  95. if (key == "score") {
  96. HitTargetNumber.Create(float.Parse(data));
  97. }
  98. if (key == "shootSpeed") {
  99. Billboard.ins?.SetShootSpeedText(data);
  100. }
  101. }
  102. void AutoSwitchBanUserControlBow() {
  103. BowCamera.ins.banLogic = ArmBow.ins.banLogic = !IsMyPlayerRunning();
  104. }
  105. //记录可射击的靶子
  106. TargetBody targetBody;
  107. bool hasStart = false;
  108. public override void Start() {
  109. hasStart = true;
  110. TargetView.ins.Show(true);
  111. }
  112. void AddReadyView()
  113. {
  114. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView");
  115. GameObject.Instantiate(view);
  116. }
  117. public override void HitTarget(float score) {
  118. gameLogic.HitTarget(score);
  119. HitTargetNumber.Create(score);
  120. scoreWillSend = score.ToString();
  121. }
  122. public override bool DoNextShoot() {
  123. return gameLogic.DoNextShoot(this);
  124. }
  125. public override object[] Settle() {
  126. return gameLogic.gameRes;
  127. }
  128. float uploadGameDataTime = 0;
  129. float uploadBowTime = 0;
  130. public override void Update() {
  131. if (!IsMyPlayerInited()) return;
  132. if (!hasStart) return;
  133. AutoSwitchBanUserControlBow();
  134. if (IsMyPlayerRunning()) {
  135. gameLogic.Update(this);
  136. upload1();
  137. }
  138. upload2();
  139. gameDisplay.Update(this);
  140. }
  141. Dictionary<int, ArrowSync> arrowSyncMap = new Dictionary<int, ArrowSync>();
  142. void upload1() {
  143. uploadBowTime -= Time.deltaTime;
  144. if (uploadBowTime <= 0) {
  145. uploadBowTime = 0.033f;
  146. Quaternion qua = BowCamera.ins.transform.rotation;
  147. int aimScaleValue = GameAssistUI.ins.aimScaleValue;
  148. if (!GameAssistUI.ins.scaleAimOn) aimScaleValue = 0;
  149. //socketPlayer.UploadPKGameData("bow", qua.x + "," + qua.y + "," + qua.z + "," + qua.w + "," + ArmBow.ins.phase + "," + aimScaleValue);
  150. }
  151. }
  152. string scoreWillSend = null;
  153. public string shootSpeedWillSend = null;
  154. void upload2() {
  155. uploadGameDataTime -= Time.deltaTime;
  156. if (uploadGameDataTime <= 0) {
  157. uploadGameDataTime = 0.033f;
  158. List<ArrowSync.SyncData> arrowSyncDataList = new List<ArrowSync.SyncData>();
  159. foreach (var item in Arrow.arrowSet) {
  160. if (item.outputSyncData == null || !item.outputSyncData.inited) continue;
  161. arrowSyncDataList.Add(item.outputSyncData);
  162. }
  163. if (scoreWillSend != null) {
  164. scoreWillSend = null;
  165. }
  166. if (shootSpeedWillSend != null) {
  167. shootSpeedWillSend = null;
  168. }
  169. }
  170. }
  171. public override void onBowReady() {
  172. if (!IsMyPlayerRunning()) return;
  173. gameLogic.singleShootReadyTime = gameLogic.singleShootReadyMaxTime;
  174. gameLogic.singleShootTimeRunning = true;
  175. }
  176. public override void onBowShoot() {
  177. if (!IsMyPlayerRunning()) return;
  178. gameLogic.singleShootTimeRunning = false;
  179. }
  180. //gamelogic-data
  181. public GameLogic gameLogic = new GameLogic();
  182. public class GameLogic {
  183. public int currentPlayerIndex = 0;
  184. public int[] totalScores = {0, 0};
  185. public float[] currentScores = {0, 0};
  186. public int round = 1;
  187. public int maxRound = 5;
  188. public int[] shootCount = {0, 0};
  189. public int maxShootCount = 3;
  190. public float singleShootReadyTime = 20;
  191. public float singleShootReadyMaxTime = 20;
  192. public bool singleShootTimeRunning = false;
  193. public string[] gameRes = {"平局", "平局"};
  194. public bool gameEnd = false;
  195. //玩家出场顺序
  196. public List<int> appearPlayerIndexes = new List<int>(); //这里不用Queue,是因为IL2CPP环境下json反序列化失败
  197. public int[] sequencePlayerIndexes = new int[]{0, 1};
  198. public int nextPlayerExcuteID = 0;
  199. public void InitAppearPlayerIndexes()
  200. {
  201. if (round >= 2)
  202. {
  203. if (totalScores[0] < totalScores[1])
  204. {
  205. sequencePlayerIndexes = new int[]{0, 1};
  206. }
  207. else if (totalScores[1] < totalScores[0])
  208. {
  209. sequencePlayerIndexes = new int[]{1, 0};
  210. }
  211. }
  212. for (int i = 0; i < maxShootCount; i++) {
  213. appearPlayerIndexes.Add(sequencePlayerIndexes[0]);
  214. appearPlayerIndexes.Add(sequencePlayerIndexes[1]);
  215. }
  216. }
  217. public void SetCurrentPlayerIndexToNext() {
  218. currentPlayerIndex = appearPlayerIndexes[0]; appearPlayerIndexes.RemoveAt(0);
  219. nextPlayerExcuteID++;
  220. }
  221. public void Update(PKGameMode_OnlinePK gm) {
  222. if (singleShootTimeRunning && !gm.pauseTimeCounting) {
  223. singleShootReadyTime -= Time.deltaTime;
  224. if (singleShootReadyTime <= 0) {
  225. singleShootReadyTime = 0;
  226. singleShootTimeRunning = false;
  227. HitTarget(0);
  228. // BanBowReady();
  229. // //超时显示
  230. // Text timeoutText = PKGameView.ins.transform.Find("TimeoutText").GetComponent<Text>();
  231. // Sequence seq = DOTween.Sequence();
  232. // seq.Append(timeoutText.DOFade(1, 0.5f));
  233. // seq.AppendInterval(1);
  234. // seq.Append(timeoutText.DOFade(0, 0.5f));
  235. // seq.AppendCallback(delegate(){
  236. // if (DoNextShoot()) {
  237. // UnbanBowReady();
  238. // }
  239. // });
  240. DoNextShoot(gm);
  241. }
  242. }
  243. }
  244. public void HitTarget(float score) {
  245. currentScores[currentPlayerIndex] += score;
  246. shootCount[currentPlayerIndex]++;
  247. }
  248. public bool DoNextShoot(PKGameMode_OnlinePK gm) {
  249. if (gm.gameMgr.gameOver) return false;
  250. bool nextRound = false;
  251. if (shootCount[0] == maxShootCount && shootCount[1] == maxShootCount ) {
  252. shootCount = new int[]{0, 0};
  253. nextRound = true;
  254. //更新总比分
  255. if (currentScores[0] == currentScores[1]) {
  256. totalScores[0] += 1;
  257. totalScores[1] += 1;
  258. } else if (currentScores[0] > currentScores[1]) {
  259. totalScores[0] += 2;
  260. } else if (currentScores[0] < currentScores[1]) {
  261. totalScores[1] += 2;
  262. }
  263. //根据总比分判断游戏是否结束
  264. if (totalScores[0] == totalScores[1]) {
  265. if (round == maxRound) {
  266. if (round == 5) {
  267. maxShootCount = 1;
  268. maxRound = 6;
  269. } else {
  270. gameEnd = true;
  271. gameRes = new string[]{"平局", "平局"};
  272. }
  273. }
  274. } else if (totalScores[0] >= 6 && totalScores[0] > totalScores[1]) {
  275. gameEnd = true;
  276. gameRes = new string[]{"胜利", "失败"};
  277. } else if (totalScores[1] >= 6 && totalScores[1] > totalScores[0]) {
  278. gameEnd = true;
  279. gameRes = new string[]{"失败", "胜利"};
  280. }
  281. }
  282. if (gameEnd) {
  283. return false;
  284. } else {
  285. //进入下一回合?
  286. if (nextRound) {
  287. round++;
  288. currentScores[0] = currentScores[1] = 0;
  289. InitAppearPlayerIndexes();
  290. }
  291. //本轮玩家登记
  292. SetCurrentPlayerIndexToNext();
  293. gm.BanBowReady();
  294. }
  295. return true;
  296. }
  297. }
  298. //game-display
  299. public GameDisplay gameDisplay = new GameDisplay();
  300. public class GameDisplay {
  301. readonly float[] targetDistancesOnRound = {10, 20, 30, 50, 70, 70};
  302. public int[] playerRoleIDs = {1, 2};
  303. public int showRoundValueOnReadyView = 0;
  304. #region 用于控制显示更新的变量
  305. int round = 0;
  306. int nextPlayerExcuteID = 0;
  307. bool gameEnd = false;
  308. #endregion
  309. public void Update(PKGameMode_OnlinePK gm) {
  310. if (round != gm.gameLogic.round) {
  311. round = gm.gameLogic.round;
  312. gm.targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  313. }
  314. if (nextPlayerExcuteID != gm.gameLogic.nextPlayerExcuteID) {
  315. nextPlayerExcuteID = gm.gameLogic.nextPlayerExcuteID;
  316. gm.BanBowReady();
  317. gm.AddReadyView();
  318. //清除箭矢
  319. foreach (var arrow in Arrow.arrowSet) {
  320. try {
  321. GameObject.Destroy(arrow.gameObject);
  322. } catch (UnityException e) {
  323. Debug.Log("Delete Arrow Error\n" + e.Message);
  324. }
  325. }
  326. }
  327. if (!gameEnd && gm.gameLogic.gameEnd) {
  328. gameEnd = true;
  329. gm.gameMgr.StopGame();
  330. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameSettleView");
  331. GameObject.Instantiate(view);
  332. }
  333. if (!gm.IsMyPlayerRunning() && gm.hasBowTargBtQua) {
  334. BowCamera.ins.transform.rotation = Quaternion.Lerp(BowCamera.ins.transform.rotation, gm.bowTargetQua, Time.deltaTime * 12);
  335. }
  336. }
  337. }
  338. }