import EventMgr from "./EventMgr"; import { GameEvent } from "./GameEvent"; import Hero, { GameInput } from "./Hero"; import HeroHit from "./HeroHit"; import Main from "./Main"; import MonsterBase from "./MonsterBase"; import { WeaponState } from "./WeaponState"; import { WeaponType } from "./WeaponType"; const {ccclass, property} = cc._decorator; // 动画名称 enum Anima { /**武器被英雄手持 */ holdInHero = 'holdInHero', /**武器扔出去路上 */ move = 'move', /**击中在敌人身上 */ hitInEnemy = 'hitInEnemy', /**武器掉落 */ drop = 'drop', } @ccclass export default class Weapon extends cc.Component { @property(cc.Node) canAttachTag: cc.Node = null; @property(cc.Node) warnTag: cc.Node = null; private state:WeaponState; public get State():WeaponState { return this.state; } public set State(val:WeaponState) { // console.error("weapon state:"+val); this.state = val; } protected weaponType:WeaponType = WeaponType.throw; public get WeaponType():WeaponType { return this.weaponType; } public set WeaponType(val:WeaponType) { this.weaponType = val; } protected canPick:boolean; public get CanPick():boolean { return this.canPick; } protected isSafe:boolean; public get IsSafe():boolean { return this.isSafe; } @property(cc.Animation) animCom: cc.Animation = null; @property(cc.RigidBody) private rigidBody:cc.RigidBody = null; annima:string = null private speed:number = 400; // 玩家节点 playerNode: cc.Node = null; private parentId:string; public get WPos(){ return this.node.convertToWorldSpaceAR(cc.Vec2.ZERO); } protected get handAnim():string { return Anima.holdInHero.toString(); } protected get dropAnim():string { return Anima.drop.toString(); } /** * 擊中需要附在敵人臉上 */ public IsAttatch: boolean = true; // LIFE-CYCLE CALLBACKS: /**运动方向 -1:左,1:右 */ private moveDir:number; protected onLoad () { this.State = WeaponState.none; this.node.scale = 1.3; EventMgr.Instance.add_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill); Ticker.register(this.tickFun,this); if (Main.Ins.IsGuide) { EventMgr.Instance.add_event_listenner(GameEvent.GuideStepWolfStop, this, this.OnGuideStepWolfStop); } } OnGuideStepWolfStop(GuideStepEnd: GameEvent, isStop) { // if (guide == 5) { // this.stopMove(); // } if (isStop) { this.stopMove(); }else{ this.repuseMove(); } } private isStop: boolean = false; private stopSpeed: number; private stopGravityScale:number; public stopMove() { this.isStop = true; this.stopGravityScale = this.rigidBody.gravityScale; this.stopSpeed = this.rigidBody.linearVelocity.x; this.rigidBody.linearVelocity = cc.v2( 0,0); } public repuseMove() { this.isStop = false; this.rigidBody.gravityScale = this.stopGravityScale; this.rigidBody.linearVelocity = cc.v2(this.stopSpeed, 0); } OnHeroSuperSkill(HeroSuperSkill: GameEvent, arg1: this, OnHeroSuperSkill: any) { this.node.destroy(); } onDestroy() { this.logErr("武器 销毁"); EventMgr.Instance.remove_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill); EventMgr.Instance.remove_event_listenner(GameEvent.GuideStepWolfStop, this, this.OnGuideStepWolfStop); Ticker.unregister(this.tickFun,this); } /** * * @returns 扔出来的武器归属 */ public IsHero():boolean{ return this.State==WeaponState.isHeroThrow; } /** * 扔出来的武器归属 * @returns */ public get IsMonster(): boolean { return this.State == WeaponState.isMonsterThrow; } // start () {} private tt: number = 0; private checkSameWolfAtk: boolean = false; tickFun(dt: number) { if (this.isStop) { return;} if (this.IsMonster) { this.tt += dt; if (this.tt > 0.2) { this.tt = 0; if (!this.checkSameWolfAtk) {//击中同类检测 if (this.moveDir < 0 && this.node.position.x < 0 || this.moveDir > 0 && this.node.position.x > 0 ) {//超过左边半屏幕,可以攻击同种类狼 // // console.error("改变分组:"+this.State+" "+this.IsMonster); this.checkSameWolfAtk = true; this.node.group = "heroWeapon"; this.node.active = false; this.node.active = true; } } let p_position = Main.Ins.hero.node.position; let distance = Math.abs(p_position.x - this.node.position.x);// cc.Vec2.distance(p_position, e_position); if (distance <250) { if (Main.Ins.TryGuideStep(3) && Main.Ins.GuideSteping!=3) { Main.Ins.BallonGuide.showTop(true,"跳跃躲避攻击"); Main.Ins.BallonGuide.showBottom(true,"蹲下躲避攻击"); EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,true); Main.Ins.SetGuideIptPermit(GameInput.up,GameInput.down); // Main.Ins.pauseGame(); // Main.Ins.guideView.show("跳跃或蹲下躲避攻击"); } } // if (distance < 200) { // this.canPickShow(); // } else if (this.annima != this.dropAnim) { if (this.moveDir < 0 && this.node.position.x > 30 || this.moveDir > 0 && this.node.position.x < -30) { this.dangerShow(); } else if (this.state!=WeaponState.heroHold){ this.safeShow(); } } else { if (distance < 200) { this.canPickShow(); } } } } } /** * 播放新动画 * @param animiat */ setAnima(animiat: string):void { if(!animiat || this.annima == animiat) { return } // cc.log('动画改变 old {}, new {}',this.annima, animiat) this.annima = animiat // 播放新动画 this.log('当前播放动画:' + animiat) this.animCom.play(animiat) } /** * 掉落 * @param wPos */ public Drop(wPos) { let me = this; this.SetWPos(wPos); this.node.group = "monsterWeapon"; this.node.active = false; this.node.active = true; this.canPick = false; this.isSafe = false; // console.error(" weapon group:"+this.node.group); this.rigidBody.gravityScale = 1; this.rigidBody.linearVelocity = cc.v2(0, 550); Ticker.registerDelay(() => { me.State = WeaponState.drop;//晚点设置状态,以防刚出来就被捡走,玩家搞不清 }, 500); this.setAnima(this.dropAnim); let p_position = Main.Ins.hero.node.position; let distance = Math.abs(p_position.x - this.node.position.x);// cc.Vec2.distance(p_position, e_position); /**这里为什么要加这个逻辑?*/ if (distance < 200) { this.canPickShow(); } } // 碰撞回调 onCollisionEnter(other: cc.BoxCollider, self: cc.BoxCollider) { this.logErr("武器 onCollisionEnter: " + other.node.group+" self group"+self.node.group,true); if (other.node.group == 'hero' ) {//碰到英雄 if (this.isSafe) { return; } let heroHit = other.node.getComponent(HeroHit); if (!heroHit) {return;} let hero = heroHit.node.parent.getComponent(Hero); if ( other.tag == 1 ) { if (hero.AtkWeapon(this)) //武器被击飞了 { return; } if (this.state == WeaponState.drop) //判断并捡起武器 { if (hero.OnHoldWeapon(this)) { this.setAnima(this.handAnim); this.rigidBody.linearVelocity = cc.v2(0,0); this.rigidBody.gravityScale = 0; this.canAttachTag.active = false; this.warnTag.active = false; } return; } // if (this.canPick && !Main.Ins.IsGuide) { // this.Drop(this.WPos); // } } // // if (other.size.width ==0 || other.size.height ==0 ) { return;} // if ( other.tag == 1 && this.canPick) //判断捡起武器 // { // let hero = heroHit.node.parent.getComponent(Hero); // if (!hero) {return;} // if (hero.OnHoldWeapon(this)) { // this.setAnima(this.handAnim); // this.rigidBody.linearVelocity = cc.v2(0,0); // this.rigidBody.gravityScale = 0; // this.canAttachTag.active = false; // this.warnTag.active = false; // } // } else if (this.State == WeaponState.isMonsterThrow && other.tag == 2) { heroHit.onHurt(this.WPos.x < heroHit.WPos.x,this); // this.node.destroy();//击中了英雄,会转为掉落状态 } }else if (this.State == WeaponState.isHeroThrow ||this.State == WeaponState.isMonsterThrow && other.node.group == 'enemy') {//碰到怪物 let enemy = other.node.parent.getComponent(MonsterBase); if (enemy.id!=this.parentId) {//攻击生效 if (enemy.hurtWithWeapon(this)) { this.HitWolf(!enemy.isDead()); } } } } public Body():cc.Node{ return this.animCom.node; } SetWPos(wPos){ let localPos = this.node.parent.convertToNodeSpaceAR(wPos); this.node.position =cc.v3(localPos.x,localPos.y,0); } public HeroHold(){ this.State = WeaponState.heroHold; this.isSafe = false; this.canAttachTag.active = false; this.warnTag.active = false; } public HeroThrow(isLeft,parentId){ this.logErr("HeroThrow"); this.State = WeaponState.isHeroThrow; this.parentId = parentId; this.setAnima(Anima.move); this.moveDir = isLeft?-1:1; this.rigidBody.linearVelocity = cc.v2( this.moveDir*this.speed,0); this.canAttachTag.active = false; this.warnTag.active = false; } public MonstThrow(isLeft,parentId){ this.logErr("MonstThrow"); this.State = WeaponState.isMonsterThrow; this.parentId = parentId; this.rigidBody.gravityScale = 0; this.dangerShow(); this.setAnima(Anima.move); this.moveDir = isLeft ? -1 : 1; this.rigidBody.linearVelocity = cc.v2( this.moveDir * this.speed, 0); } /**击中怪物 */ public HitWolf(isAttatch){ // return; this.logErr("击中怪物"); // SoundMgr.Instance.play_effect("飞刀击中狼"); // if (isAttatch) { // this.setAnima(Anima.hitInEnemy); // this.State = State.hitInEnemy; // this.rigidBody.linearVelocity = cc.v2(0,0); // this.node.group = "default"; // this.node.active = false; // this.node.active = true; // }else{ this.node.destroy(); // } } safeShow(){ this.isSafe = true; this.canPick = false; this.canAttachTag.active = true; this.warnTag.active = false; } canPickShow(){ this.canPick = true; this.isSafe = false; this.canAttachTag.active = true; this.warnTag.active = false; } dangerShow(){ this.canPick = false; this.isSafe = false; this.canAttachTag.active = false; this.warnTag.active = true; } /**被英雄的手持武器击中 */ heroAtkWeapon(isLeft:boolean,parentId){ if (isLeft) { //向右 this.moveDir = 1; }else{ this.moveDir = -1; } this.canPick = false; this.canAttachTag.active = false; this.warnTag.active = false; this.speed*=2; this.rigidBody.gravityScale = 0; this.rigidBody.linearVelocity = cc.v2( this.moveDir*this.speed,0); this.canAttachTag.active = false; this.warnTag.active = true; this.node.group = "heroWeapon"; this.node.active = false; this.node.active = true; this.State = WeaponState.isHeroThrow; this.parentId = parentId; } private log(msg){ return; console.log("武器-->"+msg); } private logWarn(msg){ return; console.warn("武器-->"+msg); } private logErr(msg,isShow:boolean = false){ if (!isShow) { return; } return; console.error("武器-->"+msg); } }