chenxin
2020-11-14 4a083f2f3d8baf1c2630b3717d62904edcc8e24a
Assets/Scripts/TowerDefense/Agents/Agent.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using ActionGameFramework.Health;
using Core.Health;
@@ -188,7 +189,9 @@
        /// <summary>
        /// 动画器.
        /// </summary>
        protected Animator mAnim;
        public Animator ActionAnimator;
        protected AnimationClip[] clips;
        protected bool bInDeathAct = false;
@@ -209,6 +212,7 @@
        public bool bInDeathState
        {
            get { return this.bInDeathAct; }
            set { bInDeathAct = value; }
        }
        /// <summary>
@@ -231,20 +235,25 @@
            get { return this.m_NavMeshAgent; }
        }
        /// <summary>
        /// 是否显示目标位置信息
        /// </summary>
        public bool bShowDebugNode = false;
        /// <summary>
        /// 持续寻路,起到寻路成功
        /// </summary>
        protected bool nodeMoveUpdate = false;
        /// <summary>
        /// 代理的类型
        /// </summary>
        public SpawnAgentType AgentType { get; set; } = SpawnAgentType.Normal;
        /// <summary>
        /// 无尽模式小怪对应 endless_enemy表中数据
        /// </summary>
        public endless_enemy EnemyData { get; set; }
        /// <summary>
        /// Accessor to <see cref="m_NavMeshAgent"/>
@@ -273,6 +282,12 @@
        /// </summary>
        public float originalMovementSpeed { get; protected set; }
        /// <summary>
        /// 小怪的动作状态
        /// </summary>
        public AgentActionState ActionState { get; protected set; }
        private string paramName = "AgentActionState";
        /// <summary>
        /// 更新怪物的移动速度。
@@ -293,6 +308,7 @@
        {
            this.configuration.maxHealth = health;
            this.configuration.startingHealth = health;
            this.configuration.SetMaxHealth(health);
            this.configuration.Init();
            this.configuration.SetHealth(health);
@@ -398,43 +414,16 @@
        /// 处理Agent的锁定Buf,播放特效,设置速度为零,并时间到达后重设数据。
        /// </summary>
        /// <param name="binfo"></param>
        protected void SetAgentStopBuff(buffinfo binfo)
        protected virtual void SetAgentStopBuff(buffinfo binfo)
        {
            MoveStopTime = binfo.last / 1000;
            if (WaveLineSelMgr.instanceExists)
            if (!isFrost)
            {
                // 播放特效:
                if (WaveLineSelMgr.instance.bufStopMovePrefab == null) return;
                isFrost = true;
                // 正在播放的话,直接返回.
                if (MoveStopEffect)
                    return;
                GameObject obj = Instantiate(WaveLineSelMgr.instance.bufStopMovePrefab);
                MoveStopEffect = obj.GetComponent<ParticleSystem>();
                if (MoveStopEffect == null)
                    MoveStopEffect = obj.transform.GetChild(0).GetComponent<ParticleSystem>();
                MoveStopEffect.transform.position = this.position;
                MoveStopEffect.Play();
            }
            else if (EndlessWaveLineManager.instanceExists)
            {
                // 播放特效:
                if (EndlessWaveLineManager.instance.bufStopMovePrefab == null) return;
                // 正在播放的话,直接返回.
                if (MoveStopEffect)
                    return;
                GameObject obj = Instantiate(EndlessWaveLineManager.instance.bufStopMovePrefab);
                MoveStopEffect = obj.GetComponent<ParticleSystem>();
                if (MoveStopEffect == null)
                    MoveStopEffect = obj.transform.GetChild(0).GetComponent<ParticleSystem>();
                MoveStopEffect.transform.position = this.position;
                MoveStopEffect.Play();
                if (FrostParticle != null)
                    FrostParticle.Play();
            }
        }
@@ -468,7 +457,7 @@
        {
            // 统一管理器内删除当前的Agent:
            AgentInsManager.instance.removeAgent(this);
            EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessAgentDead, this);
            base.Remove();
            if (m_LevelManager) m_LevelManager.DecrementNumberOfEnemies();
@@ -492,26 +481,32 @@
            timeToPoisonHurt = 0;
            bShieldBreak = false;
            bInDeathAct = false;
            ChangeState(AgentActionState.Move);
            configuration.ClearShieldWall();
            StopFrostParticle();
            //this.SetTargetableMatColor(Color.white);
            // 删除当前停止特效和状态.
            if (MoveStopTime > 0)
            {
                if (MoveStopEffect)
                {
                    MoveStopEffect.Stop();
                    Destroy(MoveStopEffect);
                    MoveStopEffect = null;
                }
                MoveStopTime = 0.0f;
            }
            // 停止DoTween动画.
            this.transform.DOKill();
            Poolable.TryPool(gameObject);
        }
        private void StopFrostParticle()
        {
            if (isFrost)
            {
                isFrost = false;
                if (FrostParticle != null)
                    FrostParticle.Stop();
            }
        }
        /// <summary>   
