import Armature from "./Armature"; import Role from "./Role"; const {ccclass, property} = cc._decorator; /**怪物类 */ @ccclass export default class Monster extends Role { public property_damage:number = 8; public property_enduranceResumeSpeed:number = 6; public onLoad():void{ //初始化骨骼 this.armature =new Armature('armature/monster',this.node); //初始动画 this.armature.playAnimation(Role.AnimationName_Idle,0); //获得目标对手监听,启动目标攻击事件监听 this.node.on(Role.EventType_GetTarget,this.onTargetAttackCallback,this); super.onLoad(); } /**目标攻击监听回调 */ private onTargetAttackCallback():void{ this.target.node.on(Role.EventType_DoAttack,()=>{ let random = Math.random(); if(random<0.5)return;//半概率不躲避 //根据hp和耐力值决定出手策略 let enoughHp = this.property_hp>0.6*this.property_maxHp; let enoughEndurance = this.property_endurance>0.6*this.property_maxEndurance; if(!enoughHp&&enoughEndurance){ this.defense(); }else if(enoughHp&&!enoughEndurance){ if(this.target.direction==1){ this.rightDodge(); }else{ this.leftDodge(); } }else{ if(random<0.65){ this.defense(); }else{ if(this.target.direction==1){ this.rightDodge(); }else{ this.leftDodge(); } } } },this); } /**上一次攻击提示时间 */ private lastAttackTipTime:number = 0; /**下一次攻击名称 */ private nextAttack:string; /**自动闪躲攻击策略 */ private autoDodgeAndAttack():void{ if(this.pausing)return; let random = Math.random(); let now = Date.now(); if(now-this.lastAttackTipTime>400&&this.nextAttack){ if(this.nextAttack=='leftJay'){ this.leftJay(); this.nextAttack = null; }else if(this.nextAttack=='rightJay'){ this.rightJay(); this.nextAttack = null; } }else if(random<0.2&&!this.nextAttack){ if(random<0.05){ this.leftDodge(); }else if(random<0.1){ this.rightDodge(); }else if(random<0.2){ if(random<0.15){ if(now-this.lastAttackTipTime>1000){ this.createMark(1); this.nextAttack = 'leftJay'; this.lastAttackTipTime = now; } }else{ if(now-this.lastAttackTipTime>1000){ this.createMark(-1); this.nextAttack = 'rightJay'; this.lastAttackTipTime = now; } } } } } public update(dt:number):void{ super.update(dt); this.autoDodgeAndAttack(); } /** * 创建预备攻击提示标志 * @param direction 1为左,-1为右 * @param autoDestroy 自动销毁 * @param callback 回调函数 */ private createMark(direction:number):void{ let node = new cc.Node(); node.setScale(3); node.setPosition(this.node.position.add(cc.v2(-250*direction,650))); node.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/mark',cc.SpriteFrame); window.gameMgr.background.addChild(node); node.runAction(cc.sequence(cc.shake(0.4,5,10),cc.callFunc(()=>{ node.destroy(); },this))); cc.audioEngine.playEffect(cc.loader.getRes('audio/ding',cc.AudioClip),false); } }