Match.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. var webView = require("../WebView");
  2. var lib = require("../Library");
  3. var lockStepClient = require("../LockStepClient");
  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. this.getPlayerInfoFromWebServer();
  45. }
  46. //count down time to match ui 开启计时,时间到没有匹配到玩家就生成Ai开始游戏
  47. let interval = 1; // 以秒为单位的时间间隔let
  48. let repeat = cc.macro.REPEAT_FOREVER; // 重复次数
  49. let delay = 0; // 开始延时
  50. this.schedule(this.countTime, interval, repeat, delay);
  51. //开始匹配
  52. this.startMatching();
  53. // 预加载下一个游戏场景
  54. // because loading scene not a immediately stuff,A smart phone will cost 5-8 second for loading
  55. if(this.nextSceneName!='')
  56. {
  57. cc.director.preloadScene(this.nextSceneName, function () {
  58. cc.log("Next scene preloaded");
  59. });
  60. }
  61. },
  62. init() {
  63. this.matchTime = 0;
  64. this.name1 = this.player1.getChildByName('Name');
  65. this.name2 = this.player2.getChildByName('Name');
  66. this.gender1 = this.player1.getChildByName('Gender');
  67. this.gender2 = this.player2.getChildByName('Gender');
  68. this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
  69. this.avatarSp2 = this.player2.getChildByName('Mask').getChildByName('AvatarSp');
  70. },
  71. getPlayerInfoFromWebServer(){
  72. //init web sdk
  73. if (lib.openInWebview())
  74. {
  75. // 在app内Webview打开
  76. webView.register(this.node);
  77. this.node.on('onGameInit',this.onGameInit,this);
  78. this.node.on('onAiRandomInfo',this.onAiRandomInfo,this);
  79. this.node.on('onUrlToBase64',this.onUrlToBase64,this);
  80. }
  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 > 10)
  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. lockStepClient.login(globalConfig.openid,this.name,this.avatarUrl,this.gender,function(e){
  107. lockStepClient.match(globalConfig.openid,function (other_openid,other_name,other_avatarUrl,other_gender) {
  108. Self.matched(other_avatarUrl,other_name,other_gender,false);
  109. });
  110. });
  111. },
  112. matched(other_avatarUrl,other_name,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.rivalAvatarUrl = other_avatarUrl;
  119. globalConfig.rivalName = other_name;
  120. globalConfig.rivalGender = other_gender;
  121. //change top UI
  122. this.meteorLine.active = true;
  123. this.titleTxt.getComponent(cc.Label).string = "匹配成功";
  124. this.timeTxt.active = false;
  125. this.waitTxt.active = false;
  126. //show rival information
  127. this.player2.getChildByName("DotAnimation").active = false;
  128. this.player2.active = true;
  129. this.name2.active = true;
  130. this.gender2.active = true;
  131. this.avatarSp2.active = true;
  132. //渠道下一个场景 go to next scene
  133. if(this.nextSceneName!='')
  134. {
  135. cc.director.loadScene(this.nextSceneName);
  136. }
  137. },
  138. setGender(gender, bBoy) {
  139. this.iconBoy = gender.getChildByName('IconBoy');
  140. this.iconGirl = gender.getChildByName('IconGirl');
  141. if (!bBoy) {
  142. this.iconBoy.active = true;
  143. this.iconGirl.active = false;
  144. } else {
  145. this.iconBoy.active = false;
  146. this.iconGirl.active = true;
  147. }
  148. },
  149. loadAvatar(avatarBase64,callback)
  150. {
  151. lib.setImageBase64(avatarBase64,function (texture2D) {
  152. let frame = new cc.SpriteFrame(texture2D);
  153. callback && callback(frame);
  154. });
  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(globalConfig.rivalAvatarUrl,globalConfig.rivalName,globalConfig.rivalGender,true);
  166. }, 2);
  167. }.bind(this));
  168. },
  169. generateAI() {
  170. if (lib.openInWebview())// 在app内Webview打开
  171. {
  172. // match failed so we need to get a Ai information to start game
  173. webView.getAiInfo();
  174. } else {
  175. //TODO
  176. //In browser we just use default information to generate AI
  177. this.resetRivalUIByData(globalConfig.name, globalConfig.gender, globalConfig.avatarBase64);
  178. }
  179. },
  180. onUrlToBase64(data)
  181. {
  182. globalConfig.rivalavatarBase64 = data.base64;
  183. this.resetRivalUIByData(globalConfig.rivalName, globalConfig.rivalGender, globalConfig.rivalAvatarBase64);
  184. },
  185. onGameInit(data) {
  186. globalConfig.name = data.userName;
  187. globalConfig.avatarUrl = data.avatarUrl;
  188. globalConfig.avatarBase64 = data.avatarBase64Url;
  189. globalConfig.gender = data.gender;
  190. globalConfig.calorieParams = data.calorieParams;
  191. globalConfig.device = data.device;
  192. this.initPlayerUI(globalConfig.name, globalConfig.gender,globalConfig.avatarBase64);
  193. },
  194. initPlayerUI(name,gender,avatarBase64) {
  195. this.name1.getComponent(cc.Label).string = name;
  196. this.setGender(this.gender1, gender);
  197. this.loadAvatar(avatarBase64,function (frame) {
  198. this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
  199. }.bind(this));
  200. },
  201. onAiRandomInfo(data) {
  202. globalConfig.rivalAvatarBase64 = data.aiAvatarBase64Url;
  203. globalConfig.rivalName = data.aiName;
  204. globalConfig.rivalGender = data.aiGender;
  205. this.resetRivalUIByData(globalConfig.rivalName, globalConfig.rivalGender, globalConfig.rivalAvatarBase64);
  206. },
  207. });