@@ -712,6 +707,7 @@
        /// </summary>
        protected override void Awake()
        {
            //Debug.Log("哪里生成的?");
            base.Awake();
            LazyLoad();
            m_NavMeshAgent.enabled = false;
@@ -734,12 +730,7 @@
                MoveStopTime -= Time.deltaTime;
                if (MoveStopTime <= 0)
                {
                    if (MoveStopEffect)
                    {
                        MoveStopEffect.Stop();
                        Destroy(MoveStopEffect);
                        MoveStopEffect = null;
                    }
                    StopFrostParticle();
                    MoveStopTime = 0;
                }
                else
@@ -753,6 +744,24 @@
            }
            float finalSpeed = fMoveSpeed * (1 - speedSlowRate);
            if (speedSlowRate > 0 && !isSlowDown)
            {
                isSlowDown = true;
                if (SlowDownParticle != null)
                    SlowDownParticle.Play();
            }
            else if (speedSlowRate == 0 && isSlowDown)
            {
                isSlowDown = false;
                if (SlowDownParticle != null)
                {
                    SlowDownParticle.Stop();
                    SlowDownParticle.Clear();
                }
            }
            Vector3 curPos = m_NavMeshAgent.transform.position;
            bool swithNode = false;
@@ -819,38 +828,67 @@
            return;
        }
        float atime = 0;
        /// <summary>
        /// 更新动作信息.
        /// </summary>
        /// <returns></returns>
        protected void UpdateAction()
        {
            if (!mAnim) return;
            AnimatorStateInfo stateInfo = ActionAnimator.GetCurrentAnimatorStateInfo(0);
            AnimatorStateInfo stateinfo = mAnim.GetCurrentAnimatorStateInfo(0);
            switch (ActionState)
            {
                case AgentActionState.Move:
                    break;
                case AgentActionState.GetHit:
                    if (stateInfo.normalizedTime >= 1f)
                        ChangeState(AgentActionState.Move);
                    break;
                case AgentActionState.Attack:
                    if (stateInfo.normalizedTime >= 1f)
                    {
                        if (isDead)
                        {
                            AgentInsManager.instance.removeAgent(this);
                            Remove();
                        }
                    }
                    break;
                case AgentActionState.Death:
                    if (stateInfo.normalizedTime >= 1f)
                        Remove();
                    break;
            }
        }
            if (stateinfo.IsName("Die") && (stateinfo.normalizedTime >= 0.7f))
            {
                this.Remove();
                return;
            }
            else if (stateinfo.IsName("GetHit") && (stateinfo.normalizedTime >= 0.7f))
            {
                mAnim.SetBool("GetHit", false);
            }
            else if (stateinfo.IsName("Attack") && (stateinfo.normalizedTime >= 0.9f))
            {
                if (this.healthVal <= 0.1)
                {
                    // 统一管理器内删除当前的Agent:
                    AgentInsManager.instance.removeAgent(this);
                    this.Remove();
                    Debug.Log("删除多余的攻击Agent.");
                    return;
                }
            }
        private void ChangeState(AgentActionState state)
        {
            if (state == ActionState) return;
            return;
            ActionState = state;
            ActionAnimator.SetInteger(paramName, (int)state);
        }
        /// <summary>
        /// 检查自身血量
        /// </summary>
        private void CheckHealth()
        {
            if (this.healthVal <= 0.1)
            {
                Die();
                //Debug.Log("删除多余的攻击Agent.");
            }
        }
        private void Die()
        {
            // 统一管理器内删除当前的Agent:
            AgentInsManager.instance.removeAgent(this);
            this.Remove();
        }
        /// <summary>
