PKComp.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class PKComp : Singleton<PKComp>
  4. {
  5. public void randomMatch() {
  6. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  7. UserPlayer.ins.call("pkComp.randomMatch", playerInfo, GlobalData.matchGameType);
  8. }
  9. public void cancelRandomMatch() {
  10. UserPlayer.ins.call("pkComp.cancelRandomMatch", LoginMgr.myUserInfo.id);
  11. }
  12. public void inviteFriendGamePK(int targetID, string inviterViewUUID) {
  13. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  14. UserPlayer.ins.call("pkComp.inviteFriendGamePK", playerInfo, GlobalData.matchGameType, targetID, inviterViewUUID);
  15. }
  16. public void acceptFriendGamePK(string roomKey) {
  17. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  18. UserPlayer.ins.call("pkComp.acceptFriendGamePK", playerInfo, roomKey);
  19. }
  20. public void inviteOtherTryAgainGamePK(string inviterViewUUID) {
  21. //get other info
  22. int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
  23. MatchPlayerInfo ohterInfo = GlobalData.matchPlayerInfos[otherIndex];
  24. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  25. UserPlayer.ins.call("pkComp.inviteOtherTryAgainGamePK", playerInfo, GlobalData.matchGameType, ohterInfo.playerID, inviterViewUUID);
  26. }
  27. public void rejectPKInvite(string roomKey) {
  28. UserPlayer.ins.call("pkComp.rejectPKInvite", roomKey);
  29. }
  30. public void acceptOtherTryAgainGamePK(string roomKey) {
  31. MatchPlayerInfo playerInfo = CreateMyMatchPlayerInfo();
  32. UserPlayer.ins.call("pkComp.acceptOtherTryAgainGamePK", playerInfo, roomKey);
  33. }
  34. private MatchPlayerInfo CreateMyMatchPlayerInfo() {
  35. UserInfo u = LoginMgr.myUserInfo;
  36. MatchPlayerInfo playerInfo = new MatchPlayerInfo(u.id, u.avatarID, u.nickname);
  37. return playerInfo;
  38. }
  39. //被服务端调用的接口
  40. public void onGamePKMatchSuccess(List<MatchPlayerInfo> playerInfoList, int playerIndexInRoom, string roomKey) {
  41. GlobalData.roomKey = roomKey;
  42. GlobalData.matchPlayerInfos = playerInfoList;
  43. GlobalData.playerIndexInRoom = playerIndexInRoom;
  44. if (PKMatchingView.ins) {
  45. PKMatchingView.ins.EnterGameSceneOnMatchSuccess();
  46. }
  47. }
  48. public void onInviteFriendGamePK(MatchPlayerInfo matchPlayerInfo, int gameType, string roomKey) {
  49. string pkTypeName = GetPKTypeName(gameType);
  50. if (pkTypeName == null) return;
  51. string tip = $"好友邀请你参加{pkTypeName}PK";
  52. System.Action cb = delegate() {
  53. if (CanShowFriendInviteOrMatch()) {
  54. PKMatchingView view = PKMatchingView.Create();
  55. view.isFriendPKInvitee = true;
  56. view.targetJoinRoomKey = roomKey;
  57. GlobalData.pkMatchType = PKMatchType.OnlinePK;
  58. GlobalData.matchGameType = gameType;
  59. } else if (PKMatchingView.ins) {
  60. PopupMgr.ins.ShowTip("当前正处于匹配状态,无法接受好友PK邀请!");
  61. }
  62. };
  63. System.Action cbCancel = delegate() {
  64. rejectPKInvite(roomKey);
  65. };
  66. if (CanShowFriendInviteOrMatch()) {
  67. PopupMgr.ins.ShowPKInviteNotice(matchPlayerInfo.avatarID, matchPlayerInfo.nickname, tip, cb, cbCancel, cbCancel);
  68. }
  69. }
  70. public void onInviteOtherTryAgainGamePK(MatchPlayerInfo matchPlayerInfo, int gameType, string roomKey) {
  71. string pkTypeName = GetPKTypeName(gameType);
  72. if (pkTypeName == null) return;
  73. string tip = $"对方邀请你再来一次{pkTypeName}PK";
  74. System.Action cb = delegate() {
  75. if (CanShowTryAgainInviteOrMatch(matchPlayerInfo)) {
  76. PKMatchingView view = PKMatchingView.Create();
  77. view.isTryAgainInvitee = true;
  78. view.targetJoinTryAgainRoomKey = roomKey;
  79. GlobalData.pkMatchType = PKMatchType.OnlinePK;
  80. GlobalData.matchGameType = gameType;
  81. } else {
  82. PopupMgr.ins.ShowTip("目前状态下,无法接受该邀请!");
  83. }
  84. };
  85. System.Action cbCancel = delegate() {
  86. rejectPKInvite(roomKey);
  87. };
  88. if (CanShowTryAgainInviteOrMatch(matchPlayerInfo)) {
  89. PopupMgr.ins.ShowPKInviteNotice(matchPlayerInfo.avatarID, matchPlayerInfo.nickname, tip, cb, cbCancel, cbCancel);
  90. }
  91. }
  92. public void onRejectPKInvite(string inviterViewUUID) {
  93. PopupMgr.ins.ShowTip("对方拒绝了你的邀请");
  94. PKMatchingView view = PKMatchingView.ins;
  95. if (!view) return;
  96. if (view.viewUUID != inviterViewUUID) return;
  97. view.eventOnRejectPKInvite?.Invoke();
  98. }
  99. //内部方法
  100. private string GetPKTypeName(int gameType) {
  101. string pkTypeName = null;
  102. switch (gameType) {
  103. case 9:
  104. pkTypeName = "静止靶";
  105. break;
  106. case 10:
  107. pkTypeName = "野兔闯关";
  108. break;
  109. case 11:
  110. pkTypeName = "野鸡闯关";
  111. break;
  112. case 12:
  113. pkTypeName = "野狼闯关";
  114. break;
  115. }
  116. return pkTypeName;
  117. }
  118. private bool CanShowFriendInviteOrMatch() {
  119. return !PKMatchingView.ins && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home";
  120. }
  121. private bool CanShowTryAgainInviteOrMatch(MatchPlayerInfo inviterInfo) {
  122. bool condition1 = !PKMatchingView.ins && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home";
  123. bool condition2 = false;
  124. if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
  125. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game")) {
  126. foreach (var item in GlobalData.matchPlayerInfos) {
  127. if (inviterInfo.playerID == item.playerID) {
  128. condition2 = true;
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. return condition1 || condition2;
  135. }
  136. }