import TouchMgr from "../help/TouchMgr"; import EventMgr from "./EventMgr"; import { GameEvent } from "./GameEvent"; import { GameMode } from "./GameMode"; import HeroHit from "./HeroHit"; import HurtTag from "./HurtTag"; import Main from "./Main"; import ResMgr from "./ResMgr"; import { SDKIpt } from "./SDKIpt"; import SoundMgr from "./SoundMgr"; import Weapon from "./Weapon"; import { WeaponType } from "./WeaponType"; // hero状态 enum State { stand = "stand", // standAtk = "standAtk", jump = "jump", // jumpAtk = 4, dun = "dun", // holdWeapon = "holdWeapon", // dunAtk = "dunAtk", die = "die", } // 动画名称 enum Anima { attack1 = 'atk1', attack2 = 'atk2', attack3 = 'atk3', attack4 = 'atk4', die = 'die', dun = 'dun', dun_atk1 = 'dun_atk1', // dun_atk2 = 'dun_atk2', /**蹲 -勾拳 */ dun_hookAtk = "dun_hookAtk", idle = 'idle', jump = "jump", jump_up_atk_sdk = "jump_up_atk_sdk", weapon_atk="weapon_atk", weapon_hold="weapon_hold", superSkill = "superSkill", /**羊面向右,向左倾 */ hurt_left="hurt_left", hurt_right="hurt_right", } // 输入状态 export enum GameInput { none = "none" , leftClick = "leftClick",//左踩MOTION.LEFT up = "up",//跳MOTION.JUMP // leftUp = "leftUp",//跳MOTION.JUMP leftDown = "leftDown",//左下MOTION.LEFT_DOWN rightClick = "rightClick",//右MOTION.RIGHT // rightUp = "rightUp",//跳MOTION.JUMP rightDown = "rightDown",//右下MOTION.RIGHT_DOWN down = "down",//右下MOTION.RIGHT_DOWN } // const Input = {} const {ccclass, property} = cc._decorator; @ccclass export default class Hero extends cc.Component { private weapon: Weapon = null; private hp: number; private maxHp: number; public get Hp(): number { return this.hp; } public set Hp(val:number){ if (val>this.maxHp) {val = this.maxHp;} if (val<0) {val = 0;} if (this.hp!=val) { this.hp = val; EventMgr.Instance.dispatch_event(GameEvent.HeroHpChange,this.hp); } } private maxRage: number; private rage: number = 0; public get Rage(): number { return this.rage; } public set Rage(val:number){ if (val>this.maxRage) {val = this.maxRage;} if (val<0) {val = 0;} if (this.rage!=val) { this.rage = val; EventMgr.Instance.dispatch_event(GameEvent.HeroRageChange,this.rage); } } touchType:string; // 速度 speed: number = 0; wPos: cc.Vec2 = null; // 方向 sp: cc.Vec2 = null; // hero动画 heroAni: cc.Animation = null; private heroHit:HeroHit = null; // hero动画状态 annima:Anima = null // 站立连击 private standAtkCombo: number = 0; // 站立连击 public get IsHeavy(): boolean { if (this.standAtkCombo == 3 || this.annima == Anima.jump_up_atk_sdk || this.annima==Anima.dun_atk1) { // console.error("重击"); } return this.standAtkCombo == 3|| this.annima == Anima.jump_up_atk_sdk || this.annima==Anima.dun_atk1; } // /**蹲着连击 */ // private dunAtkCombo: number = 0; // 刚体在世界坐标下的(线性速度) lv: cc.Vec2 = null; // LIFE-CYCLE CALLBACKS: // private inputState :Input; private rigid:cc.RigidBody; // @property(cc.Animation) // hurtTagAnim: cc.Animation = null; private hurtTag: HurtTag = null; @property(cc.Node) weaponHoldPosNode: cc.Node = null; @property(cc.Node) weaponAtkPosNode: cc.Node = null; @property(cc.Node) doctorWolfRoot: cc.Node = null; private isHeroLeftDir = false; private jump_up_atk_count:number = 0; private jump_up_atk_ing:boolean = false; private comTimerID; private _heroSate:State = State.stand; /** * 输入状态 */ public get heroSate():State{ return this._heroSate; } public set heroSate(val) { let old = this._heroSate ; this._heroSate = val; this.heroStateChange(old,val); } private _id:string; public get id(){ return this._id; } /**击杀狼数量 ,每击杀两只,涨一点怒气*/ private killWolf:number =0; private lastInputState:GameInput = GameInput.none; private _inputState:GameInput = GameInput.none; /**是否正在蹲下攻击:此时屏蔽其他所有动作*/ private isDunAtking:boolean = false; /** * 输入状态 */ public get inputState(): GameInput { return this._inputState; } public set inputState(val) { // console.log("英雄 动作变化1:"+val+" "+this.isDunAtking); if (!Main.Ins.HasGuideIptPermit(val)) { return; } let dunValid = this.isDunAtking && val != GameInput.none && val != GameInput.up; if (dunValid) { return; } // console.log("英雄 动作变化2:"+val); let old = this._inputState; this.lastInputState = old; this._inputState = val; this.inputStateChange(old, val); } init (hp:number) { // cc.director.getPhysicsManager().debugDrawFlags = 1; // console.error("生命值:"+hp); this.superSkillComb = 0; this.speed = 200 this.sp = cc.v2(0, 0) this.maxHp = hp; this.Hp = Main.Ins.IsGuide ? 7 : hp;// 10; this.maxRage = 10; this.Rage = 0; this.rigid = this.node.getComponent(cc.RigidBody); this.heroAni = this.node.getChildByName('body').getComponent(cc.Animation) this.heroHit = this.node.getChildByName('body').getComponent(HeroHit) // // 动画监听 播放结束 this.heroAni.on(cc.Animation.EventType.FINISHED, this.onAnimationFinish, this) this.wPos = this.node.convertToWorldSpaceAR(cc.Vec2.ZERO); this.annima = Anima.idle; this.heroSate = State.stand; let hurtTagNode:cc.Node= cc.instantiate(ResMgr.Instance.getAsset("LifePrefab", "hurtTag")); hurtTagNode.parent = this.node; this.hurtTag = hurtTagNode.getComponent(HurtTag); this.hurtTag.init(true); } start () { // console.error("英雄 start:"+Main.Ins.SDKOpen); if (Main.Ins.SDKOpen) { if (cc.sys.isBrowser) { EventMgr.Instance.add_event_listenner(GameEvent.DebugSDKInput,this,this.OnDebugSDKInput); } EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT); }else{ EventMgr.Instance.add_event_listenner(cc.Node.EventType.TOUCH_START,this,this.ON_TOUCH_START); EventMgr.Instance.add_event_listenner(cc.Node.EventType.TOUCH_MOVE,this,this.ON_TOUCH_MOVE); EventMgr.Instance.add_event_listenner(cc.Node.EventType.TOUCH_END,this,this.ON_TOUCH_END); EventMgr.Instance.add_event_listenner(cc.Node.EventType.TOUCH_CANCEL,this,this.ON_TOUCH_END); } EventMgr.Instance.add_event_listenner(GameEvent.MonsterDie, this, this.OnMonsterDie); } /** 每击杀两只,涨一点怒气*/ OnMonsterDie(MonsterDie: GameEvent, arg1: this, OnMonsterDie: any) { if(this.superSkillComb>=4) return this.killWolf++; if (this.killWolf==2) { this.Rage++; this.killWolf = 0; } } OnDebugSDKInput(DebugSDKInput: GameEvent, ipt: GameInput, OnDebugSDKInput: any) { this.inputState = ipt; } ON_SDK_UI_IPT(SDK_UI_IPT: GameEvent, ipt: SDKIpt, ON_SDK_UI_IPT: any) { // console.error("mainview 收到 sdk 动作:"+ipt+" 界面是否激活:"+this.isActive); // if (!this.isActive) { return; } switch (ipt) { case SDKIpt.hero_down: this.inputState = GameInput.down; break; case SDKIpt.hero_left: this.inputState =GameInput.leftClick; break; case SDKIpt.hero_right://主界面确认进入游戏//y this.inputState =GameInput.rightClick;// break; case SDKIpt.hero_up: this.inputState = GameInput.up; break; default: break; } } // private _onMotionLeftDown(arg0: string, _onMotionLeftDown: any, arg2: this) { // this.inputState =GameInput.leftClick;// GameInput.leftDown; // } // private _onMotionRightDown(arg0: string, _onMotionRightDown: any, arg2: this) { // this.inputState =GameInput.rightClick;// GameInput.rightDown; // } // private _onMotionJump(arg0: string, _onMotionJump: any, arg2: this) { // this.inputState = GameInput.up; // } // private _onMotionDown(arg0: string, _onMotionDown: any, arg2: this) { // this.inputState = GameInput.down; // } // private _onMotionRight(arg0: string, _onMotionRight: any, arg2: this) { // this.inputState = GameInput.rightClick; // } // private _onMotionLeft(arg0: string, _onMotionLeft: any, arg2: this) { // this.inputState = GameInput.leftClick; // } ON_TOUCH_END(TOUCH_END: string, arg1: this, ON_TOUCH_END: any) { let touchRight = this.isTouchRight(); // this.log("ON_TOUCH_END:"+this.touchType+" touchRight:"+touchRight +" now state:"+ this.inputState); if (this.touchType == cc.Node.EventType.TOUCH_START) { this.inputState = touchRight?GameInput.rightClick:GameInput.leftClick; } this.touchType = cc.Node.EventType.TOUCH_END; } ON_TOUCH_MOVE(TOUCH_MOVE: string, arg1: this, ON_TOUCH_MOVE: any) { // this.log("ON_TOUCH_MOVE:"+this.touchType); if (this.touchType == cc.Node.EventType.TOUCH_MOVE) { return; } if (TouchMgr.Instance.getMoveStrength() < 5) { return; } this.touchType = cc.Node.EventType.TOUCH_MOVE; let touchRight = this.isTouchRight(); let moveUp = this.isMoveUp(); // if (touchRight) {//右侧 // this.inputState = moveUp? GameInput.rightUp : GameInput.rightDown; // } else { // this.inputState = moveUp? GameInput.leftUp : GameInput.leftDown; // } this.inputState = GameInput.up; } ON_TOUCH_START(TOUCH_START: string, arg1: this, ON_TOUCH_START: any) { this.touchType = cc.Node.EventType.TOUCH_START; } onDestroy() { // this.heroAni.off(cc.Animation.EventType.FINISHED, this.onAnimationFinish, this); this.removeIptListen(); } private removeIptListen(){ EventMgr.Instance.remove_event_listenner(cc.Node.EventType.TOUCH_START,this,this.ON_TOUCH_START); EventMgr.Instance.remove_event_listenner(cc.Node.EventType.TOUCH_MOVE,this,this.ON_TOUCH_MOVE); EventMgr.Instance.remove_event_listenner(cc.Node.EventType.TOUCH_END,this,this.ON_TOUCH_END); EventMgr.Instance.remove_event_listenner(cc.Node.EventType.TOUCH_CANCEL,this,this.ON_TOUCH_END); EventMgr.Instance.remove_event_listenner(GameEvent.DebugSDKInput,this,this.OnDebugSDKInput); EventMgr.Instance.remove_event_listenner(GameEvent.MonsterDie,this,this.OnMonsterDie); EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT); } /** * 获取触摸左侧还是右侧 * @returns >0?右側 <左側 */ isTouchRight():boolean{ return this.getTouchSide()>0; } getTouchSide():number{ let side; let touchWPos = TouchMgr.Instance.WPos; let touchWPosX = touchWPos.x; let delt = touchWPosX-this.wPos.x; if (Math.abs(delt)<50) { side = 0; }else if (delt<0) { side = -1; }else{ side = 1; } return side; } isMoveUp(){ return TouchMgr.Instance.getMoveDir()<0; } private superSkillComb:number; private timer_out_superSkill:string; /**因为每一次旧的输入状态都会置为 GameInput.none,所以用这个记录真实输入状态*/ private superSkillIptState:GameInput; /** * 输入状态改变 */ inputStateChange(oldIpt: GameInput, newIpt: GameInput) { let me = this; if (newIpt == GameInput.down || newIpt == GameInput.up) { if (Main.Ins.TryGuideStep(3)) { Main.Ins.BallonGuide.showTop(false); Main.Ins.BallonGuide.showBottom(false); // Main.Ins.resumeGame(); EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,false); Main.Ins.SetGuideSteping(3); Main.Ins.SetGuideIptPermit(GameInput.down,GameInput.up,GameInput.leftClick,GameInput.rightClick); } else if (Main.Ins.TryGuideStep(4)) { Main.Ins.BallonGuide.showTop(false); EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,false); Main.Ins.SetGuideSteping(4); // Main.Ins.SetGuideIptPermit(); }else if (Main.Ins.TryGuideStep(5)) { Main.Ins.BallonGuide.showTop(false); EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,false); Main.Ins.GuideStepEnd(5); EventMgr.Instance.dispatch_event(GameEvent.GuideState,false); } } this.error("输入状态更新-> oldIpt: " + oldIpt + " newIpt: " + newIpt); if (newIpt == GameInput.none) {return; } let isHeroLeftDir = this.isHeroLeftDir; if (this.heroSate != State.jump) { if (Main.Ins.SDKOpen) { isHeroLeftDir = newIpt == GameInput.leftClick || newIpt == GameInput.leftDown; } else { isHeroLeftDir = newIpt == GameInput.leftClick || newIpt == GameInput.leftDown || newIpt == GameInput.up; } } if (newIpt == GameInput.up || newIpt == GameInput.down) { isHeroLeftDir = this.isHeroLeftDir; } this.isHeroLeftDir = isHeroLeftDir; switch (newIpt) { case GameInput.leftClick: case GameInput.rightClick: let checkSuperSkill = function () { if (newIpt != me.superSkillIptState && (me.superSkillIptState ==GameInput.leftClick||me.superSkillIptState ==GameInput.rightClick)) { // console.error("superSkillComb:"+me.superSkillComb); if (++me.superSkillComb >= 4 && me.Rage>=me.maxRage) { //放大招 // console.error("放大招1"); me.heroSate = State.jump; return true; } if (me.timer_out_superSkill) { Ticker.unregisterDelay(me.timer_out_superSkill); } me.timer_out_superSkill = Ticker.registerDelay(() => { me.superSkillComb = 0; }, 3000); return false; } me.superSkillComb =1;//记录第一次 return false; } if (this.heroSate == State.stand) { // this.onAttack(); if (!checkSuperSkill()) { this.heroSate = State.stand; } } else if (this.heroSate == State.dun) { if (!checkSuperSkill()) { this.heroSate = State.stand; } } else if (this.heroSate == State.jump) { this.heroSate = State.jump; } break; case GameInput.leftDown: case GameInput.rightDown: case GameInput.down: me.superSkillComb =0;//归零 // this.heroSate = State.dun; if (this.heroSate == State.stand) { // this.onAttack(); this.heroSate = State.dun; } else if (this.heroSate == State.dun) { this.heroSate = State.dun; } else if (this.heroSate == State.jump) { this.heroSate = State.jump; } break; case GameInput.up: // case GameInput.rightUp: me.superSkillComb =0;//归零 if (this.heroSate == State.stand) { // this.onAttack(); this.heroSate = State.jump; } else if (this.heroSate == State.dun) { this.heroSate = State.jump; } else if (this.heroSate == State.jump) { this.heroSate = State.jump; } break; } // console.error("now:"+this.node.scaleX+" new:"+(isHeroLeftDir?-1:1)); this.node.scaleX =isHeroLeftDir?-1:1; this.superSkillIptState = newIpt; this.inputState = GameInput.none; } /** * 英雄状态改变 */ heroStateChange(oldState: State, newState: State) { this.warn("英雄状态更新-> oldState: " + oldState + " newState: " + newState); // if (oldState==State.die) { return; } //更新状态 switch (newState) { case State.die: this.onDieState(oldState); break; case State.dun: this.onDunState(oldState); break; case State.jump: this.onJumpState(oldState); break; case State.stand: this.onStandState(oldState); break; default: break; } } onStandState(oldState:State) { let me = this; let annima = Anima.idle; this.log("onStandState oldState:"+oldState+" inputState:"+this.inputState); if (this.inputState == GameInput.leftClick || this.inputState == GameInput.rightClick) { if (oldState == State.dun) { if (this.isHoldWeapon) { annima = Anima.weapon_atk; this.weaponAtk(); }else{ annima = Anima.dun_hookAtk; } } else if (oldState == State.stand) { // // 播放攻击动画 this.log('即将播放攻击动画 站: ' + this.standAtkCombo); if (this.comTimerID) { Ticker.unregisterDelay(this.comTimerID); } if (this.standAtkCombo == 0) { annima = Anima.attack1 } else if (this.standAtkCombo == 1) { annima = Anima.attack2 } else if (this.standAtkCombo == 2) { annima = Anima.attack3 } else if (this.standAtkCombo == 3) { annima = Anima.attack4 } // else if (this.standAtkCombo == 4) { // annima = Anima.attack5 // } else if (this.standAtkCombo == 5) { // annima = Anima.attack6 // } if (this.isHoldWeapon) {//this.annima == Anima.weapon_hold && annima = Anima.weapon_atk; this.weaponAtk(); } } }else { if (this.isHoldWeapon) { annima = Anima.weapon_hold; this.setWeaponShow(true); } } // 修改动画 this.setAnima(annima) } onDunState(oldState:State){ let annima = Anima.dun; this.log("onDunState oldState:"+oldState+" inputState:"+this.inputState); if (this.inputState == GameInput.leftClick || this.inputState == GameInput.rightClick) { }else if (this.inputState == GameInput.leftDown || this.inputState == GameInput.rightDown|| this.inputState == GameInput.down) { annima = Anima.dun_atk1; // // 播放攻击动画 // this.log('即将播放攻击动画 蹲:' + this.dunAtkCombo); // if (this.dunAtkCombo == 0) { // } else if (this.dunAtkCombo == 1) { // annima = Anima.dun_atk1 // } }else if (this.inputState == GameInput.up)// || this.inputState == GameInput.rightUp) { } this.setWeaponShow(false); // 修改动画 this.setAnima(annima); } /** * 在空中 */ onJumpState(oldState:State) { let annima = Anima.jump; if (this.jump_up_atk_ing) { return; } this.log("onJumpState oldState:"+oldState+" inputState:"+this.inputState); if (oldState == State.dun || oldState == State.stand) { if (this.jump_up_atk_count > 0) { return; } this.jump_up_atk_ing = true; this.jump_up_atk_count++; if (this.superSkillComb>=4) { // console.error("放大招2"); this.Rage = 0; // this.superSkillComb = 0; annima = Anima.superSkill; // this.rigidLineVelocity(0,2000); // this.setAnima(Anima.superSkill); EventMgr.Instance.dispatch_event(GameEvent.SCRENN_COLOR); // Ticker.registerDelay(() => { // SoundMgr.Instance.play_effect("小羊大招"); // EventMgr.Instance.dispatch_event(GameEvent.SDK_SHAKE,600); // }, 400); setTimeout(() => { EventMgr.Instance.dispatch_event(GameEvent.HeroSuperSkill); SoundMgr.Instance.play_effect("小羊大招"); EventMgr.Instance.dispatch_event(GameEvent.SDK_SHAKE,600); }, 400); // Ticker.registerDelay(() => { // }, 400); }else{ annima = Anima.jump_up_atk_sdk; // this.rigid.linearVelocity= cc.v2(0,500);// this.rigidLineVelocity(0,900); // this.error("跳-上击-->"+this.rigid.linearVelocity); } } else if (this.inputState == GameInput.leftDown || this.inputState == GameInput.rightDown|| this.inputState == GameInput.down) { // //往下砸 // annima = Anima.dun; } this.setWeaponShow(false); // 修改动画 this.setAnima(annima) } onDieState(oldState:State){ if ( oldState == State.die) { return; } // 修改动画 this.setAnima(Anima.die); this.setWeaponShow(false); // console.error(" hero ------------------- =英雄死了============================================"); EventMgr.Instance.dispatch_event(GameEvent.HeroDie); } /** * 播放新动画 * @param animiat */ setAnima(animiat: Anima): void { if (!animiat || this.annima == animiat) { return;} // this.log('old {}, new {}',this.annima, animiat) this.annima = animiat // 播放新动画 this.log('当前播放动画:' + animiat); // console.log('当前播放动画:' + animiat); // if (animiat==Anima.jump||animiat==Anima.jump_atk||animiat==Anima.jump_bothSide||animiat==Anima.jump_atk1||animiat==Anima.jump_up_atk_sdk) { if (animiat==Anima.jump_up_atk_sdk) { // console.error("跳起音效"); SoundMgr.Instance.play_effect("小羊跳跃"); }else if(animiat==Anima.dun_atk1){ // console.error("跳起音效"); SoundMgr.Instance.play_effect("小羊蹲下"); } else if (animiat==Anima.dun_hookAtk|| animiat==Anima.attack1|| animiat==Anima.weapon_atk) { SoundMgr.Instance.play_effect("攻击1"); }else if (animiat==Anima.attack2) { SoundMgr.Instance.play_effect("攻击2"); }else if (animiat==Anima.attack3) { SoundMgr.Instance.play_effect("攻击3"); }else if (animiat==Anima.attack4) { SoundMgr.Instance.play_effect("攻击4"); }else if (animiat==Anima.superSkill) { } if (animiat==Anima.jump||animiat==Anima.jump_up_atk_sdk) { this.heroHit.jumpCollider(); } else if (animiat==Anima.dun ||animiat==Anima.dun_atk1) { this.heroHit.dunCollider(); }else if (animiat==Anima.superSkill) { this.heroHit.superSkillCollider(); }else{ this.heroHit.standCollider(); } if (animiat == Anima.dun_atk1) { this.isDunAtking = true; } this.heroAni.play(animiat); } /** * 动画监听事件 * @param e * @param data */e: cc.Event.EventKeyboard onAnimationFinish(e:string, data:cc.AnimationState) { // 重置为静态动画 let animName = data.name; this.isDunAtking = false; this.log('动画结束:'+animName+" standAtkCombo:"+this.standAtkCombo ,true); if (animName == Anima.attack1 || animName == Anima.attack2 || animName == Anima.attack3|| animName == Anima.attack4 ) { this.heroSate = State.stand; // 连击判定 this.standAtkCombo = (this.standAtkCombo + 1) % 4; this.comTimerID= Ticker.registerDelay(() => { if (this.heroSate == State.stand) { this.heroSate = State.stand; } // if (this.heroSate == State.stand) { return; } this.standAtkCombo = 0 }, 1000) }else if (animName == Anima.dun_atk1) { this.heroSate = State.dun; // // 连击判定 // this.dunAtkCombo = (this.dunAtkCombo + 1) % 2; // this.comTimerID=Ticker.registerDelay(()=> { // if (this.heroSate == State.dun) { // this.heroSate = State.dun; // } // // if(this.heroSate == State.dun) { return; } // this.dunAtkCombo = 0 // }, 1000) } else { if ( animName == Anima.jump_up_atk_sdk|| animName == Anima.superSkill) { this.jump_up_atk_ing = false; } this.heroSate = this.heroSate; } } onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) { let otherGroup = otherCollider.node.group; if (otherGroup == 'ground') { if (this.heroSate == State.jump) { this.log("onBeginContact jump:"+this.annima); if (this.annima == Anima.dun) { this.heroSate = State.dun; } else if(this.annima == Anima.jump)//|| this.annima == Anima.superSkill) { this.heroSate = State.stand; } if (this.annima != Anima.superSkill) { this.jump_up_atk_ing = false; this.jump_up_atk_count = 0; } } } } /** * * @param isEnemyInleft 敌人是否在左边 * @param weapon 如果是被武器击中的话,需要掉落武器 * @returns 已经死了,或者在跳跃状态不能受击 */ OnHurt(isEnemyInleft:boolean,weapon:Weapon = null){ if (this.heroSate == State.die ||this.heroSate == State.jump) { return false; } if (weapon) {//被武器击中 let dropX; if (this.isFaceLeft) { dropX = isEnemyInleft?100:-100; }else{ //面向右 dropX = isEnemyInleft?-100:100; } let wPos = this.node.convertToWorldSpaceAR(cc.v2(dropX, 150)); //武器掉落 weapon.Drop(wPos); } SoundMgr.Instance.play_effect("小猪受攻"); if (--this.Hp==0) { if (Main.Ins.IsGuide) { this.Hp = 1; }else{ this.node.scaleX = isEnemyInleft?-1:1; this.heroSate = State.die; this.removeIptListen(); } }else{ if (this.heroSate !=State.dun) { let hurtAnim; if (this.isFaceLeft) { hurtAnim = isEnemyInleft?Anima.hurt_left:Anima.hurt_right; }else{ //面向右 hurtAnim = isEnemyInleft?Anima.hurt_right:Anima.hurt_left; } this.setAnima(hurtAnim); } //加怒气 // this.Rage++; } if (this.Hp < 0) { this.Hp = 0; } this.hurtTag.play(isEnemyInleft); // EventMgr.Instance.dispatch_event(GameEvent.HeroHpChange,this.Hp); EventMgr.Instance.dispatch_event(GameEvent.SDK_SHAKE,400); EventMgr.Instance.dispatch_event(GameEvent.HERO_HURT,1); return true; } /**无尽模式失败 */ forceDie(){ SoundMgr.Instance.play_effect("小猪受攻"); // this.node.scaleX = isEnemyInleft?-1:1; this.heroSate = State.die; this.removeIptListen(); } private isHoldWeapon:boolean = false; /**判断并执行击飞武器 */ public AtkWeapon(weapon: Weapon): boolean { //手上有手持武器,把武器击飞 if (this.isHoldWeapon && this.weapon.WeaponType == WeaponType.hold) { weapon.heroAtkWeapon(this.WPos.x < weapon.WPos.x, this.id); return true; } // else {//手上没有手持武器,先受击,,再掉落武器//再拾起武器 // let eWPosX = weapon.WPos.x; // let hWposX = this.WPos.x; // let weaponInLeft = eWPosX < hWposX; // if (!this.OnHurt(weaponInLeft, weapon) || this.heroSate == State.die) { // } // return; // } return false; } public OnHoldWeapon(weapon:Weapon):boolean{ // this.heroSate = State.holdWeapon; // if (weapon.State != WeaponState.drop) //武器不是出于掉落状态 // { // //手上有手持武器,把武器击飞 // if (this.isHoldWeapon && this.weapon.WeaponType == WeaponType.hold) { // weapon.heroAtkWeapon(this.WPos.x < weapon.WPos.x, this.id); // return; // } else {//手上没有手持武器,先受击,,再掉落武器//再拾起武器 // let eWPosX = weapon.WPos.x; // let hWposX = this.WPos.x; // let weaponInLeft = eWPosX < hWposX; // if (!this.OnHurt(weaponInLeft, weapon) || this.heroSate == State.die) { // } // return; // } // } if (this.isHoldWeapon) { return false; } this.log('获得武器 '); weapon.node.active = false; this.isHoldWeapon = true; weapon.HeroHold(); this.weapon = weapon; this.weapon.node.parent= this.weaponHoldPosNode; this.weapon.node.position = cc.Vec3.ZERO; this.weapon.node.group = "heroWeapon"; // this.weapon.node.active = false; // this.weapon.node.active = true; return true; } private weaponAtk(){ if (this.weapon) { if (this.weapon.WeaponType == WeaponType.throw) { this.throwWeapon(); }else{ // this.bigWeaponAtk(); } } } private throwWeapon(){ this.weapon.node.parent= this.node.parent; this.weapon.node.active = true; let wPos = this.weaponAtkPosNode.convertToWorldSpaceAR(cc.Vec2.ZERO); let lPos = this.weapon.node.parent.convertToNodeSpaceAR(wPos); this.weapon.node.position = cc.v3(lPos.x,lPos.y,0); // return; this.weapon.HeroThrow(this.isFaceLeft,this.id); this.weapon = null; this.isHoldWeapon = false; } /** * 是否大武器击中 * @returns true:是大武器击中;false 不是大武器击中 */ public bigWeaponAtk():boolean{ if (this.weapon && this.weapon.WeaponType == WeaponType.hold) { this.weapon.WeaponType = WeaponType.throw; // let wPos = this.weaponAtkPosNode.convertToWorldSpaceAR(cc.v2(0, 0)); let wPos = this.node.convertToWorldSpaceAR(cc.v2(20, 150)); // this.weapon.node.group = "" this.weapon.node.parent = this.node.parent; this.weapon.Drop(wPos); this.weapon = null; this.isHoldWeapon = false; return true; } return false; } private setWeaponShow(isShow: boolean) { if (this.isHoldWeapon && this.weapon) { if (this.weapon.node) { this.weapon.node.active = isShow; this.weapon.node.parent = this.weaponHoldPosNode; this.weapon.node.position = cc.Vec3.ZERO; } else { this.isHoldWeapon = false; this.weapon = null; } } } private get isFaceLeft(): boolean { // return this.node.scaleX < 0; return this.isHeroLeftDir; } public get WPos(){ return this.node.convertToWorldSpaceAR(cc.Vec2.ZERO); } rigidLineVelocity(x,y){ // console.error("rigidLineVelocity->x:"+x+" y:"+y); this.rigid.linearVelocity = cc.v2(x,y); } public Cure(doctor: cc.Node, hp: number = 1) { let me = this; // console.error("治疗"); doctor.group = 'hero'; doctor.active = false; doctor.active = true; doctor.parent = this.doctorWolfRoot; // .addChild(doctor); doctor.position = cc.v3(0, 0, 0); this.Hp += hp; Ticker.registerDelay(() => { doctor.destroy(); }, 1000); } protected onEnable(): void { if (this.rigid) { // this.rigid.awake = true; this.rigidLineVelocity(0,0); } } protected onDisable(): void { if (this.rigid) { // this.rigid.awake = false; } } private log(msg,isShow = false){ // if (!isShow) {return; } return; console.log("hero-->"+msg) } private warn(msg,isShow = false){ // if (!isShow) {return; } return; console.warn("hero-->"+msg) } private error(msg,isShow = false){ // if (!isShow) {return; } return; console.error("hero-->"+msg) } }