@@ -858,9 +896,10 @@
        /// Reset destination when path is stale
        /// River: 更新的时候,加入离最终目标的距离数据.
        /// </summary>
        protected virtual void Update()
        protected virtual void LateUpdate()
        {
            this.UpdateAction();
            HandleShieldWall();
            // 处理死亡状态了,不必再移动:
            if (bInDeathAct || !CanMove) return;
@@ -874,6 +913,30 @@
            // 计算中毒相关.
            if ((poisonTimes > 0) && (poisonHurt > 0))
                updatePoison(Time.deltaTime);
        }
        protected virtual void Update()
        {
        }
        /// <summary>
        /// 处理魔法护盾血量值
        /// </summary>
        protected void HandleShieldWall()
        {
            if (configuration.IsExistShieldWall)
            {
                if (configuration.ShieldWallCurrentHealth <= 0.00001f)
                    configuration.IsExistShieldWall = false;
                else if (configuration.ShieldWallEffectiveTime > 0.00001f)
                {
                    if (configuration.ShieldWallRemainTime <= 0.00001f)
                        configuration.IsExistShieldWall = false;
                    else
                        configuration.ShieldWallRemainTime -= Time.deltaTime;
                }
            }
        }
        /// <summary>
@@ -959,6 +1022,7 @@
            return tmpDis;
        }
        protected ChangeMat changeMat;
        /// <summary>
        /// This is a lazy way of caching several components utilised by the Agent
        /// </summary>
@@ -976,15 +1040,6 @@
            }
            if (!endlessLevelManager)
                endlessLevelManager = EndlessLevelManager.instance;
            if (mAnim == null)
            {
                foreach (Transform t in transform.GetComponentsInChildren<Transform>())
                {
                    if (t.name == "Model")
                        mAnim = t.GetComponent<Animator>();
                }
            }
        }
        /// <summary>
@@ -992,54 +1047,45 @@
        /// </summary>
        public void PlayOnHit()
        {
            if (mAnim)
                mAnim.SetBool("GetHit", true);
            ChangeState(AgentActionState.GetHit);
        }
        public void PlayAttack()
        {
            if (mAnim)
                mAnim.SetBool("Attack", true);
            AudioSourceManager.Ins.Play(AudioEnum.AttackTower);
            ChangeState(AgentActionState.Attack);
        }
        public override void PlayDeath()
        {
            if (mAnim)
            if (isPoison)
            {
                if (isPoison)
                isPoison = false;
                // 移除Agent身上的中毒特效,并播放一个中毒效果消失的特效
                if (PoisonParticle != null)
                {
                    isPoison = false;
                    // 移除Agent身上的中毒特效,并播放一个中毒效果消失的特效
                    if (PoisonParticle != null)
                    {
                        PoisonParticle.Clear();
                        PoisonParticle.Stop();
                        if (PoisonEndParticle != null)
                            PoisonEndParticle.Play();
                    }
                    PoisonParticle.Stop();
                    PoisonParticle.Clear();
                    if (PoisonEndParticle != null)
                        PoisonEndParticle.Play();
                }
                mAnim.SetBool("Die", true);
                // 统一管理器内删除当前的Agent:
                AgentInsManager.instance.removeAgent(this);
                bInDeathAct = true;
            }
            else
            if (isSlowDown)
            {
                this.Remove();
            }
        }
                isSlowDown = false;
        IEnumerator WaitDeathStop()
        {
            yield return null;
            AnimatorStateInfo stateinfo = mAnim.GetCurrentAnimatorStateInfo(0);
            if (stateinfo.IsName("Die") && (stateinfo.normalizedTime >= 1.0f))
            {
                Debug.Log("死亡动画播放完成,开始处理.");
                this.Remove();
                if (SlowDownParticle != null)
                {
                    SlowDownParticle.Stop();
                    SlowDownParticle.Clear();
                }
            }
            ChangeState(AgentActionState.Death);
            // 统一管理器内删除当前的Agent:
            AgentInsManager.instance.removeAgent(this);
            bInDeathAct = true;
        }
        /// <summary>