123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820 |
- import EventMgr from "./EventMgr";
- import { GameEvent } from "./GameEvent";
- import { GameState } from "./GameState";
- import Hero, { GameInput } from "./Hero";
- import HurtTag from "./HurtTag";
- import Main from "./Main";
- import MonsterHit from "./MonsterHit";
- import ResMgr from "./ResMgr";
- import SoundMgr from "./SoundMgr";
- import Weapon from "./Weapon";
- const { ccclass, property } = cc._decorator;
- // 状态
- export enum MonsterState {
- none = "none",
- born = "born",//刚出生,无敌
- stand = "stand",
- move = "move",
- // run = ''
- attack = "attack",
- hurt = "hurt",
- dead = "dead",
- win = "win",
- }
- // 动画名称
- export enum MonsterAnima {
- // idle = 'idle2',
- walk = 'walk',
- attack1 = 'atk',
- hurt = 'hurt',
- hurtDizzy = 'wolfHurtDizzy',
- dead = 'die',
- win = 'win',
- }
- export enum MonsterFace {
- left = "left",
- right = "right",
- }
- @ccclass
- export default class MonsterBase extends cc.Component {
- // hero状态
- // @property(cc.Node)
- // tmp: cc.Node = null;
- // hero状态
- @property(cc.Node)
- weaponAttach: cc.Node = null;
- @property(cc.Integer)
- hp: number = 5;
- /**无尽模式死亡基础分*/
- @property(cc.Integer)
- private baseScore:number = 0;
- // /**无尽模式死亡得分*/
- // private get score(){
- // return 1;
- // }
- /** 移动方向 1:向右, -1向左, 0不动*/
- protected dirX: number = 0;
- _enemySate: MonsterState = null;
- set state(val: MonsterState){
- // console.error("状态设置:"+val);
- this._enemySate = val;
- }
- get state(){
- return this._enemySate;
- }
- anim: cc.Animation = null;
- protected hitCom:MonsterHit;
- animName: string = null
- // combo: number = 0;
- // 刚体在世界坐标下的(线性速度)
- lv: cc.Vec2 = null
- // moveLeft: boolean = false;
- // moveRight: boolean = false;
- tt: number = 0;
-
- /**生存时间 */
- private lifeTime: number = 0;
- /**上一次攻击时间 */
- private atkLifeTime:number = 0;
- // 玩家节点
- hero: Hero = null;
- protected rigidBody:cc.RigidBody;
- isDestroy: boolean = false;
- private _id: string;
- public get id() {
- return this._id;
- }
-
- /**眩晕计时器 */
- private timeId_dizzy:string;
- // LIFE-CYCLE CALLBACKS:
- public get WPos() {
- return this.node.convertToWorldSpaceAR(cc.Vec2.ZERO);
- }
- protected get walkAnim(): string {
- return MonsterAnima.walk;
- }
- protected speed: number = 150;
- protected nearSpeed: number = 140;
-
- private hurtTag:HurtTag;
- // protected get initSpeed():number{
- // return 80;
- // }
- /**
- * 设置初始位置
- * @param x
- * @param y
- */
- public SetInitPos(x,y){
- this.node.setPosition(cc.v2(x, y))
- }
- private timerId_init;
- onLoad() {
- let me = this;
- me.state = MonsterState.none;
- me.node.active = false;
- me.isInvincible = true;
- this.nearSpeed = Util.random(130,150);
- // this.timerId_init = Ticker.registerDelay(() => {
- me.node.active = true;
- me.init();
- // }, Util.randomArr([0, 100, 200, 300], 1)[0]);//延迟初始化,会导致监听到的消息处理出问题
-
- EventMgr.Instance.add_event_listenner(GameEvent.HeroDie, this, this.OnHeroDie);
- // console.error(this.id+" monster base 开始监听");
- 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();
- // }
- Main.Ins.guideLog("是否冷冻:"+isStop);
- if (isStop) {
- this.stopMove();
- }else{
- this.repuseMove();
- }
- }
-
- protected init() {
- let me = this;
- this._id = this.node.uuid;
- // this.speed = this.initSpeed;//
- this.state = MonsterState.move;
- this.animName = this.walkAnim;// this.walkAnim;;// Anima.idle
- this.hero = Main.Ins.hero;//noeds.filter(e => e.group == 'enemy');// cc.find('Canvas/bg/hero')
- // this.lv = this.node.getComponent(cc.RigidBody).linearVelocity
- this.anim = this.node.getChildByName('body').getComponent(cc.Animation);
- this.hitCom = this.node.getChildByName('body').getComponent(MonsterHit);
- this.faceHero();
- this.anim.on(cc.Animation.EventType.FINISHED, this.onFinishAnima, this)
- // this.moveLeft = false;
- // this.enemyAction();
- this.rigidBody = this.node.getComponent(cc.RigidBody);
- // this.weapon.node.active = true;
- let hurtTagNode:cc.Node= cc.instantiate(ResMgr.Instance.getAsset("LifePrefab", "hurtTag"));
- hurtTagNode.parent = this.node;
- this.hurtTag = hurtTagNode.getComponent(HurtTag);
- this.hurtTag.init(false);
- }
- /**英雄必杀 */
- OnHeroSuperSkill() {
-
- // console.error(this.id+" 收到大招0 monster base");
- this.hurt(Math.random()>0.5,true,false,true);
- }
- protected OnHeroDie() {
- this.logErr(" enemy =英雄死了");
- let me = this;
- if (this.state == MonsterState.dead) { return; }
-
- this.state = MonsterState.win;
- if (!this.node) { return; }
- // 静止
- this.dirX = 0;
- this.setSpeed();
- // Ticker.registerDelay(() => {
- me.setAnima(MonsterAnima.win);
- // }, 2000);
- }
- onDestroy() {
- this.removeListerner();
- }
- onFinishAnima(e: string, data: cc.AnimationState) {
- let aniName = data.name;
- this.logErr("动画结束:" + aniName);
- if (aniName == MonsterAnima.attack1)//||aniName == MonsterAnima.hurtDizzy)
- {
- // 继续移动
- this.state = MonsterState.move
- // 清空记录的动画,避免不能连续攻击
- this.animName = null;
- }
- }
- // start () {}
- tickFun(dt: number) {
- if (this.isStop) {return;}
- if (Main.Ins.State != GameState.inGame && Main.Ins.State != GameState.inGuide) { return; }
- if (this.state == MonsterState.none) {
- return;
- }
- if (this.hp == 0 || this.state == MonsterState.win) {
- return;
- }
- this.lifeTime +=dt;
- this.updateAction(dt)
- }
- protected updateAction(dt: number){
- this.tt += dt;
- if (this.tt >= 0.1 && (this.state == MonsterState.move || this.state == MonsterState.stand)) {
- this.enemyAction();
- this.tt = 0;
- }
- if (this.state == MonsterState.move) {
- this.onMove()
- } else if (this.state == MonsterState.attack) {
- this.onAttack()
- } else if (this.state == MonsterState.stand) {
- this.onStand()
- }
- }
- protected get heroDist() {
- let p_position = this.hero.node.position
- let e_position = this.node.position
- let distance = p_position.x - e_position.x;// cc.Vec2.distance(p_position, e_position)
- return distance;
- }
- protected get heroDisAbsolut() {
- return Math.abs(this.heroDist);
- }
- faceHero() {
- // let distance = this.heroDist;
- // console.error("faceHero:"+distance);
- let scaleX = Math.abs(this.node.scaleX)
- if (this.monsteFace == MonsterFace.left) {
- this.node.scaleX = -scaleX
- } else {
- this.node.scaleX = scaleX
- }
- }
- moveToHero() {
- }
- protected monsteFace;
- private time:number=0;
- // 敌人行为
- protected enemyAction() {
- let distance = this.heroDist;
- this.monsteFace = distance > 0 ? MonsterFace.right : MonsterFace.left;
-
-
- // // x相隔距离
- if (Math.abs(distance) <= 130) {
- // cc.log('attack')
- // 先调整方向, 面向玩家
- // // 静止
- // this.changeDirection(0)
-
- this.dirX = 0;
- this.setSpeed();
- if (Main.Ins.TryGuideStep(1)|| Main.Ins.TryGuideStep(2)) {
-
- Main.Ins.guideLog("开始引导-2");
- // this.state = MonsterState.stand;
- EventMgr.Instance.dispatch_event(GameEvent.GuideStepWolfStop,true);
- if (Main.Ins.TryGuideStep(1)) {
- Main.Ins.BallonGuide.showRight(true);
- Main.Ins.SetGuideIptPermit(GameInput.rightClick);
- }else if (Main.Ins.TryGuideStep(2)) {
- Main.Ins.BallonGuide.showLeft(true);
- Main.Ins.SetGuideIptPermit(GameInput.leftClick);
- }
- // Ticker.pause();
- // Main.Ins.guideView.show("右踩进行攻击",2,function(){
- // this.creatWolf(this.enermyNames[0],false);
- // }.bind(this));
-
-
- return;
- }
- if (Math.random() > 0.8 && this.lifeTime - this.atkLifeTime > 1.5) //两次攻击事件
- {
- this.state = MonsterState.attack;
- this.atkLifeTime = this.lifeTime;
- } else {
- this.state = MonsterState.stand;
- }
- } else //if (distance <= 150)
- {
- this.state = MonsterState.move;
- // cc.log('追击')
- // this.changeDirection(v.x)
- }
- // else{
- // cc.log('随机走 巡逻')
- // this.changeDirection(Math.random() > 0.5 ? 1 : -1)
- // }
- }
- // 移动
- onMove(): void {
- if (this.isStop ) {
- return;
- }
- let scaleX = Math.abs(this.node.getChildByName('body').scaleX)
- let annima = this.animName
- if (this.monsteFace == MonsterFace.left) {
- // <--
- this.dirX = -1
- this.node.scaleX = -scaleX
- annima = this.walkAnim;
- } else {
- this.dirX = 1
- this.node.scaleX = scaleX
- annima = this.walkAnim;
- }
- if (this.heroDisAbsolut < 180) {
- // this.sp.x = 0.7;//this.nearSpeed * this.sp.x;
- this.setSpeed(this.nearSpeed)
- } else {
- this.setSpeed()
- }
- // else {
- // this.sp.x = 0
- // annima = this.walkAnim;
- // }
- // 修改动画
- this.setAnima(annima)
- }
- onStand() {
- this.setAnima(this.walkAnim);
- if (this.state == MonsterState.stand) {
- return;
- }
- this.dirX = 0;
- this.setSpeed()
- // 修改动画
- this.setAnima(this.walkAnim);
- }
- // 攻击
- onAttack(): void {
- // return;
- this.dirX = 0
- this.setSpeed()
- let random = Math.random();
- // this.logErr("攻击:"+random);
- if (random < 0.3) {
- // 修改动画
- this.setAnima(MonsterAnima.attack1)
- }
- }
- // 改变速度(向量)
- setSpeed(forceSpeed = undefined): void {
- // console.error("setSpeed");
- if (!this.rigidBody) {return;}
- // 刚体
- // 刚体的线性速度
- this.lv = this.rigidBody.linearVelocity
- // if (forceSpeed) {
-
- // this.lv.x = forceSpeed;
- // }else{
- // 速度和方向
- if (this.dirX) {
- this.lv.x = this.dirX * (forceSpeed ? forceSpeed : this.speed);
- } else {
- this.lv.x = 0;
- }
- // }
- // 速度赋值
- this.rigidBody.linearVelocity = this.lv;
- }
- /**
- * 播放新动画
- * @param animiat
- */
- setAnima(animiat: string): void {
- if (this.animName == MonsterAnima.win) { return; }
- if (!animiat || this.animName == animiat) { return }
- // cc.log('动画改变 old {}, new {}',this.annima, animiat)
- this.animName = animiat
- // 播放新动画
- this.logErr("当前播放动画:" + animiat,true);
- this.setShowHurtWeapon(animiat == this.walkAnim || animiat == MonsterAnima.attack1);
- if (!this.anim) {
- this.logErr(this,true);
- }
- this.anim.play(animiat);
- }
- /**
- * 被武器击中后显示武器(正式版不需要此功能,注释掉)
- * @param isShow
- */
- private setShowHurtWeapon(isShow) {
- if (this.hurtWeapon) {
- // this.hurtWeapon.active = isShow;
- }
- }
- /**
- * 受击
- * @param hurtLeft true:左侧受击
- * @param isHeavy 是否重击(重击击退)
- * @param weaponHurt 是否武器攻击
- * @param mustKill 必杀
- * @returns
- */
- hurt(hurtLeft:boolean, isHeavy:boolean, weaponHurt:boolean,mustKill:boolean): boolean {
- // console.error(this.id+" 收到大招 1 monster base->isInvincible:"+this.isInvincible+" mustKill:"+mustKill);
- if (this.isInvincible && !mustKill) { return false; }
- // // 速度为0
- // this.sp.x = 0
- // this.setSpeed()
- // console.error(this.id+" 收到大招 2 monster base:"+this.state);
- // if (this.hp == 0) { return false; }
- if (this.state == MonsterState.dead||this.state == MonsterState.win) { return false; }
- this.onHurt(hurtLeft,isHeavy,weaponHurt,mustKill);
- return true;
- }
- protected onHurt(hurtLeft:boolean, isHeavy:boolean, weaponHurt:boolean,mustKill:boolean) {
- console.log(this.hp);
-
- let me = this;
- this.hp--;
- // cc.log('current hp is', this.hp)
- // console.error("狼受击音效");
- SoundMgr.Instance.play_effect("飞刀击中狼");
- this.onHurtSpeed();
- // this.changeDirection(0);
- EventMgr.Instance.dispatch_event(GameEvent.CamShake, 300);
- // 速度赋值
- // this.rigidBody.linearVelocity.x = 0;
- if (mustKill) {
- this.hp=0;
- }
- // console.error(this.id+" 收到大招 3 monster base ->mustKill:"+mustKill+" hp:"+this.hp);
- // this.log('hurt......' + this.hp);
- if (this.hp <= 0) {
- this.onHurtDie();
- } else {
- // if (!isWeapon) {
- this.onHurtStillLive(isHeavy,weaponHurt);
- }
- this.hurtTag.play(hurtLeft);
- }
- protected onHurtSpeed(){
- this.dirX = 0;
- this.setSpeed();
- }
- protected onHurtDie(){
- let me = this;
- this.logErr("onHurtDie",true);
- this.removeListerner();
- if (!Main.Ins.IsGuide) {//新手引导掉落武器会出问题
- this.dropWeapon();
- }
- Main.Ins.EnemyCount--;
- this.state = MonsterState.dead;
- this.setAnima(MonsterAnima.dead)
- SoundMgr.Instance.play_effect("狼死亡");
- EventMgr.Instance.dispatch_event(GameEvent.MonsterDie,this.baseScore);
- this.rigidBody.linearVelocity = cc.v2(this.monsteFace == MonsterFace.left ? 200 : -200, 400);
-
- // if (this.enemyAni) {
- // this.enemyAni.off(cc.Animation.EventType.FINISHED, this.onFinishAnima, this);
- // }
- this.logErr("怪物销毁",true);
-
- if (Main.Ins.TryGuideStep(1)||Main.Ins.TryGuideStep(2)) {
- Main.Ins.guideLog("开始引导-2");
- Main.Ins.SetGuideIptPermit();
- Main.Ins.BallonGuide.showRight(false);
- Main.Ins.BallonGuide.showLeft(false);
-
- if (Main.Ins.TryGuideStep(1)) {
- Main.Ins.GuideStepEnd(1);
- Main.Ins.Guide2();
- }else if (Main.Ins.TryGuideStep(2)) {
- Main.Ins.GuideStepEnd(2);
- Main.Ins.Guide3();
- }
- }else if (Main.Ins.GuideStep == 3) {
-
- Main.Ins.GuideStepEnd(3);
- Main.Ins.SetGuideIptPermit();
- Main.Ins.Guide4();
- }
- }
-
- protected onHurtStillLive(isHeavy, weaponHurt){
- let me = this;
- this.state = MonsterState.hurt;
- if (isHeavy || weaponHurt)
- {
- Ticker.unregisterDelay(me.timeId_dizzy);
- this.setAnima(MonsterAnima.hurt);
- this.rigidBody.linearVelocity = cc.v2(this.monsteFace == MonsterFace.left ? 200 : -200, 400);
- } else {
- Ticker.unregisterDelay(me.timeId_dizzy);
- // console.error("眩晕");
- //普通受击原地眩晕1秒
-
- this.setAnima(MonsterAnima.hurtDizzy);
- me.timeId_dizzy = Ticker.registerDelay(() => {
- // console.error("眩晕结束");
- me.state = MonsterState.move;
- }, 300);
- }
- }
- /**被武器击中*/
- hurtWithWeapon( weapon: Weapon): boolean {
- // console.error("狼被武器击中");
- // console.error(weapon);
-
- let eWPosX = weapon.WPos.x;
- let hWposX = this.WPos.x;
- if (this.hurt(eWPosX <hWposX,false,true,false)) {
- if (this.state != MonsterState.dead) {
- // if (weapon.IsAttatch) {
- // this.hurtWeapon = weapon.Body();
- // this.hurtWeapon.parent = this.weaponAttach;
- // this.hurtWeapon.position = cc.v3(0, 0, 0);
- // this.hurtWeapon.angle = 0;
- // weapon.node.destroy();
- // }
- }
- return true;
- } else {
- return false;
- }
- }
- private hurtWeapon: cc.Node;
- public isDead() {
- return this.state == MonsterState.dead;
- }
- // private heroInLeft:boolean = false;
- /**
- * 掉落武器
- */
- protected dropWeapon() {
- // if (Math.random() < 0.3) {
- // let wPos = this.node.convertToWorldSpaceAR(cc.v2(100, 150));
- // this.logErr("掉落武器:" + this.monsteFace + " wPos:" + wPos);
- // // this.tmp.parent = this.node.parent;
- // // let lPos = this.tmp.parent.convertToNodeSpaceAR(wPos);
- // // this.tmp.position = cc.v3(lPos.x,lPos.y,0);
- // // this.tmp.active = true;
- // let weapon = cc.instantiate(this.weaponPrefab).getComponent(Weapon);
- // weapon.node.parent = this.node.parent;
- // weapon.SetWPos(wPos);
- // weapon.Drop();
- // }
- }
- onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
- this.logErr("onBeginContact :" + otherCollider.node.group + " " + this.isDestroy);
- if (this.isDestroy) { return; }
- let otherGroup = otherCollider.node.group;
- if (otherGroup == 'ground') {
- this.logErr("onBeginContact ground:" + this.state);
- if (this.state == MonsterState.born) {
- this.state = MonsterState.move;
- } else if (this.state == MonsterState.hurt) {
- this.state = MonsterState.move;
- } else if (this.state == MonsterState.dead) {
- this.isDestroy = true;
- this.dirX= 0;
- this.setSpeed();
- // this.changeDirection(0);
- Ticker.registerDelay(() => {
- if (selfCollider && selfCollider.node) {
- this.node.destroy();
- }
- }, 1000);
- }
- }
- }
- // 只在两个碰撞体结束接触时被调用一次
- onEndContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
- if (this.isDestroy) { return; }
- let otherGroup = otherCollider.node.group;
- if (otherGroup == 'board') {
- if (this.isInvincible ) {//因为有时候会调用两次
- this.moveInScreen();
- this.isInvincible = false;
- }
- }
- }
- /**出现在屏幕中了*/
- protected moveInScreen(){
- }
- /**无敌 */
- private isInvincible:boolean;
- /**定格*/
- still(){
- //暂止动画
- //暂停物理
- //暂停update
- this.anim.pause();
- // this.rigidBody.
- }
- /**取消定格 */
- cancelStill(){
- this.anim.resume();
- }
- protected removeListerner(){
- Ticker.unregister(this.tickFun,this);
- Ticker.unregisterDelay(this.timeId_dizzy);
- Ticker.unregisterDelay(this.timerId_init);
- // console.error(this.id+" 移除监听");
- EventMgr.Instance.remove_event_listenner(GameEvent.HeroDie, this, this.OnHeroDie);
- EventMgr.Instance.remove_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
-
- EventMgr.Instance.remove_event_listenner(GameEvent.GuideStepWolfStop, this, this.OnGuideStepWolfStop);
- }
- private log(msg) {
- return;
- console.log(this.id+ "enemy--> " + msg)
- }
- private logWarn(msg) {
- return;
- console.warn(this.id+ "enemy--> " + msg)
- }
- private logErr(msg,forceShow = false) {
- if (!forceShow) {
- return;
- }
- return;
- console.error(this.id+ "enemy--> " + msg)
- }
- private isStop: boolean = false;
- private stopDirX: number;
- public stopMove() {
- // console.error("冷冻");
- this.isStop = true;
- this.stopDirX = this.dirX;
- this.dirX = 0
- this.setSpeed();
- }
- public repuseMove(){
- // console.error("恢复移动");
- this.isStop = false;
- this.dirX = this.stopDirX;
- this.setSpeed();
- }
- }
|