var GameStates = require('GameStates'); var Constants = require('Constants'); cc.Class({ extends: cc.Component, properties: { isPlayingBoos: false, PerfectGrade: 0, MyAudio: { default: null, type: cc.Node, }, FemaleDragonBone: cc.Node, PlayerAnimControl: null, PlayerStatesScript: null, isBack: false, isMoveToOrigin: false,//回到原点 backwardsDistance: 100, JavelinPrefab: { default: null, type: cc.Prefab, }, isJavelinFlying: false, JavelinTempPrefab: null, //起跳点距离 takeOffDistance: 0, }, start() { this.PlayerAnimControl = this.FemaleDragonBone.getComponent('PlayerAnimControl'); this.PlayerStatesScript = this.getComponent('PlayerStates'); // cc.log('this.PlayerStatesScript is ', this.PlayerStatesScript.name); // this.PlayerAnimControl.setEndListener(function (name) { // cc.log('333',name); // }.bind(this)); }, onLoad: function () { }, update(dt) { var enterDeceleration = GlobalData.game.isEnterGameFinishZone; if (enterDeceleration) { GlobalData.gameMode.DecelerationGameSpeed_lerp(dt); } var alwaysAddGameSpeed = GlobalData.game.playerCurrentZone == 1 && GlobalData.game.isItAlwaysTouch; if (alwaysAddGameSpeed) {//加速带加速 GlobalData.gameMode.AddGameSpeed(Constants.longAccelerationAddSpeedNum * dt); } //玩家模型向后移动 if (this.isBack) { this.nodeBackwards(dt, this.FemaleDragonBone); } if (this.isMoveToOrigin) { this.PlayerMoveToOrigin(dt); } if (this.isJavelinFlying) { if (this.JavelinTempPrefab != null) { this.nodeBackwards(dt, this.JavelinTempPrefab); } } }, //***************声音部分***start*/ addPerfectGradeAndPlay: function () { if (this.PerfectGrade < 10) { this.PerfectGrade += 1; } this.playAudioByName("Perfect" + this.PerfectGrade); }, resetPerfectGrade: function () { this.PerfectGrade = 0; }, playAudioByName(AudioName) { // console.log(AudioName); var AudioSource = this.getAudioSourceByName(AudioName); if (AudioSource != null) { if (AudioName == "Boos") {//踉跄时的音效:若正在播放此音效则不能播放; if (this.isPlayingBoos == false) { this.isPlayingBoos = true; AudioSource.play(); this.scheduleOnce(function () { this.isPlayingBoos = false; }, AudioSource.getDuration()); } } else { AudioSource.play(); } } }, stopAudioByName(AudioName) { var AudioSource = this.getAudioSourceByName(AudioName); if (AudioSource != null) { AudioSource.stop(); } }, getAudioSourceByName(AudioName) { if (this.MyAudio != null) { var AudioControlScript = this.MyAudio.getComponent("AudioControl"); if (AudioControlScript.getAudioSourceByName(AudioName) != null) { var AudioSource = AudioControlScript.getAudioSourceByName(AudioName); return AudioSource; } } }, //***************声音部分***end*/ // ***********碰撞***start/ onCollisionEnter: function (other) { // console.log('on collision enter', other.node.name); if (other.node.name == 'LongABand') { GlobalData.game.isMustTouch = false;//只要进入长加带,就重置一下这个变量 GlobalData.game.playerCurrentZone = GameStates.EnterZoneName.LongAccelerationZone; } else if (other.node.name == 'Javelin') { other.node.active = false; //拿起标枪 todo... this.PlayerStatesScript.strPlayerStates = 'HoldJavelinRun'; GlobalData.game.PlayerRunState = GlobalData.GameManager.PlayerRunState.HoldJavelinRun; // console.log('GlobalData.game.PlayerRunState',GlobalData.game.PlayerRunState); this.PlayerAnimControl.SpeedChange(); } else if (other.node.name == 'ThrowingPoint') { //到达扔标枪的地点 todo... // cc.log(this.PlayerStatesScript.strPlayerStates, '++++', this.PlayerAnimControl.PlayerStatesScript.strPlayerStates); GlobalData.game.isCanTouch = false; this.PlayerAnimControl.ThrowJavelinOut(); GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Stop; setTimeout(function () {//完成动作后扔标枪 GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Run; this.PlayerStatesScript.strPlayerStates = 'NormalRun'; GlobalData.game.PlayerRunState = GlobalData.GameManager.PlayerRunState.NormalRun; //玩家向后移动 this.isBack = true; this.javelinHero(); }.bind(this), 300)//需要标枪动作时间 } else if (other.node.name == 'Bike') { //碰撞到自行车 todo... // other.node.active = false; GlobalData.game.isCanTouch = false; GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Stop; //处理骑车动画 this.PlayerStatesScript.strPlayerStates = 'RideElephantRun'; GlobalData.game.PlayerRunState = GlobalData.GameManager.PlayerRunState.RideElephantRun; this.PlayerAnimControl.JumpOnElephant(); var otherNode = other.node; cc.find('Canvas').getChildByName('PlayerShadow').active = false; this.PlayerAnimControl.setEndListener(function () { otherNode.active = false; cc.find('Canvas').getChildByName('PlayerShadow').active = true; cc.find('Canvas').getChildByName('PlayerShadow').scale = 3; }) setTimeout(function () { GlobalData.game.isCanTouch = true; GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Run; }, 1000); } else if (other.node.name == 'GroundCollision') { GlobalData.game.playerCurrentZone = GameStates.EnterZoneName.GroundZone; GlobalData.game.isMustTouch = false; } }, onCollisionExit: function (other, self) { if (other.node.name == 'LongABand') { GlobalData.game.playerCurrentZone = GameStates.EnterZoneName.null // console.log('on collision enter', other.node.name, GlobalData.game.playerCurrentZone); if (!GlobalData.game.isMustTouch) {//如果一次都没点击过 GlobalData.gameMode.GameOver(); return; } if (GlobalData.game.isItAlwaysTouch) { //如果还在按钮按着,就摔倒。todo.... GlobalData.gameMode.GameOver(); GlobalData.gameMode.StopTouchLongAcceleration(); } else { GlobalData.gameMode.StopTouchLongAcceleration(); } } else if (other.node.name == 'GroundCollision') { GlobalData.game.playerCurrentZone = GameStates.EnterZoneName.null; } else if (other.node.name == 'AccelerationBandNormal' || other.node.name == 'PerfectHurdleBand'||other.node.name =='EndNormalLongJumpBand') { if (!GlobalData.game.isMustTouch) { // console.log('进入普通加速带没点击。GameOver'); //跨栏没跨过播放跨栏断绳子的动画 if(other.node.parent.name == 'CushionAndRailing'){ var animCtrl = other.node.parent.getChildByName('Railing').getComponent(cc.Animation); animCtrl.play("RailingBreak"); } GlobalData.gameMode.GameOver(); } }else if (other.node.name == 'EndNormalLongJumpBand') { if (!GlobalData.game.isMustTouch) { // console.log('进入普通加速带没点击。GameOver'); GlobalData.gameMode.GameOver(); } } }, // ***********碰撞***end/ //跳远 HeroLongJump: function () { var longJumpData = { name: "Hero", longJumpCount: GlobalData.game.longJumpCount,//跳的距离 takeOffDistance: this.takeOffDistance,//蓝区域跳的距离范围 } this.HeroLongJumpAnimation(longJumpData); this.playAudioByName("Hei"); }, HeroLongJumpAnimation: function (tempData) { //跳远 计数算法 todo // var UIControl = cc.find("UIControl").getComponent("UIControl"); var oldSpeed = GlobalData.game.GameSpeed; var jumpDistance = 3 + tempData.longJumpCount; //跳了多少米 var jumpPixelSpeed = jumpDistance > 4 ? 900 : 600; var jumpDistancePixel = jumpDistance * 240 + tempData.takeOffDistance;//1m约等于 240像素,并且加上 开始的距离 var jumpTimer = jumpDistancePixel / jumpPixelSpeed;//跳远的时间 GlobalData.game.GameSpeed = jumpPixelSpeed;//跳远时候用跳远的速度 // var actionMoveBy = cc.moveBy(jumpTimer, cc.p(jumpDistancePixel, 0)); // this.node.runAction(actionMoveBy); var jumpHeight = (jumpDistance - 3) * 10 + 100;//100是固定高度 var actionBy = cc.jumpBy(jumpTimer, 0, 0, jumpHeight, 1); // console.log("跳远的距离====:", jumpDistance, jumpTimer, tempData.takeOffDistance); this.node.runAction(actionBy); this.PlayerAnimControl.LongJumpUp(jumpTimer); this.PlayerAnimControl.setEndListener(function (name) { if(name == 'FemaleJump'){ this.PlayerAnimControl.LongJumpDown(jumpTimer); } }.bind(this)); //显示UI GlobalData.game.getGameUIState().onStartShowUIDistance(jumpDistance, jumpTimer); //加分 GlobalData.game.GameScore +=jumpDistance*300; setTimeout(function () { //跳远完成,停1s GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Stop; GlobalData.game.GameSpeed = oldSpeed; setTimeout(function () { GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Run; this.PlayerAnimControl.SpeedChange(); }.bind(this), 1000); }.bind(this), jumpTimer * 1000); }, //标枪 //模型向后移动 nodeBackwards(dt, node) { // console.log('===',node.name); if (node.name == 'FemaleDragonBone') { node.x += -GlobalData.game.GameSpeed * dt; if (node.x <= -this.backwardsDistance) { this.isBack = false; } } else if (node.name == 'JavelinFlying') { if (GlobalData.game.PlayerState == GlobalData.GameManager.PlayerState.Stop) { return; } node.x += -GlobalData.game.GameSpeed * dt; if (node.x <= -this.backwardsDistance) { // this.JavelinTempPrefab = null; this.isJavelinFlying = false; node.destroy(); // console.log('66',this.JavelinTempPrefab.name); } } }, //模型回到原点 PlayerMoveToOrigin(dt) { //Constants.JaveEndSpeed 定义速度 this.FemaleDragonBone.x += Constants.JaveEndSpeed * dt; if (this.FemaleDragonBone.x >= 0) { this.FemaleDragonBone.x = 0; this.isMoveToOrigin = false; //回到原点后 todo this.isJavelinFlying = true; GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Run; GlobalData.game.GameSpeed = Constants.JaveEndSpeed; GlobalData.game.isCanTouch = true; } }, //计算height,和duration javelinData: function (x) { var MiddleX = x / 2; var highest = Math.sqrt(x * x - MiddleX * MiddleX) - x; highest = highest < 0 ? -highest : highest; highest += 120; var temp = x / 720; // console.log("标枪的x值=", temp, "this.javeCount=", GlobalData.game.javeCount, "highest==", highest); var tempDuration = x > 5000 ? 4 : 2; var data = { duration: tempDuration, position: 0, y: 0, height: highest,//600 jumps: 1 }; return data; }, javelinHero: function () { //扔标枪 动作 todo... var x = Constants.JaveBaseDistance + GlobalData.game.javeCount * 72; var tPrefab = cc.instantiate(this.JavelinPrefab); tPrefab.parent = this.node; tPrefab.active = true; tPrefab.setPosition(30, 50); var oldSpeed = GlobalData.game.GameSpeed; GlobalData.game.GameSpeed = 1500; var flyTimer = x / GlobalData.game.GameSpeed;//跳远的时间 //vt+(1/2)at^2 v= gt g=9.8 var V_up = Math.tan(45) * GlobalData.game.GameSpeed; var height = V_up * flyTimer * 0.5 + 0.5 * -320 * (flyTimer * 0.5 * flyTimer * 0.5); var actionBy = cc.jumpTo(flyTimer, 0, 0, height / 4, 1); tPrefab.runAction(actionBy); var actionrotateBy = cc.rotateBy(flyTimer, 120); tPrefab.runAction(actionrotateBy); this.playAudioByName("JavelinFlying"); //显示UI var javeShowDistance = (x)/72; GlobalData.game.getGameUIState().onStartShowUIDistance(javeShowDistance, flyTimer); //加分 GlobalData.game.GameScore +=javeShowDistance*300; setTimeout(function () { this.stopAudioByName("JavelinFlying");//停止标枪飞的声音 this.playAudioByName("JavelinDown");//播放标枪落地的声音 this.PlayerAnimControl.SpeedChange(); this.JavelinTempPrefab = tPrefab; //停下来,等模型回到原点继续跑 GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Stop; this.isMoveToOrigin = true; GlobalData.game.GameSpeed = oldSpeed; }.bind(this), flyTimer * 1000); }, })