PairingMode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. // playerArr:{
  14. // default: null,
  15. // type: Array<cc.Node>,
  16. // },
  17. player1: {
  18. default: null,
  19. type: cc.Node,
  20. },
  21. player2: {
  22. default: null,
  23. type: cc.Node,
  24. },
  25. player3: {
  26. default: null,
  27. type: cc.Node,
  28. },
  29. player4: {
  30. default: null,
  31. type: cc.Node,
  32. },
  33. titleTxt: {
  34. default: null,
  35. type: cc.Node,
  36. },
  37. timeTxt: {
  38. default: null,
  39. type: cc.Node,
  40. },
  41. waitTxt: {
  42. default: null,
  43. type: cc.Node,
  44. },
  45. successAudio: {
  46. default: null,
  47. type: cc.AudioClip,
  48. },
  49. meteorLine: {
  50. default: null,
  51. type: cc.Node,
  52. },
  53. matchEffectNode: {
  54. default: null,
  55. type: cc.Node,
  56. },
  57. },
  58. onLoad() {
  59. this.init();
  60. //init web sdk
  61. if (lib.openInWebview()) {
  62. // 在app内Webview打开
  63. webView.init(this.node);
  64. }
  65. //register event from webView
  66. this.node.on('onGameInit', this.onGameInit, this);
  67. this.node.on('onAiRandomInfo', this.onAiRandomInfo, this);
  68. this.node.on('onUrlToBase64', this.onUrlToBase64, this);
  69. this.starMatching();
  70. //count down time to match ui
  71. let interval = 1;  // 以秒为单位的时间间隔let
  72. let repeat = cc.macro.REPEAT_FOREVER;  // 重复次数
  73. let delay = 0;  // 开始延时
  74. this.schedule(this.countTime, interval, repeat, delay);
  75. // 因为在手机端加载场景要5-8秒钟 所以提早加载
  76. // because loading scene not a immediately stuff,A smart phone will cost 5-8 second for loading
  77. if (this.nextSceneName != '') {
  78. cc.director.preloadScene(this.nextSceneName);
  79. }
  80. },
  81. init() {
  82. this.aiTag = 0;
  83. this.matchTime = 0;
  84. this.name1 = this.player1.getChildByName('Name');
  85. // this.name2 = this.player2.getChildByName('Name');
  86. // this.name3 = this.player3.getChildByName('Name');
  87. // this.name4 = this.player4.getChildByName('Name');
  88. this.aiNameArr = [
  89. this.player2.getChildByName('Name'),
  90. this.player3.getChildByName('Name'),
  91. this.player4.getChildByName('Name')
  92. ]
  93. this.gender1 = this.player1.getChildByName('Gender');
  94. // this.gender2 = this.player2.getChildByName('Gender');
  95. // this.gender3 = this.player3.getChildByName('Gender');
  96. // this.gender4 = this.player4.getChildByName('Gender');
  97. this.aiGenderArr = [
  98. this.player2.getChildByName('Gender'),
  99. this.player3.getChildByName('Gender'),
  100. this.player4.getChildByName('Gender')
  101. ]
  102. this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
  103. // this.avatarSp2 = this.player2.getChildByName('Mask').getChildByName('AvatarSp');
  104. // this.avatarSp3 = this.player3.getChildByName('Mask').getChildByName('AvatarSp');
  105. // this.avatarSp4 = this.player4.getChildByName('Mask').getChildByName('AvatarSp');
  106. this.aiAvatarSpArr = [
  107. this.player2.getChildByName('Mask').getChildByName('AvatarSp'),
  108. this.player3.getChildByName('Mask').getChildByName('AvatarSp'),
  109. this.player4.getChildByName('Mask').getChildByName('AvatarSp')
  110. ];
  111. this.aiPlayerArr = [
  112. this.player2,
  113. this.player3,
  114. this.player4
  115. ]
  116. /**匹配最长时间 单位秒S */
  117. this.matchMaxTime = 5;
  118. for(let i = 0;i<this.aiPlayerArr.length;i++){
  119. this.aiPlayerArr[i].active = false;
  120. }
  121. },
  122. countTime() {
  123. //update countdown time on UI
  124. this.timeTxt.getComponent(cc.Label).string = this.updateTime(this.matchTime);
  125. this.matchTime++;
  126. if (this.matchTime > this.matchMaxTime) {
  127. this.unschedule(this.countTime);
  128. if (this.bOpenMgobeServer)//if using Mgobe
  129. {
  130. // 在app内Webview打开
  131. if (lib.openInWebview()) {
  132. mgobe.cancelPlayerMatch(function () {
  133. this.generateAI();
  134. }.bind(this));
  135. }
  136. else {
  137. this.generateAI();
  138. }
  139. }
  140. else {
  141. this.generateAI();
  142. }
  143. }
  144. },
  145. generateAI() {
  146. let time = 2;
  147. let random1 = Math.random()*time;
  148. let random2 = Math.random()*time+random1;
  149. let random3 = Math.random()*time+random2;
  150. let randomArr = [random1,random2,random3]
  151. for(let i = 0;i<3;i++){
  152. this.scheduleOnce(function() {
  153. if (lib.openInWebview())// 在app内Webview打开
  154. {
  155. // match failed so we need to get a Ai information to start game
  156. webView.getAiInfo();
  157. } else {
  158. //TODO
  159. //In browser we just use default information to generate AI
  160. this.resetRivalUIByData(webView.userName, webView.gender, webView.avatarBase64);
  161. }
  162. console.log("加载AI");
  163. },randomArr[i]);
  164. }
  165. // for (let i = 0; i < 3; i++) {
  166. // if (lib.openInWebview())// 在app内Webview打开
  167. // {
  168. // // match failed so we need to get a Ai information to start game
  169. // webView.getAiInfo();
  170. // } else {
  171. // //TODO
  172. // //In browser we just use default information to generate AI
  173. // this.resetRivalUIByData(webView.userName, webView.gender, webView.avatarBase64);
  174. // }
  175. // }
  176. },
  177. setGender(gender, bBoy) {
  178. this.iconBoy = gender.getChildByName('IconBoy');
  179. this.iconGirl = gender.getChildByName('IconGirl');
  180. if (!bBoy) {
  181. this.iconBoy.active = true;
  182. this.iconGirl.active = false;
  183. } else {
  184. this.iconBoy.active = false;
  185. this.iconGirl.active = true;
  186. }
  187. },
  188. initPlayer1() {
  189. this.name1.getComponent(cc.Label).string = webView.userName;
  190. this.setGender(this.gender1, webView.gender);
  191. this.loadAvatar(webView.avatarBase64, function (frame) {
  192. this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
  193. }.bind(this));
  194. },
  195. loadAvatar(avatarBase64, callback) {
  196. lib.setImageBase64(avatarBase64, function (texture2D) {
  197. let frame = new cc.SpriteFrame(texture2D);
  198. callback && callback(frame);
  199. });
  200. },
  201. starMatching() {
  202. if (!this.bOpenMgobeServer) return;
  203. //before matching we need to load a cert file in resource folder
  204. return cc.loader.loadRes("/cacert", cc.Asset, (err, asset) => {
  205. console.log("加载证书结束 " + (!err));
  206. if (err) return;
  207. mgobe.cacertNativeUrl = asset.nativeUrl;
  208. mgobe.initSDK(function () {
  209. // bind the init success event
  210. mgobe.room.onRecvFromClient = this.onRecvFromClient.bind(this);
  211. mgobe.matchPlayers(function () {
  212. //send my information to others so that they can generate rival
  213. mgobe.sendMessage(JSON.stringify({
  214. 'avatarUrl': webView.avatarUrl,
  215. 'userName': webView.userName,
  216. 'gender': webView.gender
  217. }));
  218. }.bind(this, lib));
  219. }.bind(this));
  220. });
  221. },
  222. updateTime(t) {
  223. let theTime = Math.floor(t);
  224. let theTime1 = 0;// 分
  225. if (theTime > 60) {
  226. theTime1 = parseInt(theTime / 60);
  227. theTime = parseInt(theTime % 60);
  228. }
  229. let result = '';
  230. result = (theTime1 < 10 ? "0" + theTime1 : theTime1) + ":" + (theTime < 10 ? "0" + theTime : theTime);
  231. return result;
  232. },
  233. /**把对手的UI信息设置一下 */
  234. resetRivalUIByData(userName, gender, avatarBase64) {
  235. // this.name2.getComponent(cc.Label).string = userName;
  236. // this.setGender(this.gender2, gender);
  237. this.aiNameArr[this.aiTag].getComponent(cc.Label).string = userName;
  238. this.setGender(this.aiGenderArr[this.aiTag], gender);
  239. // loading avatar
  240. if (!avatarBase64) {
  241. //TODO
  242. //browser can start game immediately
  243. // cc.audioEngine.playEffect(this.successAudio, false);
  244. // this.scheduleOnce(() => {
  245. // this.matched();
  246. // }, 2);
  247. this.aiPlayerArr[this.aiTag].active = true;
  248. }
  249. else {
  250. //for mobile device we need to download avatar whatever AI or others
  251. this.loadAvatar(avatarBase64, function (frame) {
  252. this.aiPlayerArr[this.aiTag].active = true;
  253. this.aiAvatarSpArr[this.aiTag].getComponent(cc.Sprite).spriteFrame = frame;
  254. // this.aiAvatarSpArr[this.aiTag].visible = true;
  255. // this.aiAvatarSpArr[this.aiTag].active = true;
  256. // console.log(
  257. // "正在设置第几个AI的信息"+this.aiTag+this.aiNameArr[this.aiTag]+userName
  258. // );
  259. // this.aiPlayerArr[this.aiTag].getChildByName("DotAnimation").active = false;
  260. // this.aiPlayerArr[this.aiTag].active = true;
  261. // this.aiNameArr[this.aiTag].active = true;
  262. // this.aiGenderArr[this.aiTag].active = true;
  263. // this.aiAvatarSpArr[this.aiTag].active = true;
  264. }.bind(this));
  265. }
  266. if (this.aiTag < 3) {
  267. this.aiTag++;
  268. }
  269. if (this.aiTag == 3) {
  270. this.matchEffectNode.active = false;
  271. cc.audioEngine.playEffect(this.successAudio, false);
  272. this.scheduleOnce(() => {
  273. this.matched();
  274. }, 1);
  275. }
  276. },
  277. matched() {
  278. //change top UI
  279. this.meteorLine.active = true;
  280. this.titleTxt.getComponent(cc.Label).string = "匹配成功";
  281. this.timeTxt.active = false;
  282. this.waitTxt.active = false;
  283. // for (let i = 0; i < 3; i++) {
  284. // this.aiPlayerArr[i].getChildByName("DotAnimation").active = false;
  285. // this.aiPlayerArr[i].active = true;
  286. // this.aiNameArr[i].active = true;
  287. // this.aiGenderArr[i].active = true;
  288. // this.aiAvatarSpArr[i].active = true;
  289. // }
  290. this.scheduleOnce(() => {
  291. if (this.nextSceneName != '') {
  292. cc.director.loadScene(this.nextSceneName);
  293. }
  294. }, 2);
  295. },
  296. onRecvFromClient(event) {
  297. console.log("收到新消息" + event.data.msg);
  298. this.unschedule(this.countTime);
  299. let data = JSON.parse(event.data.msg);
  300. webView.rivalavatarUrl = data.avatarUrl;
  301. webView.rivalUserName = data.userName;
  302. webView.rivalGender = data.gender;
  303. webView.getBase64(webView.rivalavatarUrl);
  304. },
  305. onUrlToBase64(data) {
  306. webView.rivalavatarBase64 = data.base64;
  307. console.log('rivalavatarBase64=', webView.rivalavatarBase64);
  308. this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
  309. },
  310. onGameInit(data) {
  311. this.initPlayer1();
  312. },
  313. // /**匹配AI标签,0,1,2 */
  314. // aiTag : 0,
  315. onAiRandomInfo(data) {
  316. console.log("ai匹配回调,,,,,,,,,,,,,", data.aiName);
  317. let rivalavatarBase64 = data.aiAvatarBase64Url;
  318. let rivalUserName = data.aiName;
  319. let rivalGender = data.aiGender;
  320. this.resetRivalUIByData(rivalUserName, rivalGender, rivalavatarBase64);
  321. },
  322. });