import EventMgr from "./EventMgr"; import { GameEvent } from "./GameEvent"; import { GameState } from "./GameState"; import { GameInput } from "./Hero"; import Main from "./Main"; import Monster1 from "./Monster1"; import { MonsterAnima, MonsterFace, MonsterState } from "./MonsterBase"; import Weapon from "./Weapon"; const {ccclass, property} = cc._decorator; /** * 冲刺农夫狼 */ @ccclass export default class WolfRush extends Monster1 { // hero状态 @property(cc.Node) weaponAtkPosNode: cc.Node = null; // hero状态 @property(cc.Prefab) weaponPrefab: cc.Prefab = null; private rushSpeed: number = 400; /**漫游 速度 */ private wanderSpeed: number = 100; private isRushState: boolean = true; private timer_itv_wander: string; private timer_to_rush: string; private _walkAnim; protected get walkAnim() { return this._walkAnim; } protected init(): void { super.init(); let me = this; this._walkAnim = MonsterAnima.walk; // this.speed = Util.random(100,140); // Ticker.registerDelay(() => { me._walkAnim = "rush";// MonsterAnima.walk; me.speed = me.rushSpeed;// me.initSpeed; this.state = MonsterState.move; // me.rushCheck(); Main.Ins.guideLog("rush-init"); // if (Main.Ins.IsGuide) { // EventMgr.Instance.add_event_listenner(GameEvent.GuideStepEnd, this, this.OnGuideStepEnd); // } // }, 800); } protected start(): void { this.initColor(); } // OnGuideStepEnd(GuideStepEnd: GameEvent, guide) { // if (guide == 4) { // this.repuseMove(); // } // } protected updateAction(dt):void{ if (this.isRushState) //冲锋状态 { if ( Main.Ins.TryGuideStep(4) && Main.Ins.GuideSteping!=4) { if (this.heroDisAbsolut <240) { Main.Ins.BallonGuide.showTop(true,"跳跃躲避攻击"); Main.Ins.SetGuideIptPermit(GameInput.up); EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,true); // this.stopMove(); // Main.Ins.pauseGame(); // Main.Ins.guideView.show("跳跃或蹲下躲避攻击"); } } }else{//普通状态 super.updateAction(dt); } } protected onHurtSpeed(): void { if (!this.isRushState) { super.onHurtSpeed(); } } protected onHurtStillLive(isHeavy: any, weaponHurt: any): void { if (this.isRushState) { if (weaponHurt) { this.changeToNormal(); super.onHurtStillLive(true, weaponHurt); } } else { super.onHurtStillLive(isHeavy, weaponHurt); } } changeToNormal(){ this.isRushState = false; this.speed = this.initSpeed; // /**击退*/ this._walkAnim = MonsterAnima.walk; this.setBlink(false); } protected dropWeapon(): void { // if (Math.random() < 0.3) { if (this.isRushState) { let wPos = this.DropWPos;// this.node.convertToWorldSpaceAR(cc.v2(100, 150)); let weapon = cc.instantiate(this.weaponPrefab).getComponent(Weapon); weapon.node.parent = this.node.parent; weapon.Drop(wPos); } } private get DropWPos(){ return this.node.convertToWorldSpaceAR(cc.v2(100, 150)); } onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) { super.onBeginContact(contact, selfCollider, otherCollider); if (this.isRushState) { if (otherCollider.node.group == 'board') { Main.Ins.guideLog("rush-board"); if (this.rushRound >= 1) { if (Main.Ins.IsGuide) { Main.Ins.SetGuideIptPermit(); Main.Ins.GuideStepEnd(4); Main.Ins.Guide5(); this.node.destroy(); } else { this.changeToNormal(); } } else { this.rushCheck(); } } } } private rushRound: number = 0; rushCheck() { let me = this; if (Main.Ins.State == GameState.win || Main.Ins.State == GameState.fail) { return; } // if (me.dirX == 0) { return; } let monsteFace = this.heroDist > 0 ? MonsterFace.right : MonsterFace.left; me.monsteFace = monsteFace; me.faceHero(); me.dirX = 0; me.setSpeed(); Ticker.unregisterDelay(me.timer_to_rush); me.timer_to_rush = Ticker.registerDelay(() => { if (me.monsteFace == MonsterFace.left) { // <-- me.dirX = -1; } else { me.dirX = 1; } me.rushRound++; me.setSpeed(me.rushSpeed); }, 500); } OnHeroDie() { // super.OnHeroDie(); //开始漫步 let me = this; // this.faceHero this.setAnima(MonsterAnima.walk); Ticker.unregisterDelay(me.timer_to_rush); let wanderFun = function () { me.dirX = me.dirX < 0 ? 1 : -1; let scaleX = Math.abs(me.node.scaleX) if (me.dirX < 0) { me.node.scaleX = -scaleX } else { me.node.scaleX = scaleX } me.setSpeed(me.wanderSpeed); } wanderFun(); me.timer_itv_wander = Ticker.registerDelay(() => { wanderFun(); }, Util.random(1000,3000),0); } private _material; private realVal:number = 0; private maxVal:number = 5; private dir:number = 10; private isBlink:boolean = false; private initColor(){ // 获取材质 if (this.hitCom.node.getComponent(cc.Sprite)) { this._material = this.hitCom.node.getComponent(cc.Sprite).getMaterial(0); if (this._material) { // 设置材质对应的属性 this._material.setProperty("uSize", this.realVal); this._material.setProperty("uGlowColor", cc.color(255,208,75)); Ticker.register(this.blinkTickFun,this); this.setBlink(true); } } } blinkTickFun(dt) { if (!this.isBlink) {return;} this.realVal+=dt * this.dir; if (this.realVal >= this.maxVal || this.realVal <= 0) { this.dir = -this.dir; // if (this.blinkCount-- <= 0) { // this.realVal = 0; // this.isBlink = false; // } } this._material.setProperty("uSize", this.realVal); } private setBlink(isBlink:boolean){ this.isBlink = isBlink; if (!this._material) { this.isBlink = false; } // console.error(this._material) if (!this.isBlink) { this._material.setProperty("uSize", 0); } // console.error("设置 setBlink "+isBlink+" "+ this.isBlink) } onDestroy(){ super.onDestroy(); Ticker.unregisterDelay(this.timer_itv_wander); Ticker.unregisterDelay(this.timer_to_rush); Ticker.unregister(this.blinkTickFun,this); // EventMgr.Instance.remove_event_listenner(GameEvent.GuideStepEnd, this, this.OnGuideStepEnd); super.onDestroy(); } }