PairingMode.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var webView = require("../WebView");
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. bOpenMgobeServer: {
  6. default: false,
  7. },
  8. player1: {
  9. default: null,
  10. type: cc.Node,
  11. },
  12. player2: {
  13. default: null,
  14. type: cc.Node,
  15. },
  16. titleTxt: {
  17. default: null,
  18. type: cc.Node,
  19. },
  20. timeTxt: {
  21. default: null,
  22. type: cc.Node,
  23. },
  24. waitTxt: {
  25. default: null,
  26. type: cc.Node,
  27. },
  28. successAudio: {
  29. default: null,
  30. type: cc.AudioClip,
  31. },
  32. meteorLine: {
  33. default: null,
  34. type: cc.Node,
  35. },
  36. },
  37. onLoad() {
  38. this.init();
  39. //init web sdk
  40. if (this.openInWebview())
  41. {
  42. // 在app内Webview打开
  43. webView.init(this.node);
  44. }
  45. else
  46. {
  47. this.initPlayer1();
  48. }
  49. //register event from webView 监听web服务器的回调
  50. this.node.on('onGameInit',this.onGameInit,this);
  51. this.node.on('onAiRandomInfo',this.onAiRandomInfo,this);
  52. this.node.on('onUrlToBase64',this.onUrlToBase64,this);
  53. //count down time to match ui 计时
  54. let interval = 1;  // 以秒为单位的时间间隔let
  55. let repeat = cc.macro.REPEAT_FOREVER;  // 重复次数
  56. let delay = 0;  // 开始延时
  57. this.schedule(this.countTime, interval, repeat, delay);
  58. },
  59. init() {
  60. this.matchTime = 0;
  61. this.name1 = this.player1.getChildByName('Name');
  62. this.name2 = this.player2.getChildByName('Name');
  63. this.gender1 = this.player1.getChildByName('Gender');
  64. this.gender2 = this.player2.getChildByName('Gender');
  65. this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
  66. this.avatarSp2 = this.player2.getChildByName('Mask').getChildByName('AvatarSp');
  67. },
  68. countTime() {
  69. //update countdown time on UI
  70. this.timeTxt.getComponent(cc.Label).string = this.updateTime(this.matchTime);
  71. this.matchTime++;
  72. if (this.matchTime > 5)
  73. {
  74. this.unschedule(this.countTime);
  75. if(this.openInWebview())
  76. {
  77. webView.getAiInfo();
  78. }
  79. else
  80. {
  81. this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
  82. }
  83. }
  84. },
  85. //设置性别
  86. setGender(gender, bBoy) {
  87. this.iconBoy = gender.getChildByName('IconBoy');
  88. this.iconGirl = gender.getChildByName('IconGirl');
  89. if (!bBoy) {
  90. this.iconBoy.active = true;
  91. this.iconGirl.active = false;
  92. } else {
  93. this.iconBoy.active = false;
  94. this.iconGirl.active = true;
  95. }
  96. },
  97. //初始化自己的头像 等信息
  98. initPlayer1() {
  99. this.name1.getComponent(cc.Label).string = webView.userName;
  100. this.setGender(this.gender1, webView.gender);
  101. this.loadAvatar(webView.avatarBase64,function (frame) {
  102. this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
  103. }.bind(this));
  104. },
  105. //加载头像
  106. loadAvatar(avatarBase64,callback)
  107. {
  108. this.setImageBase64(avatarBase64,function (texture2D) {
  109. let frame = new cc.SpriteFrame(texture2D);
  110. callback && callback(frame);
  111. });
  112. },
  113. //刷新计时器
  114. updateTime(t) {
  115. let theTime = Math.floor(t);
  116. let theTime1 = 0;// 分
  117. if (theTime > 60)
  118. {
  119. theTime1 = parseInt(theTime / 60);
  120. theTime = parseInt(theTime % 60);
  121. }
  122. let result = '';
  123. result = (theTime1 < 10 ? "0" + theTime1 : theTime1) + ":" + (theTime < 10 ? "0" + theTime : theTime);
  124. return result;
  125. },
  126. resetRivalUIByData(userName, gender, avatarBase64) {
  127. this.name2.getComponent(cc.Label).string = userName;
  128. this.setGender(this.gender2, gender);
  129. // loading avatar
  130. //for mobile device we need to download avatar whatever AI or others
  131. this.loadAvatar(avatarBase64, function (frame) {
  132. this.avatarSp2.getComponent(cc.Sprite).spriteFrame = frame;
  133. cc.audioEngine.playEffect(this.successAudio, false);
  134. this.scheduleOnce(() => {
  135. this.matched();
  136. }, 2);
  137. }.bind(this));
  138. },
  139. matched() {
  140. //change top UI
  141. this.meteorLine.active = true;
  142. this.titleTxt.getComponent(cc.Label).string = "匹配成功";
  143. this.timeTxt.active = false;
  144. this.waitTxt.active = false;
  145. //show rival information
  146. this.player2.getChildByName("DotAnimation").active = false;
  147. this.player2.active = true;
  148. this.name2.active = true;
  149. this.gender2.active = true;
  150. this.avatarSp2.active = true;
  151. },
  152. //tool funtion
  153. //判断是在一般浏览器还是在webview里面运行
  154. openInWebview () {
  155. let ua = navigator.userAgent.toLowerCase()
  156. if (ua.match(/MicroMessenger/i) == 'micromessenger') { // 微信浏览器判断
  157. return false
  158. } else if (ua.match(/QQ/i) == 'qq') { // QQ浏览器判断
  159. return false
  160. } else if (ua.match(/WeiBo/i) == "weibo") {
  161. return false
  162. } else {
  163. if (ua.match(/Android/i) != null) {
  164. return ua.match(/browser/i) == null
  165. } else if (ua.match(/iPhone/i) != null) {
  166. return ua.match(/safari/i) == null
  167. } else {
  168. return (ua.match(/macintosh/i) == null && ua.match(/windows/i) == null)
  169. }
  170. }
  171. },
  172. //base64转texture
  173. setImageBase64(base64,callback){
  174. let img = new Image();
  175. img.src = base64;
  176. img.onload = function(){
  177. let texture = new cc.Texture2D();
  178. texture.initWithElement(img);
  179. texture.handleLoadedTexture();
  180. if (callback)
  181. callback(texture);
  182. }
  183. },
  184. //call back
  185. onGameInit(data) {
  186. this.initPlayer1();
  187. },
  188. onAiRandomInfo(data) {
  189. this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
  190. },
  191. onUrlToBase64(data)
  192. {
  193. webView.rivalavatarBase64 = data.base64;
  194. this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
  195. }
  196. });