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;
@@ -12,6 +13,7 @@
using TowerDefense.Economy;
using TowerDefense.Level;
using TowerDefense.Nodes;
using TowerDefense.UI.HUD;
using UnityEngine;
using UnityEngine.AI;
@@ -188,7 +190,9 @@
        /// <summary>
        /// 动画器.
        /// </summary>
        protected Animator mAnim;
        public Animator ActionAnimator;
        protected AnimationClip[] clips;
        protected bool bInDeathAct = false;
@@ -232,12 +236,10 @@
            get { return this.m_NavMeshAgent; }
        }
        /// <summary>
        /// 是否显示目标位置信息
        /// </summary>
        public bool bShowDebugNode = false;
        /// <summary>
        /// 持续寻路,起到寻路成功
@@ -281,6 +283,44 @@
        /// </summary>
        public float originalMovementSpeed { get; protected set; }
        /// <summary>
        /// 小怪的动作状态
        /// </summary>
        public AgentActionState ActionState { get; protected set; }
        private string paramName = "AgentActionState";
        /// <summary>
        /// 外部赋值,唯一标识一个代理
        /// </summary>
        public int Id { get; set; }
        private Tween repelTween;
        /// <summary>
        /// 击退距离
        /// </summary>
        protected float repelDistance { get; set; } = 7f;
        /// <summary>
        /// 击退时间间隔
        /// </summary>
        protected float repelTime { get; set; } = 0.5f;
        /// <summary>
        /// 是否受到木属性强化子弹攻击
        /// </summary>
        public bool IsEnhancedBulletAttack { get; set; }
        /// <summary>
        /// 木属性精灵瞄准小怪特效
        /// </summary>
        public ParticleSystem WoodAimEffect;
        /// <summary>
        /// 有几个木属性精灵瞄准了Agent
        /// </summary>
        public int WoodAimCount { get; set; }
        /// <summary>
        /// 更新怪物的移动速度。
@@ -442,15 +482,45 @@
            return true;
        }
        /// <summary>
        /// Agent被击退
        /// </summary>
        public void AgentBeRepelled()
        {
            CanMove = false;
            if (repelTween != null)
                repelTween.Kill();
            Node StartingNodeList = EndlessLevelManager.instance.StartingNodeList[waveLineID];
            repelTween = transform.DOMoveZ(Mathf.Min(StartingNodeList.transform.position.z, transform.position.z + repelDistance), repelTime);
            repelTween.onComplete = RepelCompleted;
        }
        private void RepelCompleted()
        {
            CanMove = true;
            repelTween = null;
        }
        /// <summary>
        /// Stops the navMeshAgent and attempts to return to pool
        /// </summary>
        public override void Remove()
        {
            GameObject prefab = Resources.Load<GameObject>("Prefabs/Endless/AgentDeathEffect");
            if (prefab != null)
            {
                GameObject obj = Poolable.TryGetPoolable(prefab);
                obj.transform.position = transform.position;
                ParticleSystem ps = obj.transform.GetChild(0).GetComponent<ParticleSystem>();
                ps?.Play();
            }
            // 统一管理器内删除当前的Agent:
            AgentInsManager.instance.removeAgent(this);
            EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessAgentDead, this);
            if (EnemyData != null)
                EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessAgentDead, EnemyData.point);
            base.Remove();
            if (m_LevelManager) m_LevelManager.DecrementNumberOfEnemies();
@@ -472,18 +542,29 @@
            poisonAttid = 0;
            poisonTimes = 0;
            timeToPoisonHurt = 0;
            isFrost = false;
            bShieldBreak = false;
            bInDeathAct = false;
            ChangeState(AgentActionState.Move);
            configuration.ClearShieldWall();
            StopFrostParticle();
            if (WoodAimCount > 0)
                WoodAimCount = 0;
            WoodAimEffect.Stop();
            WoodAimEffect.Clear();
            //this.SetTargetableMatColor(Color.white);
            // 删除当前停止特效和状态.
            if (MoveStopTime > 0)
                MoveStopTime = 0.0f;
            if (repelTween != null)
            {
                repelTween.Kill();
                repelTween = null;
            }
            // 停止DoTween动画.
            this.transform.DOKill();
