PairingMode.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. let webView = require("../WebView");
  2. let lib = require("../Library");
  3. let mgobe = require("../Mgobe");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. nextSceneName: {
  8. default: '',
  9. },
  10. bOpenMgobeServer: {
  11. default: false,
  12. },
  13. player1: {
  14. default: null,
  15. type: cc.Node,
  16. },
  17. player2: {
  18. default: null,
  19. type: cc.Node,
  20. },
  21. titleTxt: {
  22. default: null,
  23. type: cc.Node,
  24. },
  25. timeTxt: {
  26. default: null,
  27. type: cc.Node,
  28. },
  29. waitTxt: {
  30. default: null,
  31. type: cc.Node,
  32. },
  33. successAudio: {
  34. default: null,
  35. type: cc.AudioClip,
  36. },
  37. meteorLine: {
  38. default: null,
  39. type: cc.Node,
  40. },
  41. },
  42. onLoad() {
  43. let self = this;
  44. this.init();
  45. //init web sdk
  46. if (lib.openInWebview())
  47. {
  48. // 在app内Webview打开
  49. webView.init(this.node,function () {
  50. self.starMatching();
  51. });
  52. }
  53. //register event from webView
  54. this.node.on('onGameInit',this.onGameInit,this);
  55. this.node.on('onAiRandomInfo',this.onAiRandomInfo,this);
  56. this.node.on('onUrlToBase64',this.onUrlToBase64,this);
  57. //count down time to match ui
  58. let interval = 1;  // 以秒为单位的时间间隔let
  59. let repeat = cc.macro.REPEAT_FOREVER;  // 重复次数
  60. let delay = 0;  // 开始延时
  61. this.schedule(this.countTime, interval, repeat, delay);
  62. // 因为在手机端加载场景要5-8秒钟 所以提早加载
  63. // because loading scene not a immediately stuff,A smart phone will cost 5-8 second for loading
  64. if(this.nextSceneName!='')
  65. {
  66. cc.director.preloadScene(this.nextSceneName);
  67. }
  68. },
  69. init() {
  70. this.matchTime = 0;
  71. webView.avatarSpriteFrame = this.player1.getChildByName('Mask').getChildByName('AvatarSp').getComponent(cc.Sprite).spriteFrame;
  72. webView.rivalAvatarSpriteFrame = this.player2.getChildByName('Mask').getChildByName('AvatarSp').getComponent(cc.Sprite).spriteFrame;
  73. this.name1 = this.player1.getChildByName('Name');
  74. this.name2 = this.player2.getChildByName('Name');
  75. webView.userName = this.name1.getComponent(cc.Label).string;
  76. webView.rivalUserName = this.name2.getComponent(cc.Label).string;
  77. this.gender1 = this.player1.getChildByName('Gender');
  78. this.gender2 = this.player2.getChildByName('Gender');
  79. this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
  80. this.avatarSp2 = this.player2.getChildByName('Mask').getChildByName('AvatarSp');
  81. },
  82. countTime() {
  83. //update countdown time on UI
  84. this.timeTxt.getComponent(cc.Label).string = this.updateTime(this.matchTime);
  85. this.matchTime++;
  86. if (this.matchTime > 5)
  87. {
  88. this.unschedule(this.countTime);
  89. if(this.bOpenMgobeServer)//if using Mgobe
  90. {
  91. // 在app内Webview打开
  92. if(lib.openInWebview())
  93. {
  94. mgobe.cancelPlayerMatch(function () {
  95. this.generateAI();
  96. }.bind(this));
  97. }
  98. else {
  99. this.quickStart();
  100. }
  101. }
  102. else
  103. {
  104. this.quickStart();
  105. }
  106. }
  107. },
  108. setGender(gender, bBoy) {
  109. this.iconBoy = gender.getChildByName('IconBoy');
  110. this.iconGirl = gender.getChildByName('IconGirl');
  111. if (!bBoy) {
  112. this.iconBoy.active = true;
  113. this.iconGirl.active = false;
  114. } else {
  115. this.iconBoy.active = false;
  116. this.iconGirl.active = true;
  117. }
  118. },
  119. initPlayer1() {
  120. this.name1.getComponent(cc.Label).string = webView.userName;
  121. this.setGender(this.gender1, webView.gender);
  122. this.loadAvatar(webView.avatarBase64,function (frame) {
  123. this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
  124. webView.avatarSpriteFrame = frame;
  125. }.bind(this));
  126. },
  127. loadAvatar(avatarBase64,callback)
  128. {
  129. lib.setImageBase64(avatarBase64,function (texture2D) {
  130. let frame = new cc.SpriteFrame(texture2D);
  131. callback && callback(frame);
  132. });
  133. },
  134. starMatching() {
  135. if(!this.bOpenMgobeServer) return;
  136. //before matching we need to load a cert file in resource folder
  137. return cc.loader.loadRes("/cacert", cc.Asset, (err, asset) => {
  138. console.log("加载证书结束 " + (!err));
  139. if (err)return;
  140. mgobe.cacertNativeUrl = asset.nativeUrl;
  141. mgobe.initSDK(function ()
  142. {
  143. // bind the init success event
  144. mgobe.room.onRecvFromClient = this.onRecvFromClient.bind(this);
  145. mgobe.matchPlayers(function ()
  146. {
  147. //send my information to others so that they can generate rival
  148. mgobe.sendMessage(JSON.stringify({
  149. 'avatarUrl': webView.avatarUrl,
  150. 'userName': webView.userName,
  151. 'gender': webView.gender
  152. }));
  153. }.bind(this,lib));
  154. }.bind(this));
  155. });
  156. },
  157. updateTime(t) {
  158. let theTime = Math.floor(t);
  159. let theTime1 = 0;// 分
  160. if (theTime > 60)
  161. {
  162. theTime1 = parseInt(theTime / 60);
  163. theTime = parseInt(theTime % 60);
  164. }
  165. let result = '';
  166. result = (theTime1 < 10 ? "0" + theTime1 : theTime1) + ":" + (theTime < 10 ? "0" + theTime : theTime);
  167. return result;
  168. },
  169. resetRivalUIByData(userName, gender, avatarBase64) {
  170. this.name2.getComponent(cc.Label).string = userName;
  171. this.setGender(this.gender2, gender);
  172. // loading avatar
  173. //for mobile device we need to download avatar whatever AI or others
  174. this.loadAvatar(avatarBase64, function (frame) {
  175. this.avatarSp2.getComponent(cc.Sprite).spriteFrame = frame;
  176. webView.rivalavatarBase64 = frame;
  177. cc.audioEngine.playEffect(this.successAudio, false);
  178. this.scheduleOnce(() => {
  179. this.matched();
  180. }, 2);
  181. }.bind(this));
  182. },
  183. matched() {
  184. //change top UI
  185. this.meteorLine.active = true;
  186. this.titleTxt.getComponent(cc.Label).string = "匹配成功";
  187. this.timeTxt.active = false;
  188. this.waitTxt.active = false;
  189. //show rival information
  190. this.player2.getChildByName("DotAnimation").active = false;
  191. this.player2.active = true;
  192. this.name2.active = true;
  193. this.gender2.active = true;
  194. this.avatarSp2.active = true;
  195. this.scheduleOnce(() => {
  196. if(this.nextSceneName!='')
  197. {
  198. cc.director.loadScene(this.nextSceneName);
  199. }
  200. }, 1);
  201. },
  202. generateAI() {
  203. if (lib.openInWebview())// 在app内Webview打开
  204. {
  205. // match failed so we need to get a Ai information to start game
  206. webView.getAiInfo();
  207. } else {
  208. //TODO
  209. //In browser we just use default information to generate AI
  210. this.resetRivalUIByData(webView.userName, webView.gender, webView.avatarBase64);
  211. }
  212. },
  213. quickStart()
  214. {
  215. cc.audioEngine.playEffect(this.successAudio, false);
  216. this.scheduleOnce(() => {
  217. this.matched();
  218. }, 2);
  219. },
  220. onRecvFromClient(event) {
  221. console.log("收到新消息" + event.data.msg);
  222. this.unschedule(this.countTime);
  223. let data = JSON.parse(event.data.msg);
  224. webView.rivalavatarUrl = data.avatarUrl;
  225. webView.rivalUserName=data.userName;
  226. webView.rivalGender = data.gender;
  227. webView.getBase64(webView.rivalavatarUrl);
  228. },
  229. onUrlToBase64(data)
  230. {
  231. webView.rivalavatarBase64 = data.base64;
  232. this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
  233. },
  234. onGameInit(data) {
  235. this.initPlayer1();
  236. },
  237. onAiRandomInfo(data) {
  238. this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
  239. },
  240. });