Match.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. var webView = require("../WebView");
  2. var lib = require("../Library");
  3. var server = require("../MiniGameServer");
  4. var globalConfig = require("../Global");
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. nextSceneName: {
  9. default: '',
  10. },
  11. player1: {
  12. default: null,
  13. type: cc.Node,
  14. },
  15. player2: {
  16. default: null,
  17. type: cc.Node,
  18. },
  19. titleTxt: {
  20. default: null,
  21. type: cc.Node,
  22. },
  23. timeTxt: {
  24. default: null,
  25. type: cc.Node,
  26. },
  27. waitTxt: {
  28. default: null,
  29. type: cc.Node,
  30. },
  31. successAudio: {
  32. default: null,
  33. type: cc.AudioClip,
  34. },
  35. meteorLine: {
  36. default: null,
  37. type: cc.Node,
  38. },
  39. },
  40. onLoad() {
  41. this.init();
  42. //从服务器下载获取当前玩家信息 get player info from webserver
  43. if (lib.openInWebview()) {
  44. //运行在手机浏览器
  45. this.getPlayerInfoFromWebServer();
  46. }
  47. else{
  48. //开始匹配
  49. this.startMatching();
  50. }
  51. //count down time to match ui 开启计时,时间到没有匹配到玩家就生成Ai开始游戏
  52. let interval = 1; // 以秒为单位的时间间隔let
  53. let repeat = cc.macro.REPEAT_FOREVER; // 重复次数
  54. let delay = 0; // 开始延时
  55. this.schedule(this.countTime, interval, repeat, delay);
  56. // 预加载下一个游戏场景
  57. // because loading scene not a immediately stuff,A smart phone will cost 5-8 second for loading
  58. if(this.nextSceneName!='')
  59. {
  60. cc.director.preloadScene(this.nextSceneName, function () {
  61. cc.log("Next scene preloaded");
  62. });
  63. }
  64. },
  65. init() {
  66. this.matchTime = 0;
  67. this.name1 = this.player1.getChildByName('Name');
  68. this.name2 = this.player2.getChildByName('Name');
  69. this.gender1 = this.player1.getChildByName('Gender');
  70. this.gender2 = this.player2.getChildByName('Gender');
  71. this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
  72. this.avatarSp2 = this.player2.getChildByName('Mask').getChildByName('AvatarSp');
  73. },
  74. getPlayerInfoFromWebServer(){
  75. //init web sdk
  76. // 在app内Webview打开
  77. webView.register(this.node);
  78. this.node.on('onGameInit',this.onGameInit,this);
  79. this.node.on('onAiRandomInfo',this.onAiRandomInfo,this);
  80. this.node.on('onUrlToBase64',this.onUrlToBase64,this);
  81. },
  82. countTime() {
  83. //update countdown time on UI
  84. this.timeTxt.getComponent(cc.Label).string = this.updateTime(this.matchTime);
  85. this.matchTime++;
  86. if (this.matchTime > 20)
  87. {
  88. this.unschedule(this.countTime);
  89. this.generateAI();
  90. }
  91. },
  92. updateTime(t) {
  93. let theTime = Math.floor(t);
  94. let theTime1 = 0;// 分
  95. if (theTime > 60)
  96. {
  97. theTime1 = parseInt(theTime / 60);
  98. theTime = parseInt(theTime % 60);
  99. }
  100. let result = (theTime1 < 10 ? "0" + theTime1 : theTime1) + ":" + (theTime < 10 ? "0" + theTime : theTime);
  101. return result;
  102. },
  103. startMatching() {
  104. let Self = this;
  105. globalConfig.openid = new Date().getMilliseconds().toString();
  106. server.login(globalConfig.openid,globalConfig.name,globalConfig.avatarUrl,globalConfig.gender,function(){
  107. server.match(globalConfig.openid,'lockStep',function (roomId,other_openid,other_name,other_avatarUrl,other_gender) {
  108. Self.matched(roomId,other_openid,other_name,other_avatarUrl,other_gender,false);
  109. });
  110. });
  111. },
  112. matched(roomId,other_openid,other_name,other_avatarUrl,other_gender,bAi) {
  113. //停止计时 stop counting down time
  114. this.unschedule(this.countTime);
  115. // cancel ai
  116. globalConfig.bAi = bAi;
  117. //保存对手的信息 save rival info
  118. globalConfig.roomId = roomId;
  119. globalConfig.rivalOpenid = other_openid;
  120. globalConfig.rivalAvatarUrl = other_avatarUrl;
  121. globalConfig.rivalName = other_name;
  122. globalConfig.rivalGender = other_gender;
  123. //change top UI
  124. this.meteorLine.active = true;
  125. this.titleTxt.getComponent(cc.Label).string = "匹配成功";
  126. this.timeTxt.active = false;
  127. this.waitTxt.active = false;
  128. //show rival information
  129. this.player2.getChildByName("DotAnimation").active = false;
  130. this.player2.active = true;
  131. this.name2.active = true;
  132. this.gender2.active = true;
  133. this.avatarSp2.active = true;
  134. //渠道下一个场景 go to next scene
  135. if(this.nextSceneName!='')
  136. {
  137. cc.director.loadScene(this.nextSceneName);
  138. }
  139. },
  140. setGender(gender, bBoy) {
  141. this.iconBoy = gender.getChildByName('IconBoy');
  142. this.iconGirl = gender.getChildByName('IconGirl');
  143. if (!bBoy) {
  144. this.iconBoy.active = true;
  145. this.iconGirl.active = false;
  146. } else {
  147. this.iconBoy.active = false;
  148. this.iconGirl.active = true;
  149. }
  150. },
  151. loadAvatar(avatarBase64,callback)
  152. {
  153. lib.setImageBase64(avatarBase64,function (texture2D) {
  154. let frame = new cc.SpriteFrame(texture2D);
  155. callback && callback(frame);
  156. });
  157. },
  158. resetRivalUIByData(userName, gender, avatarBase64) {
  159. this.name2.getComponent(cc.Label).string = userName;
  160. this.setGender(this.gender2, gender);
  161. // loading avatar
  162. //for mobile device we need to download avatar whatever AI or others
  163. this.loadAvatar(avatarBase64, function (frame) {
  164. this.avatarSp2.getComponent(cc.Sprite).spriteFrame = frame;
  165. cc.audioEngine.playEffect(this.successAudio, false);
  166. this.scheduleOnce(() => {
  167. this.matched(globalConfig.rivalAvatarUrl,globalConfig.rivalName,globalConfig.rivalGender,true);
  168. }, 2);
  169. }.bind(this));
  170. },
  171. generateAI() {
  172. if (lib.openInWebview())// 在app内Webview打开
  173. {
  174. // match failed so we need to get a Ai information to start game
  175. webView.getAiInfo();
  176. } else {
  177. // TODO
  178. // In browser we just use default information to generate AI
  179. this.resetRivalUIByData(globalConfig.name, globalConfig.gender, globalConfig.avatarBase64);
  180. }
  181. },
  182. onUrlToBase64(data)
  183. {
  184. globalConfig.rivalavatarBase64 = data.base64;
  185. this.resetRivalUIByData(globalConfig.rivalName, globalConfig.rivalGender, globalConfig.rivalAvatarBase64);
  186. },
  187. onGameInit(data) {
  188. globalConfig.name = data.userName;
  189. globalConfig.avatarUrl = data.avatarUrl;
  190. globalConfig.avatarBase64 = data.avatarBase64Url;
  191. globalConfig.gender = data.gender;
  192. globalConfig.calorieParams = data.calorieParams;
  193. globalConfig.device = data.device;
  194. this.initPlayerUI(globalConfig.name, globalConfig.gender,globalConfig.avatarBase64);
  195. //开始匹配
  196. this.startMatching();
  197. },
  198. initPlayerUI(name,gender,avatarBase64) {
  199. this.name1.getComponent(cc.Label).string = name;
  200. this.setGender(this.gender1, gender);
  201. this.loadAvatar(avatarBase64,function (frame) {
  202. this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
  203. }.bind(this));
  204. },
  205. onAiRandomInfo(data) {
  206. globalConfig.rivalAvatarBase64 = data.aiAvatarBase64Url;
  207. globalConfig.rivalName = data.aiName;
  208. globalConfig.rivalGender = data.aiGender;
  209. this.resetRivalUIByData(globalConfig.rivalName, globalConfig.rivalGender, globalConfig.rivalAvatarBase64);
  210. },
  211. });