@@ -493,13 +574,8 @@
        private void StopFrostParticle()
        {
            if (isFrost)
            {
                isFrost = false;
                if (FrostParticle != null)
                    FrostParticle.Stop();
            }
            if (FrostParticle != null)
                FrostParticle.Stop();
        }
        /// <summary>   
@@ -507,6 +583,7 @@
        /// </summary>
        public virtual void Initialize()
        {
            Id = GameUtils.GetId();
            ResetPositionData();
            LazyLoad();
            configuration.SetHealth(configuration.maxHealth);
@@ -827,52 +904,42 @@
        /// <returns></returns>
        protected void UpdateAction()
        {
            if (mAnim)
            {
                AnimatorStateInfo stateinfo = mAnim.GetCurrentAnimatorStateInfo(0);
            if (ActionAnimator == null) return;
                if (stateinfo.IsName("Die") && (stateinfo.normalizedTime >= 0.7f))
                {
                    this.Remove();
                }
                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)
            AnimatorStateInfo stateInfo = ActionAnimator.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)
                    {
                        // 统一管理器内删除当前的Agent:
                        AgentInsManager.instance.removeAgent(this);
                        this.Remove();
                        if (isDead)
                        {
                            AgentInsManager.instance.removeAgent(this);
                            Remove();
                        }
                    }
                }
            }
            else if (changeMat)
            {
                    break;
                case AgentActionState.Death:
                    if (stateInfo.normalizedTime >= 1f)
                        Remove();
                    break;
            }
        }
        /// <summary>
        /// 检查自身血量
        /// </summary>
        private void CheckHealth()
        private void ChangeState(AgentActionState state)
        {
            if (this.healthVal <= 0.1)
            {
                Die();
            if (state == ActionState) return;
                //Debug.Log("删除多余的攻击Agent.");
            }
        }
        private void Die()
        {
            // 统一管理器内删除当前的Agent:
            AgentInsManager.instance.removeAgent(this);
            this.Remove();
            ActionState = state;
            if (ActionAnimator != null)
                ActionAnimator.SetInteger(paramName, (int)state);
        }
        /// <summary>
@@ -880,7 +947,7 @@
        /// Reset destination when path is stale
        /// River: 更新的时候,加入离最终目标的距离数据.
        /// </summary>
        protected virtual void Update()
        protected virtual void LateUpdate()
        {
            this.UpdateAction();
            HandleShieldWall();
@@ -897,6 +964,11 @@
            // 计算中毒相关.
            if ((poisonTimes > 0) && (poisonHurt > 0))
                updatePoison(Time.deltaTime);
        }
        protected virtual void Update()
        {
        }
        /// <summary>
@@ -1019,22 +1091,6 @@
            }
            if (!endlessLevelManager)
                endlessLevelManager = EndlessLevelManager.instance;
            if (mAnim == null)
            {
                foreach (Transform t in transform.GetComponentsInChildren<Transform>())
                {
                    if (t.name == "Model")
                        mAnim = t.GetComponent<Animator>();
                }
            }
            //使用三渲二的方式
            if (mAnim == null)
            {
                changeMat = GetComponent<ChangeMat>();
                changeMat?.SetSelf(this.opponentAgent, CheckHealth, Die);
            }
        }
        /// <summary>
@@ -1042,11 +1098,19 @@
        /// </summary>
        public void PlayOnHit()
        {
            if (mAnim)
                mAnim.SetBool("GetHit", true);
            else if (changeMat)
            ChangeState(AgentActionState.GetHit);
        }
        /// <summary>
        /// 播放受击动画,直接从头开始播放
        /// </summary>
        public void PlayOnHitImmediately()
        {
            ChangeState(AgentActionState.GetHit);
            if (ActionAnimator)
            {
                changeMat.SetGetHit();
                ActionAnimator.Play("GetHit", 0, 0);
                ActionAnimator.Update(0);
            }
        }
