PairingMode.js 8.0 KB

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