MatchView.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. const {ccclass, property} = cc._decorator;
  2. import lib = require("../BiBeng/Library");
  3. import webView = require("../BiBeng/WebView");
  4. import WebViewListener from "../BiBeng/WebViewListener";
  5. import Utils from "../Game/Utils";
  6. import PIFS from "../PIFS";
  7. @ccclass
  8. export default class MatchView extends cc.Component {
  9. @property(cc.Node)
  10. player1: cc.Node = null;
  11. @property(cc.Node)
  12. player2: cc.Node = null;
  13. @property(cc.Node)
  14. player3: cc.Node = null;
  15. @property(cc.Node)
  16. player4: cc.Node = null;
  17. @property(cc.Node)
  18. titleTxt: cc.Node = null;
  19. @property(cc.Node)
  20. timeTxt: cc.Node = null;
  21. @property(cc.Node)
  22. waitTxt: cc.Node = null;
  23. @property({type: cc.AudioClip})
  24. successAudio: cc.AudioClip = null;
  25. @property(cc.Node)
  26. meteorLine: cc.Node = null;
  27. @property(cc.Node)
  28. matchEffectNode: cc.Node = null;
  29. aiTag: number;
  30. matchTime: number;
  31. name1: cc.Node;
  32. aiNameArr: cc.Node[];
  33. gender1: cc.Node;
  34. aiGenderArr: cc.Node[];
  35. avatarSp1: cc.Node;
  36. aiAvatarSpArr: cc.Node[];
  37. aiPlayerArr: cc.Node[];
  38. quckTest: boolean = false;
  39. static Instance: MatchView;
  40. onLoad() {
  41. MatchView.Instance = this;
  42. // if (window.location.href.includes(":7456")) this.quckTest = true;
  43. if (this.quckTest) cc.director.getScheduler().setTimeScale(20);
  44. this.init();
  45. this.schedule(this.countTime, 1, cc.macro.REPEAT_FOREVER, 0);
  46. }
  47. protected start(): void {
  48. if (lib.openInWebview()) {
  49. webView.init(this.node);
  50. this.node.on('onGameInit', this.onGameInit, this);
  51. WebViewListener.Init();
  52. } else {
  53. webView.userName = PIFS.myPlayerInfo.nickname;
  54. webView.gender = PIFS.myPlayerInfo.gender;
  55. webView.avatarBase64 = PIFS.myPlayerInfo.avatarUrl;
  56. this.onGameInit();
  57. }
  58. }
  59. onDestroy() {
  60. if (MatchView.Instance == this) MatchView.Instance = null;
  61. if (this.quckTest) cc.director.getScheduler().setTimeScale(1);
  62. }
  63. init() {
  64. this.aiTag = 0;
  65. this.matchTime = 0;
  66. this.name1 = this.player1.getChildByName('Name');
  67. this.aiNameArr = [
  68. this.player2.getChildByName('Name'),
  69. this.player3.getChildByName('Name'),
  70. this.player4.getChildByName('Name')
  71. ]
  72. this.gender1 = this.player1.getChildByName('Gender');
  73. this.aiGenderArr = [
  74. this.player2.getChildByName('Gender'),
  75. this.player3.getChildByName('Gender'),
  76. this.player4.getChildByName('Gender')
  77. ]
  78. this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
  79. this.aiAvatarSpArr = [
  80. this.player2.getChildByName('Mask').getChildByName('AvatarSp'),
  81. this.player3.getChildByName('Mask').getChildByName('AvatarSp'),
  82. this.player4.getChildByName('Mask').getChildByName('AvatarSp')
  83. ];
  84. this.aiPlayerArr = [
  85. this.player2,
  86. this.player3,
  87. this.player4
  88. ]
  89. for(let i = 0;i<this.aiPlayerArr.length;i++){
  90. this.aiPlayerArr[i].active = false;
  91. }
  92. }
  93. countTime() {
  94. this.timeTxt.getComponent(cc.Label).string = Utils.FormatSecToMinSec(this.matchTime);
  95. this.matchTime++;
  96. }
  97. setGender(genderNode: cc.Node, gender) {
  98. let iconBoy = genderNode.getChildByName('IconBoy');
  99. let iconGirl = genderNode.getChildByName('IconGirl');
  100. if (gender == 1) {
  101. iconBoy.active = false;
  102. iconGirl.active = true;
  103. } else {
  104. iconBoy.active = true;
  105. iconGirl.active = false;
  106. }
  107. }
  108. matched() {
  109. this.meteorLine.active = true;
  110. this.titleTxt.getComponent(cc.Label).string = "匹配成功";
  111. this.timeTxt.active = false;
  112. this.waitTxt.active = false;
  113. this.matchEffectNode.active = false;
  114. cc.audioEngine.playEffect(this.successAudio, false);
  115. }
  116. close() {
  117. this.node.destroy()
  118. }
  119. static isGameInited: boolean = false;
  120. onGameInit() {
  121. console.log("MatchView-onGameInit")
  122. PIFS.myPlayerInfo.nickname = webView.userName;
  123. PIFS.myPlayerInfo.gender = webView.gender;
  124. PIFS.myPlayerInfo.avatarUrl = webView.avatarBase64;
  125. MatchView.isGameInited = true;
  126. this.renderMyPlayerInfo();
  127. window.gm.socketPlayer.requestEnterRoom();
  128. }
  129. renderMyPlayerInfo() {
  130. this.name1.getComponent(cc.Label).string = PIFS.myPlayerInfo.nickname;
  131. this.setGender(this.gender1, PIFS.myPlayerInfo.gender);
  132. Utils.LoadSpriteFrame(PIFS.myPlayerInfo.avatarUrl, (frame) => {
  133. this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
  134. });
  135. }
  136. renderOtherPlayerInfo(userName, gender, avatarBase64, delay) {
  137. this.scheduleOnce(() => {
  138. let aiTag = this.aiTag++;
  139. this.aiNameArr[aiTag].getComponent(cc.Label).string = userName;
  140. this.setGender(this.aiGenderArr[aiTag], gender);
  141. Utils.LoadSpriteFrame(avatarBase64, (frame) => {
  142. this.aiPlayerArr[aiTag].active = true;
  143. this.aiAvatarSpArr[aiTag].getComponent(cc.Sprite).spriteFrame = frame;
  144. });
  145. }, delay);
  146. }
  147. static handleMatchSuccess() {
  148. if (!MatchView.Instance) return;
  149. let delay = 0;
  150. for (let i = 0; i < PIFS.matchPlayerInfos.length; i++) {
  151. if (i !== window.gameSystem.masterId) {
  152. let e = PIFS.matchPlayerInfos[i];
  153. MatchView.Instance.renderOtherPlayerInfo(e.nickname, e.gender, e.avatarUrl, delay);
  154. delay += 0.3;
  155. }
  156. }
  157. MatchView.Instance.matched();
  158. }
  159. }