PairingMode.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. // randomCount,
  40. onLoad() {
  41. this.init();
  42. //init web sdk
  43. if (lib.openInWebview()) {
  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. //随机一个开始时间
  57. let randomRange = [3, 6];
  58. this.randomCount = Math.random() * (randomRange[1] - randomRange[0]) + randomRange[0];
  59. console.log("随机一个开始时间",this.randomCount);
  60. this.schedule(this.countTime, interval, repeat, delay);
  61. // 因为在手机端加载场景要5-8秒钟 所以提早加载
  62. // because loading scene not a immediately stuff,A smart phone will cost 5-8 second for loading
  63. if (this.nextSceneName != '') {
  64. // cc.director.preloadScene(this.nextSceneName);
  65. cc.director.preloadScene(this.nextSceneName, function () {
  66. cc.log("Next scene preloaded");
  67. });
  68. }
  69. },
  70. init() {
  71. this.matchTime = 0;
  72. this.name1 = this.player1.getChildByName('Name');
  73. this.name2 = this.player2.getChildByName('Name');
  74. this.gender1 = this.player1.getChildByName('Gender');
  75. this.gender2 = this.player2.getChildByName('Gender');
  76. this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
  77. this.avatarSp2 = this.player2.getChildByName('Mask').getChildByName('AvatarSp');
  78. },
  79. countTime() {
  80. //update countdown time on UI
  81. this.timeTxt.getComponent(cc.Label).string = this.updateTime(this.matchTime);
  82. this.matchTime++;
  83. if (this.matchTime > 10) {
  84. // if (this.matchTime > this.randomCount) {
  85. this.unschedule(this.countTime);
  86. // 在app内Webview打开
  87. if (lib.openInWebview()) {
  88. mgobe.cancelPlayerMatch(function () {
  89. this.generateAI();
  90. }.bind(this));
  91. }
  92. else {
  93. this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
  94. }
  95. }
  96. },
  97. setGender(gender, bBoy) {
  98. this.iconBoy = gender.getChildByName('IconBoy');
  99. this.iconGirl = gender.getChildByName('IconGirl');
  100. if (!bBoy) {
  101. this.iconBoy.active = true;
  102. this.iconGirl.active = false;
  103. } else {
  104. this.iconBoy.active = false;
  105. this.iconGirl.active = true;
  106. }
  107. },
  108. initPlayer1() {
  109. this.name1.getComponent(cc.Label).string = webView.userName;
  110. this.setGender(this.gender1, webView.gender);
  111. this.loadAvatar(webView.avatarBase64, function (frame) {
  112. this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
  113. }.bind(this));
  114. },
  115. loadAvatar(avatarBase64, callback) {
  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. // bind the init success event
  129. mgobe.room.onRecvFromClient = this.onRecvFromClient.bind(this);
  130. mgobe.matchPlayers(function () {
  131. webView.bAi = false;
  132. //send my information to others so that they can generate rival
  133. mgobe.sendMessage(JSON.stringify({
  134. 'avatarUrl': webView.avatarUrl,
  135. 'userName': webView.userName,
  136. 'gender': webView.gender
  137. }));
  138. }.bind(this, lib));
  139. }.bind(this));
  140. });
  141. },
  142. updateTime(t) {
  143. let theTime = Math.floor(t);
  144. let theTime1 = 0;// 分
  145. if (theTime > 60) {
  146. theTime1 = parseInt(theTime / 60);
  147. theTime = parseInt(theTime % 60);
  148. }
  149. let result = '';
  150. result = (theTime1 < 10 ? "0" + theTime1 : theTime1) + ":" + (theTime < 10 ? "0" + theTime : theTime);
  151. return result;
  152. },
  153. resetRivalUIByData(userName, gender, avatarBase64) {
  154. this.name2.getComponent(cc.Label).string = userName;
  155. this.setGender(this.gender2, gender);
  156. // loading avatar
  157. //for mobile device we need to download avatar whatever AI or others
  158. this.loadAvatar(avatarBase64, function (frame) {
  159. this.avatarSp2.getComponent(cc.Sprite).spriteFrame = frame;
  160. cc.audioEngine.playEffect(this.successAudio, false);
  161. this.scheduleOnce(() => {
  162. this.matched();
  163. }, 2);
  164. }.bind(this));
  165. },
  166. matched() {
  167. //change top UI
  168. this.meteorLine.active = true;
  169. this.titleTxt.getComponent(cc.Label).string = "匹配成功";
  170. this.timeTxt.active = false;
  171. this.waitTxt.active = false;
  172. //show rival information
  173. this.player2.getChildByName("DotAnimation").active = false;
  174. this.player2.active = true;
  175. this.name2.active = true;
  176. this.gender2.active = true;
  177. this.avatarSp2.active = true;
  178. this.scheduleOnce(() => {
  179. if (this.nextSceneName != '') {
  180. // webView.unRegister(this.node);
  181. cc.director.loadScene(this.nextSceneName);
  182. }
  183. }, 1);
  184. },
  185. generateAI() {
  186. if (lib.openInWebview())// 在app内Webview打开
  187. {
  188. // match failed so we need to get a Ai information to start game
  189. webView.getAiInfo();
  190. } else {
  191. //TODO
  192. //In browser we just use default information to generate AI
  193. this.resetRivalUIByData(webView.userName, webView.gender, webView.avatarBase64);
  194. }
  195. },
  196. onRecvFromClient(event) {
  197. console.log("收到新消息" + event.data.msg);
  198. this.unschedule(this.countTime);
  199. let data = JSON.parse(event.data.msg);
  200. webView.rivalavatarUrl = data.avatarUrl;
  201. webView.rivalUserName = data.userName;
  202. webView.rivalGender = data.gender;
  203. if (lib.openInWebview()) {
  204. // 在app内Webview打开
  205. webView.getBase64(webView.rivalavatarUrl);
  206. }
  207. else {
  208. this.matched();
  209. }
  210. },
  211. onUrlToBase64(data) {
  212. webView.rivalavatarBase64 = data.base64;
  213. console.log('rivalavatarBase64=', webView.rivalavatarBase64);
  214. this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
  215. },
  216. onGameInit(data) {
  217. this.initPlayer1();
  218. },
  219. onAiRandomInfo(data) {
  220. let rivalavatarBase64 = webView.rivalavatarBase64;
  221. let rivalUserName = webView.rivalUserName;
  222. let rivalGender = webView.rivalGender;
  223. this.resetRivalUIByData(rivalUserName, rivalGender, rivalavatarBase64);
  224. },
  225. });