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;
@@ -209,6 +213,7 @@
        public bool bInDeathState
        {
            get { return this.bInDeathAct; }
            set { bInDeathAct = value; }
        }
        /// <summary>
@@ -231,20 +236,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 +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>
        /// 更新怪物的移动速度。
@@ -293,6 +341,7 @@
        {
            this.configuration.maxHealth = health;
            this.configuration.startingHealth = health;
            this.configuration.SetMaxHealth(health);
            this.configuration.Init();
            this.configuration.SetHealth(health);
@@ -398,7 +447,7 @@
        /// 处理Agent的锁定Buf,播放特效,设置速度为零,并时间到达后重设数据。
        /// </summary>
        /// <param name="binfo"></param>
        protected void SetAgentStopBuff(buffinfo binfo)
        protected virtual void SetAgentStopBuff(buffinfo binfo)
        {
            MoveStopTime = binfo.last / 1000;
@@ -433,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);
            if (EnemyData != null)
                EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessAgentDead, EnemyData.point);
            base.Remove();
            if (m_LevelManager) m_LevelManager.DecrementNumberOfEnemies();
@@ -463,16 +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();
@@ -482,13 +574,8 @@
        private void StopFrostParticle()
        {
            if (isFrost)
            {
                isFrost = false;
                if (FrostParticle != null)
                    FrostParticle.Stop();
            }
            if (FrostParticle != null)
                FrostParticle.Stop();
        }
        /// <summary>   
@@ -496,6 +583,7 @@
        /// </summary>
        public virtual void Initialize()
        {
            Id = GameUtils.GetId();
            ResetPositionData();
            LazyLoad();
            configuration.SetHealth(configuration.maxHealth);
@@ -689,6 +777,7 @@
        /// </summary>
        protected override void Awake()
        {
            //Debug.Log("哪里生成的?");
            base.Awake();
            LazyLoad();
            m_NavMeshAgent.enabled = false;
@@ -815,32 +904,42 @@
        /// <returns></returns>
        protected void UpdateAction()
        {
            if (!mAnim) return;
            if (ActionAnimator == null) return;
            AnimatorStateInfo stateinfo = mAnim.GetCurrentAnimatorStateInfo(0);
            AnimatorStateInfo stateInfo = ActionAnimator.GetCurrentAnimatorStateInfo(0);
            if (stateinfo.IsName("Die") && (stateinfo.normalizedTime >= 0.7f))
            switch (ActionState)
            {
                this.Remove();
                return;
                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;
            }
            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;
                }
            }
        }
            return;
        private void ChangeState(AgentActionState state)
        {
            if (state == ActionState) return;
            ActionState = state;
            if (ActionAnimator != null)
                ActionAnimator.SetInteger(paramName, (int)state);
        }
        /// <summary>
@@ -848,9 +947,10 @@
        /// Reset destination when path is stale
        /// River: 更新的时候,加入离最终目标的距离数据.
        /// </summary>
        protected virtual void Update()
        protected virtual void LateUpdate()
        {
            this.UpdateAction();
            HandleShieldWall();
            // 处理死亡状态了,不必再移动:
            if (bInDeathAct || !CanMove) return;
@@ -864,6 +964,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>
@@ -949,6 +1073,7 @@
            return tmpDis;
        }
        protected ChangeMat changeMat;
        /// <summary>
        /// This is a lazy way of caching several components utilised by the Agent
        /// </summary>
@@ -966,15 +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>();
                }
            }
        }
        /// <summary>
@@ -982,65 +1098,74 @@
        /// </summary>
        public void PlayOnHit()
        {
            if (mAnim)
                mAnim.SetBool("GetHit", true);
            ChangeState(AgentActionState.GetHit);
        }
        /// <summary>
        /// 播放受击动画,直接从头开始播放
        /// </summary>
        public void PlayOnHitImmediately()
        {
            ChangeState(AgentActionState.GetHit);
            if (ActionAnimator)
            {
                ActionAnimator.Play("GetHit", 0, 0);
                ActionAnimator.Update(0);
            }
        }
        /// <summary>
        /// 播放火技能打击动画
        /// </summary>
        public void PlayFireSkillHit()
        {
            if (FireSkillParticle != null)
                FireSkillParticle.Play();
        }
        public void PlayAttack()
        {
            if (mAnim)
                mAnim.SetBool("Attack", true);
            AudioSourceManager.Ins.Play(AudioEnum.AttackTower);
            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)
            if (isSlowDown)
            {
                isSlowDown = false;
                if (SlowDownParticle != null)
                {
                    isSlowDown = false;
                    if (SlowDownParticle != null)
                    {
                        SlowDownParticle.Stop();
                        SlowDownParticle.Clear();
                    }
                    SlowDownParticle.Stop();
                    SlowDownParticle.Clear();
                }
                mAnim.SetBool("Die", true);
                // 统一管理器内删除当前的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>