// Scene的控制器 // 管理角色和球的信息 var Role = require("role") var Ball = require("ball") var Numlabel = require("numlabel") cc.Class({ extends: cc.Component, properties: { rival: { default: null, type: Role, }, mine: { default: null, type: Role, }, ball: { default: null, type: Ball, }, // 分数标签 mines: { default: null, type: Numlabel, }, rivals: { default: null, type: Numlabel, }, // 几个 scorepanel: { default: null, type: cc.Node, }, // 操作面板 operatepanel: { default: null, type: cc.Node, }, // 结束面板 endpanel: { default: null, type: cc.Node, }, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start () { this.initValues(); this.initListener(); // 游戏开始 this.gameStart(); }, // 初始化数据 initValues:function() { // 球的状态信息 this.ballconfig = require("ballconfig"); // 当前球的状态,初始化为发球开始状态 this.ballstate = this.ballconfig.BALL_STATE_SERVE_START; this.mineScore = 0; this.rivalScore = 0; }, // 初始化监听 initListener:function() { var self = this; this.rival.serveStart = function() { cc.log ("发球了 ... ") // self.rivalserve.ballServe(); self.ball.rivalServer(); self.ball.ballAction(); } // 添加触摸开始事件 this.node.on (cc.Node.EventType.TOUCH_START, function(event) { // 如果是处于结束状态,则无法响应 if (self.isEnd) { return; } var touches = event.getTouches(); var loc = self.node.convertToNodeSpaceAR(touches[0].getLocation()); var value = Math.random() * 2; // 根据位置判断我方的方向 if (loc.x < 0) { cc.log ("在左侧 ... ") // 左侧可接,暂时我方左侧只有这个功能 self.mine.pat.pat(); self.mine.setDir(1); if (self.ballstate == self.ballconfig.BALL_STATE_MINE_LEFT_PAT) { self.ball.ballStop(); if (value > 1) { // 往左边打 self.ball.minePatLToL_01(); } else { // 往右边打 self.ball.minePatLToR_01(); } } } else { cc.log ("来右边 ... ") self.mine.setDir(2); if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PAT) { self.ball.ballStop(); // 右侧可接 self.mine.pat.pat(); // 这个里面只有往右的 self.ball.minePatRToR_01(); } else if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PEEL) { // 右侧可削 self.mine.peel.peel(); self.scheduleOnce (function() { // 需要保证不是在结束状态 if (!this.isEnd) { self.ball.ballStop(); // 这个里面只有往左的 self.ball.minePeelRToL_01(); } }, 0.2); } else { self.mine.pat.pat(); } } }); // 配置球的状态判断回调 this.ball.stateCallback = function(state) { // 此处为测试球运动专用功能 // if (state == this.ballconfig.BALL_STATE_DROP_IN_RIVAL || // state == this.ballconfig.BALL_STATE_DROP_IN_MINE) { // return // } self.ballstate = state; }; }, // 功能性方法 // 帧判断,用来检测球状态,给对手的行为进行决策 rivalUpdate:function (delay) { // 需要保证不打扰削球动作 if (this.rival.ispeel == true) { return; } this.rival.ispeel = false; var value = Math.random() * 2; if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_LEFT_PAT) { this.ball.ballStop(); this.rival.pat.pat(); this.rival.setDir(3); // 敌方左侧可接 if (value > 1) { // 往左侧打 this.ball.rivalPatLToL_01(); } else { // 往右侧打 this.ball.rivalPatLToR_01(); } } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_LEFT_PEEL) { this.rival.ispeel = true; this.rival.peel.peel(); this.rival.setDir(3); var self = this; this.scheduleOnce (function() { self.rival.ispeel = false; if (!this.isEnd) { this.ball.ballStop(); // 敌方左侧可削 if (value > 1) { // 往左侧削 this.ball.rivalPeelLtoL_01(); } else { // 往右侧削 this.ball.rivalPeelLtoR_01(); } } }, 0.2); } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_RIGHT_PAT) { this.ball.ballStop(); this.rival.pat.pat(); this.rival.setDir(4); var index = Math.random() * 2; // 敌方右侧可接 if (value > 1) { // 往左侧打 if (index > 1) { this.ball.rivalPatRToL_01(); } else { this.ball.rivalPatRToL_02(); } } else { // 往右侧打 if (index > 1) { this.ball.rivalPatRToR_01(); } else { this.ball.rivalPatRToR_02(); } } } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_RIGHT_PEEL) { // 没有 } else if (this.ballstate == this.ballconfig.BALL_STATE_DROP_IN_MINE) { // 掉落到我方方位 this.win = false; this.roundOver(); } else if (this.ballstate == this.ballconfig.BALL_STATE_DROP_IN_RIVAL) { // 掉落到敌方方位 this.win = true; this.roundOver(); } }, // 游戏开始 gameStart:function() { this.ballstate = this.ballconfig.BALL_STATE_SERVE_START; this.ball.ballEnd(); // 将双方置为等待状态 this.mine.idle.idle(); // 设置面板信息 this.scorepanel.active = true; this.operatepanel.active = true; this.endpanel.active = false; // 其他数据 this.isEnd = false; // 游戏开始,对方开始发球 this.rival.setDir(4); this.rival.roleServe(); cc.log ("发起了 .. ") this.schedule(this.rivalUpdate, 1.0 / 60); }, // 回合结束 roundOver:function() { this.unschedule(this.rivalUpdate); this.isEnd = true; // 停止动作,主要是为了停止回调 this.mine.stop(); this.rival.stop(); if (this.win) { cc.log ("游戏结束,胜利了 .. ") this.mine.win.win(); this.rival.fail.fail(); this.mines.value ++; this.mines.updateValue(); if (this.mines.value == 5) { this.scheduleOnce(this.gameOver, 3); return; } } else { cc.log ("输了 ... ") this.rival.win.win(); this.mine.fail.fail(); this.rivals.value ++; this.rivals.updateValue(); if (this.rivals.value == 5) { this.scheduleOnce(this.gameOver, 3); return; } } this.scheduleOnce(this.gameStart, 3); }, // 游戏结束 gameOver:function() { if (this.mines.value == 5) { cc.log("我方胜利 ... "); } else { cc.log("敌方胜利 ... "); } this.scorepanel.active = false; this.operatepanel.active = false; this.endpanel.active = true; this.mines.value = 0; this.mines.updateValue(); this.rivals.value = 0; this.rivals.updateValue(); }, // 事件 // 重新开始 restartClicked:function() { this.gameStart(); }, // 点击排行榜 rankClicked:function() { if (cc.sys.platform == cc.sys.WECHAT_GAME) { wx.shareAppMessage({ withShareTicket: true, title: '我要主动拉起分享啦', success: function(shareTickets,groupMsgInfos){console.log('主动成功')}, fail: function(res){console.log('主动失败');console.log(res);}, }); } } });