// 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, }, // 音效 // 击打 crashclip: { default: null, type: cc.AudioClip, }, winclip: { default: null, type: cc.AudioClip, }, failclip: { default: null, type: cc.AudioClip, }, // 摄像机 camera: { default: null, type: cc.Camera, }, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start () { this.initValues(); this.initListener(); // 游戏开始 this.gameStart(); }, // 初始化数据 initValues:function() { // 球的状态信息 this.ballconfig = require("ballconfig"); // 加载全局变量 this.global = require("global"); // 当前球的状态,初始化为发球开始状态 this.ballstate = this.ballconfig.BALL_STATE_SERVE_START; // 我方得分和敌方得分 this.mineScore = 0; this.rivalScore = 0; var index = Math.random() * 3; if (index > 2) { this.camera.backgroundColor = new cc.Color().fromHEX("#FFB4DE"); } else if (index > 1 && index < 2) { this.camera.backgroundColor = new cc.Color().fromHEX("#FFF8E0"); } else { this.camera.backgroundColor = new cc.Color().fromHEX("#E0FFF0"); } }, // 初始化监听 initListener:function() { var self = this; this.rival.serveStart = function() { cc.log ("发球了 ... ") // self.rivalserve.ballServe(); self.ball.rivalServer(); self.ball.ballAction(); // 发出撞击声 self.scheduleOnce (function() { cc.audioEngine.play(self.crashclip, false, 1); }, 1.2); } // 添加触摸开始事件 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) { console.log ("在左侧 ... ") // 左侧可接,暂时我方左侧只有这个功能 self.mine.pat.pat(); self.mine.setDir(1); if (self.ballstate == self.ballconfig.BALL_STATE_MINE_LEFT_PAT) { self.ball.ballStop(); cc.audioEngine.play(self.crashclip, false, 1); // self.ball.minePatLToL_01(); if (value > 1) { console.log ("往左侧打 ... ") // 往左边打 self.ball.minePatLToL_01(); } else { console.log ("往左侧打 ... ") // 往右边打 self.ball.minePatLToR_01(); } } } else { console.log ("来右边 ... ") self.mine.setDir(2); if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PAT) { self.ball.ballStop(); cc.audioEngine.play(self.crashclip, false, 1); // 右侧可接 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(); cc.audioEngine.play(self.crashclip, false, 1); // 这个里面只有往左的 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.global.infiMode == false) { // 否则需要进行校验 var count = parseInt(Math.random() * 100); if (count > 15) { return; } } else { // 时间戳增加,用来调整难度 this.timestamp++; // 保证速度不会小于50 if (this.ball.speed > 50) { this.ball.speed -= 8.0 / 60; } } // 需要保证不打扰削球动作 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(); cc.audioEngine.play(this.crashclip, false, 1); 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(); cc.audioEngine.play(self.crashclip, false, 1); // 敌方左侧可削 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(); cc.audioEngine.play(this.crashclip, false, 1); 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.timestamp = 0; // 初始化速度 this.ball.speed = 100; // 游戏开始,对方开始发球 this.rival.setDir(4); this.rival.roleServe(); this.schedule(this.rivalUpdate, 1.0 / 45); }, // 回合结束 roundOver:function() { if (this.global.infiMode) { this.global.score = this.timestamp / 45; console.log ("时间 -> " + this.global.score + ", 时间 = " + this.timestamp / 45) this.global.saveBestTime(); } this.unschedule(this.rivalUpdate); this.isEnd = true; // 停止动作,主要是为了停止回调 this.mine.stop(); this.rival.stop(); if (this.win) { cc.log ("游戏结束,胜利了 .. ") cc.audioEngine.play(this.winclip, false, 1); 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 ("输了 ... ") cc.audioEngine.play(this.failclip, false, 1); 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.global.infiMode) { if (this.mines.value == 5) { cc.log("我方胜利 ... "); // 得一分 this.global.score ++; this.global.saveBestScore(); } 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(); }, // 事件 // 返回 backClicked:function() { cc.director.loadScene("startscene") }, // 重新开始 restartClicked:function() { this.gameStart(); }, // 点击排行榜 rankClicked:function() { if (cc.sys.platform == cc.sys.WECHAT_GAME) { wx.shareAppMessage({ withShareTicket: true, title: '乒乓高手', query: "mode=" + "pingpong", imageUrl: 'https://pics6.baidu.com/feed/a8014c086e061d95efd7d3f118ee0dd463d9ca02.jpeg?token=5f7a71d9129d97b8411b4965f7e23a7c&s=A8E2AF0810E09ABEE6B56DDE010050A2', success: function(shareTickets,groupMsgInfos) { console.log('主动成功') }, fail: function(res) { console.log('主动失败'); console.log(res); }, }); } } });