| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- var webView = require("../WebView");
- var lib = require("../Library");
- var mgobe = require("../Mgobe");
- cc.Class({
- extends: cc.Component,
- properties: {
- nextSceneName: {
- default: '',
- },
- player1: {
- default: null,
- type: cc.Node,
- },
- player2: {
- default: null,
- type: cc.Node,
- },
- titleTxt: {
- default: null,
- type: cc.Node,
- },
- timeTxt: {
- default: null,
- type: cc.Node,
- },
- waitTxt: {
- default: null,
- type: cc.Node,
- },
- successAudio: {
- default: null,
- type: cc.AudioClip,
- },
- meteorLine: {
- default: null,
- type: cc.Node,
- },
- },
- onLoad() {
- this.init();
- //init web sdk
- if (lib.openInWebview())
- {
- // 在app内Webview打开
- webView.init(this.node);
- }
- //register event from webView
- this.node.on('onGameInit',this.onGameInit,this);
- this.node.on('onAiRandomInfo',this.onAiRandomInfo,this);
- this.node.on('onUrlToBase64',this.onUrlToBase64,this);
- // this.starMatching();
- //count down time to match ui
- let interval = 1; // 以秒为单位的时间间隔let
- let repeat = cc.macro.REPEAT_FOREVER; // 重复次数
- let delay = 0; // 开始延时
- this.schedule(this.countTime, interval, repeat, delay);
- // 因为在手机端加载场景要5-8秒钟 所以提早加载
- // because loading scene not a immediately stuff,A smart phone will cost 5-8 second for loading
- if(this.nextSceneName!='')
- {
- // cc.director.preloadScene(this.nextSceneName);
- cc.director.preloadScene(this.nextSceneName, function () {
- cc.log("Next scene preloaded");
- });
- }
- },
- init() {
- this.matchTime = 0;
- this.name1 = this.player1.getChildByName('Name');
- this.name2 = this.player2.getChildByName('Name');
- this.gender1 = this.player1.getChildByName('Gender');
- this.gender2 = this.player2.getChildByName('Gender');
- this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
- this.avatarSp2 = this.player2.getChildByName('Mask').getChildByName('AvatarSp');
- },
- countTime() {
- //update countdown time on UI
- this.timeTxt.getComponent(cc.Label).string = this.updateTime(this.matchTime);
- this.matchTime++;
- if (this.matchTime > 5)
- {
- this.unschedule(this.countTime);
- // 在app内Webview打开
- if(lib.openInWebview())
- {
- // mgobe.cancelPlayerMatch(function ()
- // {
- this.generateAI();
- // }.bind(this));
- }
- else {
- this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
- }
- }
- },
- setGender(gender, bBoy) {
- this.iconBoy = gender.getChildByName('IconBoy');
- this.iconGirl = gender.getChildByName('IconGirl');
- if (!bBoy) {
- this.iconBoy.active = true;
- this.iconGirl.active = false;
- } else {
- this.iconBoy.active = false;
- this.iconGirl.active = true;
- }
- },
- initPlayer1() {
- this.name1.getComponent(cc.Label).string = webView.userName;
- this.setGender(this.gender1, webView.gender);
- this.loadAvatar(webView.avatarBase64,function (frame) {
- this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
- }.bind(this));
- },
- loadAvatar(avatarBase64,callback)
- {
- lib.setImageBase64(avatarBase64,function (texture2D) {
- let frame = new cc.SpriteFrame(texture2D);
- callback && callback(frame);
- });
- },
- starMatching() {
- //before matching we need to load a cert file in resource folder
- return cc.loader.loadRes("/cacert", cc.Asset, (err, asset) => {
- console.log("加载证书结束 " + (!err));
- if (err)return;
- mgobe.cacertNativeUrl = asset.nativeUrl;
- mgobe.initSDK(function ()
- {
- // bind the init success event
- mgobe.room.onRecvFromClient = this.onRecvFromClient.bind(this);
- mgobe.matchPlayers(function ()
- {
- webView.bAi = false;
- //send my information to others so that they can generate rival
- mgobe.sendMessage(JSON.stringify({
- 'avatarUrl': webView.avatarUrl,
- 'userName': webView.userName,
- 'gender': webView.gender
- }));
- }.bind(this,lib));
- }.bind(this));
- });
- },
- updateTime(t) {
- let theTime = Math.floor(t);
- let theTime1 = 0;// 分
- if (theTime > 60)
- {
- theTime1 = parseInt(theTime / 60);
- theTime = parseInt(theTime % 60);
- }
- let result = '';
- result = (theTime1 < 10 ? "0" + theTime1 : theTime1) + ":" + (theTime < 10 ? "0" + theTime : theTime);
- return result;
- },
- resetRivalUIByData(userName, gender, avatarBase64) {
- this.name2.getComponent(cc.Label).string = userName;
- this.setGender(this.gender2, gender);
- // loading avatar
- //for mobile device we need to download avatar whatever AI or others
- this.loadAvatar(avatarBase64, function (frame) {
- this.avatarSp2.getComponent(cc.Sprite).spriteFrame = frame;
- cc.audioEngine.playEffect(this.successAudio, false);
- this.scheduleOnce(() => {
- this.matched();
- }, 2);
- }.bind(this));
- },
- matched() {
- //change top UI
- this.meteorLine.active = true;
- this.titleTxt.getComponent(cc.Label).string = "匹配成功";
- this.timeTxt.active = false;
- this.waitTxt.active = false;
- //show rival information
- this.player2.getChildByName("DotAnimation").active = false;
- this.player2.active = true;
- this.name2.active = true;
- this.gender2.active = true;
- this.avatarSp2.active = true;
- this.scheduleOnce(() => {
- if(this.nextSceneName!='')
- {
- // webView.unRegister(this.node);
- cc.director.loadScene(this.nextSceneName);
- }
- }, 1);
- },
- generateAI() {
- if (lib.openInWebview())// 在app内Webview打开
- {
- // match failed so we need to get a Ai information to start game
- webView.getAiInfo();
- } else {
- //TODO
- //In browser we just use default information to generate AI
- this.resetRivalUIByData(webView.userName, webView.gender, webView.avatarBase64);
- }
- },
- onRecvFromClient(event) {
- console.log("收到新消息" + event.data.msg);
- this.unschedule(this.countTime);
- let data = JSON.parse(event.data.msg);
- webView.rivalavatarUrl = data.avatarUrl;
- webView.rivalUserName=data.userName;
- webView.rivalGender = data.gender;
- if (lib.openInWebview())
- {
- // 在app内Webview打开
- webView.getBase64(webView.rivalavatarUrl);
- }
- else {
- this.matched();
- }
- },
- onUrlToBase64(data)
- {
- webView.rivalavatarBase64 = data.base64;
- console.log('rivalavatarBase64=',webView.rivalavatarBase64);
- this.resetRivalUIByData(webView.rivalUserName, webView.rivalGender, webView.rivalavatarBase64);
- },
- onGameInit(data) {
- this.initPlayer1();
- },
- onAiRandomInfo(data) {
- let rivalavatarBase64 = webView.rivalavatarBase64;
- let rivalUserName = webView.rivalUserName;
- let rivalGender = webView.rivalGender;
- this.resetRivalUIByData(rivalUserName, rivalGender, rivalavatarBase64);
- },
- });
|