PairingMode.js 8.2 KB

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