var beiMiCommon = require("BeiMiCommon"); cc.Class({ extends: beiMiCommon, properties: { // foo: { // default: null, // The default value will be used only when the component attaching // to a node for the first time // url: cc.Texture2D, // optional, default is typeof default // serializable: true, // optional, default is true // visible: true, // optional, default is true // displayName: 'Foo', // optional // readonly: false, // optional, default is false // }, // ... gamebtn: { default: null, type: cc.Node }, continuegamebtn:{ default: null, type: cc.Node }, poker: { default: null, type: cc.Node }, lastCardsPanel: { //底牌 default: null, type: cc.Node }, waitting: { default: null, type: cc.Prefab }, ratio:{ //底牌 default: null, type: cc.Label }, summary_win:{ default:null , type : cc.Prefab }, summary:{ default:null , type : cc.Prefab }, inviteplayer:{ default:null , type : cc.Prefab } }, // use this for initialization onLoad: function () { /** * 适配屏幕尺寸 */ this.resize(); this.player = new Array() ; //存放玩家数据 this.pokercards = new Array(); this.lastcards = new Array(); this.lastCardsPanel.active = false ; this.summarypage = null ; this.inited = false ; this.lasttip = null ; if(cc.beimi!=null){ if(cc.beimi.gamestatus!=null && cc.beimi.gamestatus == "playing"){ //恢复数据 this.recovery() ; }else if(cc.beimi.extparams!=null && cc.beimi.extparams.gamemodel == "room"){ /** * 房卡模式,开始启动游戏,当前玩家进入等待游戏的状态,显示邀请好友游戏,并分配 6位数字的房间号码 */ /** * 处理完毕,清理掉全局变量 * @type {null} */ this.invite = cc.instantiate(this.inviteplayer) ; } this.initgame(); } }, begin:function(){ if(cc.beimi.data!=null && cc.beimi.data.enableai == true){ this.statictimer("正在匹配玩家" , cc.beimi.data.waittime) ; }else{ this.statictimer("正在匹配玩家,请稍候" , cc.beimi.data.noaiwaitime) ; } this.startgame("false") ; }, opendeal:function(){ if(cc.beimi.data!=null && cc.beimi.data.enableai == true){ this.statictimer("正在匹配玩家" , cc.beimi.data.waittime) ; }else{ this.statictimer("正在匹配玩家,请稍候" , cc.beimi.data.noaiwaitime) ; } this.startgame("true") ; }, recovery:function(){ this.statictimer("正在恢复数据,请稍候" , cc.beimi.data.waittime) ; }, initgame:function(){ let self = this ; this.gamebtn.active = true ; this.continuegamebtn.active = false ; if(this.ready()) { let socket = this.socket(); this.game = this.getCommon("DizhuDataBind"); this.map("joinroom" , this.joinroom_event) ; //加入房价 this.map("players" , this.players_event) ; //接受玩家列表 this.map("catch" , this.catch_event) ; //叫地主 this.map("catchresult" , this.catchresult_event) ; //最终抢到地主的玩家 this.map("lasthands" , this.lasthands_event) ; //翻底牌 this.map("takecards" , this.takecards_event) ; //出牌信息 this.map("ratio" , this.ratio_event) ; //有炸 this.map("play" , this.play_event) ; //接受玩家列表 this.map("allcards" , this.allcards_event) ; //我出的牌 this.map("cardtips" , this.cardtips_event) ; //提示 this.map("roomready" , this.roomready_event) ; //提示 this.map("playeready" , this.playeready_event) ; //玩家点击了开始游戏 , 即准备就绪 this.map("cardtips" , this.cardtips_event) ; //提示 this.map("recovery" , this.recovery_event) ; //恢复牌局数据 socket.on("command" , function(result){ cc.beimi.gamestatus = "playing" ; if(self.inited == true){ var data = self.parse(result) ; self.route(data.command)(data , self); } }); /** * 心跳检查,服务端发起的事件,服务端可以设置 PING的时长和 PING的 TimeOut */ socket.on("ping" , function(){ }); /** * 此处有个问题:游戏中,如果点击了返回,进入过其他游戏,这个 cc.beimi.extparams.playway * 就发生了改变,会导致当前游戏结束后,继续游戏的时候会出现问题 * @type {{token: (*|null), playway, orgi, extparams: *}} */ var param = { token:cc.beimi.authorization, playway:cc.beimi.extparams.playway, orgi:cc.beimi.user.orgi, extparams:cc.beimi.extparams } ; socket.emit("joinroom" ,JSON.stringify(param)) ; this.inited = true ; } }, /** * 新创建牌局,首个玩家加入,进入等待状态,等待其他玩家加入,服务端会推送 players数据 * @param data * @param context */ joinroom_event:function(data , context){ if(data.cardroom == true && context.inviteplayer!=null){ let script = context.invite.getComponent("BeiMiQR") script.init(data.roomid); context.invite.parent = context.root() ; } //显示 匹配中,并计时间,超过设定的倒计时时间即AI加入,根据当前的 玩家数量 匹配机器人 if(data.player.id && data.player.id == cc.beimi.user.id){ //本人,开始计时 //console.log("本人") ; //self.player[0] = data ; context.index = data.index ; //当前玩家所在位置 }else{ //其他玩家加入,初始化 var inroom = false ; for(var i = 0 ; i < context.player.length ; i++){ var player = context.player[i].getComponent("PlayerRender") ; if(player.userid == data.player.id){ inroom = true ; } } if(inroom == false){ context.newplayer(context.player.length , context , data.player , context.index + 1 == data.index) ; } } }, /** * 房卡模式下,邀请的好友人到齐了 * @param data * @param context */ roomready_event:function(data , context){ if(data.cardroom == true && context.invite!=null){ context.invite.destroy(); } }, /** * * @param data * @param context */ playeready_event:function(data , context){ if(data.userid == cc.beimi.user.id){ context.gamebtn.active = false ; } }, /** * 接收到服务端的 推送的 玩家数据,根据玩家数据 恢复牌局 , 玩家座位排列方式 , 和麻将统一, 都是东南西北方式 排列 * 0是首位玩家 * 1是右侧玩家 * 2是左侧玩家 * 麻将还有一个对家 * @param data * @param context */ players_event:function(data,context){ var inx = -1 ; for(var i = 0 ; i 1 && inx >=0){ var pos = inx+1 ; while(true){ if(pos == data.player.length){pos = 0 ;} if(context.playerexist(data.player[pos], context) == false){ context.newplayer(context.player.length , context , data.player[pos] , context.player.length == 0 && !(pos == 0 && data.player.length < data.maxplayers) ) ; } if(pos == inx){break ;} pos = pos + 1; } } }, playerexist:function(player,context){ var inroom = false ; if(player.id == cc.beimi.user.id){ inroom = true ; }else{ for(var j = 0 ; j < context.player.length ; j++){ if(context.player[j].id == player.id){ inroom = true ; break ; } } } return inroom ; }, /** * 接收到服务端的 推送的 玩家数据,根据玩家数据 恢复牌局 * @param data * @param context */ catch_event:function(data,context){ /** * 修改倍率 */ if(context.ratio){ context.ratio.string = data.ratio+"倍" ; } if(data.userid == cc.beimi.user.id){ //该我抢 context.game.catchtimer(15); }else{ //该别人抢 for(var inx =0 ; inx 0){ for(var i =0 ; i 0){ self.player[inx] = game.playerspool.get() ; self.player[inx].parent = self.root() ; self.player[inx].setPosition(pos); var render = self.player[inx].getComponent("PlayerRender") ; render.initplayer(data , isRight); } }, // called every frame, uncomment this function to activate update callback // update: function (dt) { // }, /** * 不抢/叫地主 */ givup:function(){ if(this.ready()){ let socket = this.socket(); socket.emit("giveup","giveup"); } }, /** * 开始游戏 */ startgame:function(opendeal){ if(this.ready()){ let socket = this.socket(); socket.emit("start",opendeal); } }, /** * 不抢/叫地主 */ cardtips:function(){ if(this.ready()){ let socket = this.socket(); if(this.lasttip!=null){ socket.emit("cardtips",this.lasttip); }else{ socket.emit("cardtips",""); } this.lasttip = null ; } }, /** * 抢/叫地主触发事件 */ docatch:function(){ if(this.ready()){ let socket = this.socket(); socket.emit("docatch","docatch"); } }, /** * 出牌 */ doPlayCards:function(){ if(this.ready()){ let socket = this.socket(); this.game.selectedcards.splice(0 , this.game.selectedcards.length) ; for(var i=0 ; i