123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- 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,
- },
- },
-
-
- 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.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);
-
-
- 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) {
-
-
-
-
-
- self.ballstate = state;
- };
- },
-
-
- rivalUpdate:function (delay) {
-
-
- if (this.global.infiMode == false) {
-
- var count = parseInt(Math.random() * 100);
- if (count > 15) {
- return;
- }
- } else {
-
- this.timestamp++;
-
- 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);
- },
- });
- }
- }
- });
|