@@ -1059,116 +1123,49 @@
                FireSkillParticle.Play();
        }
        public void PlayGetHitParticle(int attributeId)
        {
            //这里的id是BallisticAttack上面设置的
            if (attributeId == 10101)
            {
                //10101 火
            }
            else if (attributeId == 20101)
            {
                //20101 水
            }
            else if (attributeId == 30101)
            {
                //30101 木
            }
        }
        public void PlayAttack()
        {
            AudioSourceManager.Ins.Play(AudioEnum.AttackTower);
            if (mAnim)
                mAnim.SetBool("Attack", true);
            else if (changeMat)
            {
                changeMat.SetAttack();
            }
            ChangeState(AgentActionState.Attack);
        }
        public override void PlayDeath()
        {
            if (mAnim)
            if (bInDeathAct) return;
            if (isPoison)
            {
                if (isPoison)
                isPoison = false;
                // 移除Agent身上的中毒特效,并播放一个中毒效果消失的特效
                if (PoisonParticle != null)
                {
                    isPoison = false;
                    // 移除Agent身上的中毒特效,并播放一个中毒效果消失的特效
                    if (PoisonParticle != null)
                    {
                        PoisonParticle.Stop();
                        PoisonParticle.Clear();
                        if (PoisonEndParticle != null)
                            PoisonEndParticle.Play();
                    }
                    PoisonParticle.Stop();
                    PoisonParticle.Clear();
                    if (PoisonEndParticle != null)
                        PoisonEndParticle.Play();
                }
                if (isSlowDown)
                {
                    isSlowDown = false;
                    if (SlowDownParticle != null)
                    {
                        SlowDownParticle.Stop();
                        SlowDownParticle.Clear();
                    }
                }
                mAnim.SetBool("Die", true);
                // 统一管理器内删除当前的Agent:
                AgentInsManager.instance.removeAgent(this);
                bInDeathAct = true;
            }
            else if (changeMat)
            if (isSlowDown)
            {
                if (isPoison)
                isSlowDown = false;
                if (SlowDownParticle != null)
                {
                    isPoison = false;
                    // 移除Agent身上的中毒特效,并播放一个中毒效果消失的特效
                    if (PoisonParticle != null)
                    {
                        PoisonParticle.Stop();
                        PoisonParticle.Clear();
                        if (PoisonEndParticle != null)
                            PoisonEndParticle.Play();
                    }
                    SlowDownParticle.Stop();
                    SlowDownParticle.Clear();
                }
                if (isSlowDown)
                {
                    isSlowDown = false;
                    if (SlowDownParticle != null)
                    {
                        SlowDownParticle.Stop();
                        SlowDownParticle.Clear();
                    }
                }
                changeMat.SetDie();
                // 统一管理器内删除当前的Agent:
                AgentInsManager.instance.removeAgent(this);
                bInDeathAct = true;
            }
            else
            bInDeathAct = true;
            if (EnemyData != null && EndlessGameUI.instance.state != EndlessGameUI.State.GameOver)
            {
                this.Remove();
                // Debug.Log("小怪被杀死了,增加能量" + EnemyData.energy);
                EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EnergyUp, EnemyData.energy);
            }
        }
        IEnumerator WaitDeathStop()
        {
            yield return null;
            AnimatorStateInfo stateinfo = mAnim.GetCurrentAnimatorStateInfo(0);
            if (stateinfo.IsName("Die") && (stateinfo.normalizedTime >= 1.0f))
            {
                Debug.Log("死亡动画播放完成,开始处理.");
                this.Remove();
            }
            Remove();
        }
        /// <summary>