// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html import GameData from "./GameData"; import GameManager from "./GameManager"; import AudioManager from "./tool/AudioManager"; import { Notification } from './tool/Notification'; const {ccclass, property} = cc._decorator;0 @ccclass export default class NewClass extends cc.Component { @property(cc.Node) add: cc.Node = null; @property(cc.SpriteAtlas) plist: cc.SpriteAtlas =null; basicHeight:number //每次跳跃前基础高度 jumpState:number; //跳跃状态,控制1段跳2段跳 jumpHeight:number; //跳跃高度 jumpAction: cc.Tween;//跳跃tween动画,用于停止控制 up:boolean; //跳跃上升下降状态判断 block: cc.Node; death:boolean; interfere:boolean; double:boolean; // LIFE-CYCLE CALLBACKS: // onLoad () {} start () { this.init(); } init(){ this.jumpState = 0; this.jumpHeight = this.node.y; this.basicHeight = this.node.y; this.up = true; this.double = false; this.death = false; this.jumpAction = null; this.block = null; this.interfere = false; } //跳动 jump(){ if(this.jumpState < 2){ if(this.jumpState == 0){ if(this.death == false){ this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(1,"jump",false); } } else if(this.jumpState == 1 || this.node.y > (this.jumpHeight+GameData._instance.GM_INFO.jumpHeight-5)){ if(this.death == false){ this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(1,"jump",false); } } setTimeout(() => { if(this.jumpState == 0){ if(this.block){ this.block.getComponent("Block").hide(); this.block = null; } GameData._instance.CLICK_DATA.jumps = 1; AudioManager._instance.playJump(); this.basicHeight = this.node.y; this.jumpState += 1; this.up = true; Notification.emit("showGround",this.node.y); this.jumpAction = cc.tween(this.node) .to(GameData._instance.GM_INFO.jumpUpTime,{position:cc.v3(0,this.jumpHeight+GameData._instance.GM_INFO.jumpHeight,0)},{easing:'quadOut'}) .call(() =>{ this.up = false; this.jumpState = -1; var temp = this.node.y - this.jumpHeight; console.log("距离",temp) }) .to(GameData._instance.GM_INFO.jumpDownTime,{position:cc.v3(0,this.jumpHeight,0)},{easing:'cubicIn'}) .call(() =>{ this.jumpHeight = this.node.y; this.jumpState = -1; this.up = true; }) .start(); } else if(this.jumpState == 1 || this.node.y > (this.jumpHeight+GameData._instance.GM_INFO.jumpHeight-15)){ if(this.jumpAction)this.jumpAction.stop(); AudioManager._instance.playJump(); GameData._instance.CLICK_DATA.jumps = 2; this.jumpState = 2; this.up = true; this.jumpAction = cc.tween(this.node) .to(GameData._instance.GM_INFO.jumpUpTime,{position:cc.v3(0,this.node.y+GameData._instance.GM_INFO.jumpHeight,0)},{easing:'quadOut'}) .call(() =>{ this.up = false; }) .to(GameData._instance.GM_INFO.jumpDownTime*1.5,{position:cc.v3(0,this.jumpHeight,0)},{easing:'cubicIn'}) .call(() =>{ this.jumpState = -1; this.jumpHeight = this.node.y; this.up = true; }) .start(); } }, 100); } } onCollisionEnter(other: cc.Collider, self: cc.Collider) { if(GameManager._instance){ if(GameManager._instance.over == true){ return; } } let max = other.name.length; let num = 6; if(other.node.name != "Ground"){ if(other.node.getComponent("Block").num > 9) num = 7; } let ColliderName = other.name.substring(num,max); //获取碰撞体名字 if(ColliderName == ""){ if(this.node.y <= other.node.y && this.up == true && this.death == false){ if(this.jumpAction)this.jumpAction.stop(); this.jumpState = -1; AudioManager._instance.playGround(); // console.log("死亡1"); this.gameDeath(); } else if(other.node.name == "Ground" && this.death == false){ // if(this.jumpAction)this.jumpAction.stop(); this.jumpState = -1; // console.log("死亡2"); this.gameDeath(); } else{ // console.log("没碰到边缘",this.up,this.node.y,other.node.y,this.jumpState,this.up); } } if(ColliderName == "" ){ if(this.death == true){ // console.log("已经死亡"); return; } // let portrait = Math.abs(this.node.y-other.node.y) let width = other.node.width/2; let pos = Math.abs(this.node.x - other.node.x); let distance =pos/width; // console.log("我的位置:",this.node.x,"块的位置:",other.node.x,"块的宽度:",width,"百分比:",distance); if(this.up == true){ // console.log("死亡4"); AudioManager._instance.playGround(); this.gameDeath(); } else{ if(distance*100 < GameData._instance.GM_INFO.distanceMin && this.jumpState != 0){ // console.log("成功1"); console.log("百分比:",distance*100+"%"); this.success(other,distance*100); } else{ if(portrait >= 20 || distance*100 > GameData._instance.GM_INFO.distanceMax){ // console.log("死亡3","落差:",portrait,"状态:",this.up); AudioManager._instance.playGround(); this.gameDeath(); } else{ // console.log("成功2"); console.log("百分比:",distance*100+"%"); this.success(other,distance*100); } } } } } success(other,distance){ GameData._instance.CLICK_DATA.percent = Math.floor(distance); if(GameManager._instance)GameManager._instance.destroyBlock(); this.add.active = true; this.add.opacity = 0; let number = 1; if(distance <= 7.5) number = 3; else if(distance <= 15) number = 2; GameData._instance.CLICK_DATA.accuracy = number; this.add.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.plist.getSpriteFrames()[number]; this.add.getChildByName("jia").getComponent(cc.Sprite).spriteFrame = this.plist.getSpriteFrames()[number+3]; this.addAction(); this.node.getChildByName("guang").active = true; this.node.getChildByName("guang").getComponent(sp.Skeleton).setAnimation(0,"double_1",false); if(this.jumpAction)this.jumpAction.stop(); if(this.death == false){ this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(1,"jump",false); } this.block = other.node; this.block.getComponent("Block").setScore(number); this.basicHeight = this.node.y; if(number > 1){ AudioManager._instance.playPz(); if(this.double == false){ this.double = true; GameData._instance.LEVEL_INFO.doubleHit = 1.05; } else{ GameData._instance.LEVEL_INFO.doubleHit += GameData._instance.LEVEL_INFO.doubleAdd; } var combo = (GameData._instance.LEVEL_INFO.doubleHit-1)*(1/0.05); GameData._instance.CLICK_DATA.combo = parseInt(combo + ""); } else{ AudioManager._instance.playLuo(); this.double = false; GameData._instance.CLICK_DATA.combo = 0; } Notification.emit("setData",null); setTimeout(() => { Notification.emit("createBlock",null); }, 10); if(number > 1 && GameData._instance.LEVEL_INFO.doubleSuccess != 0 ){ GameData._instance.LEVEL_INFO.doubleSuccess -= 1; } else{ if(GameData._instance.LEVEL_INFO.doubleSuccess != 0 && GameData._instance.LEVEL_INFO.doubleSuccess<3){ GameData._instance.LEVEL_INFO.doubleSuccess = 3; } } if(GameData._instance.LEVEL_INFO.doubleSuccess == 0){ setTimeout(() => { Notification.emit("createCrackBlock",null); }, 20); } else{ GameManager._instance.interfere = false; GameData._instance.CLICK_DATA.fake = GameManager._instance.interfere; } this.jumpState = 0; this.up = true; this.node.y = other.node.y-3+other.node.height/2; this.jumpHeight = this.node.y; } gameDeath(){ // console.log("条失败"); GameData._instance.CLICK_DATA.combo = 0; GameData._instance.CLICK_DATA.accuracy = 0; GameData._instance.CLICK_DATA.percent = -1; Notification.emit("setData",null); this.double = false; GameData._instance.LEVEL_INFO.doubleSuccess = 3; this.death = true; if(this.jumpAction)this.jumpAction.stop(); this.jumpState = -1; Notification.emit("death",null); } xinAction(){ // console.log("生命:",GameData._instance.GM_INFO.life); if(GameData._instance.GM_INFO.life >= 0){ let xin = this.node.getChildByName("xin"); xin.y = 120; cc.tween(xin) .to(0.2,{opacity:255,y:xin.y+50}) .delay(0.5) .to(0.3,{opacity:0,y:xin.y+100}) .call(() =>{ }) .start(); } } addAction(){ this.add.y = 120; cc.tween(this.add) .to(0.2,{opacity:255,y:this.add.y+50}) .delay(0.5) .to(0.3,{opacity:0,y:this.add.y+100}) .call(() =>{ this.add.active = false; }) .start(); } update (dt) { // if(this.death == true && this.node.y > -500 && GameManager._instance.over == false){ // // this.node.y -= 3; // // if(this.node.y <= -500){ // // this.node.y = -500; // // } // } } }