import BaseEvent from "../fight/evnet/base/BaseEvent"; /** * 推箱子、石头 */ const {ccclass, property} = cc._decorator; @ccclass export default class JG0109 extends BaseEvent { @property({ displayName:'推图标', type:cc.SpriteFrame }) pullIcon: cc.SpriteFrame = null; /** * 推的方向 * 上下左右 * 1,2,3,4 */ private pullDir = -1; /** * 当前是否推的状态 */ private isPull = false /** * 主角进入碰撞区域 * @param tag 碰撞组件编号 */ public onBegin(tag: number) { let pullDir = this.getPullDir() // cc.log('pullDir : ',pullDir) if(pullDir > 0){ this.isPull = true; this.pullDir = pullDir this.showOptTouch(this.pullIcon, () => { this.pull() },()=>{ this.isPull = false; }) } } /** * 主角离开碰撞区域 * @param tag 碰撞组件编号 */ public onEnd(tag: number) { this.closeOptTouch() this.isPull = false; } /** * 接触到石头后确定方向 * @returns */ public getPullDir():number{ let mainSprite = this.ff.mainSprite let pbc = this.node.getComponent(cc.PhysicsBoxCollider) let x0,y0,x1,y1; x0 = this.node.x + pbc.offset.x - pbc.size.width/2 - 10 x1 = this.node.x + pbc.offset.x + pbc.size.width/2 + 10 y0 = this.node.y + pbc.offset.y - pbc.size.height/2 - 20//下 y1 = this.node.y + pbc.offset.y + pbc.size.height/2 + 20//上 let x = mainSprite.node.x let y = mainSprite.node.y if(x > x0 && x < x1){//当前角色在上下2个方向 if(y > y1){ return 1; }else{ return 2; } }else if(y > y0 && y < y1){ if(x < x0){ return 3 }else{ return 4 } } return -1 } private pull(){ let pos = cc.v2() if(this.pullDir == 1){ pos.y = -64 }else if(this.pullDir == 2){ pos.y = 64 }else if(this.pullDir == 3){ pos.x = 64 }else if(this.pullDir == 4){ pos.x = -64 } let tx = this.node.x + pos.x let ty = this.node.y + pos.y if(this.ff.mMap.checkCollision(tx,ty)){ return } cc.tween(this.node).sequence( cc.delayTime(0.6), cc.callFunc(()=>{ if(this.isPull){ cc.tween(this.node).sequence( cc.moveBy(0.5,pos), cc.callFunc(()=>{}) ).start() } }), cc.delayTime(0.5), cc.callFunc(()=>{ if(this.isPull){ this.pull() } }) ).start() } }