Assets/Scripts/TowerDefense/Agents/Agent.cs
@@ -39,7 +39,7 @@
    /// An agent will follow a path of nodes
    /// NavMeshAgent和AttackAffector是必须得加入的EntityNode.
    /// </summary>
    [RequireComponent(typeof(NavMeshAgent)), RequireComponent(typeof(AttackAffector))]
    [RequireComponent(typeof(NavMeshAgent))]
    public abstract class Agent : Targetable
    {
        /// <summary>
@@ -222,10 +222,43 @@
        /// </summary>
        protected bool isSlowDown;
        private bool isFrost;
        /// <summary>
        /// 是否处于冰冻状态
        /// </summary>
        protected bool isFrost;
        /// <value></value>
        public bool IsFrost
        {
            get { return isFrost; }
            set
            {
                if (!isFrost)
                {
                    if (value)
                    {
                        EndlessGameUI.instance.FloatFrostWord(transform.position);
                        PlaySlowDownEffect();
                        CanMove = false;
                    }
                }
                else
                {
                    if (!value)
                    {
                        StopSlowDownEffect();
                        CanMove = true;
                    }
                }
                isFrost = value;
            }
        }
        /// <summary>
        /// 冰冻剩余时间,< 0时解除冰冻,冰冻期间不会被再次冰冻
        /// </summary>
        public float FrostRemainTime { get; set; }
        /// <summary>
        /// 中毒粒子特效
@@ -243,15 +276,9 @@
        public ParticleSystem SlowDownParticle;
        /// <summary>
        /// 冰冻特效
        /// </summary>
        public ParticleSystem FrostParticle;
        /// <summary>
        /// 被火技能攻击特效
        /// </summary>
        public ParticleSystem FireSkillParticle;
        /// <summary>
        /// 降低移动速度.
@@ -264,13 +291,26 @@
                speedSlowRate = 0.5f;
        }
        private bool _HasSlowDownText;
        public bool HasSlowDownText
        {
            set
            {
                _HasSlowDownText = value;
            }
            get
            {
                return _HasSlowDownText;
            }
        }
        /// <summary>
        /// 怪物中毒.
        /// </summary>
        /// <param name="damage"></param>
        public void poisonAgent(float damage, int attid)
        {
            if (this.poisonTimes >= 1) return;
            if (poisonTimes >= 1) return;
            if (!isPoison)
            {
@@ -280,13 +320,13 @@
                    PoisonParticle.Play();
            }
            this.poisonTimes++;
            this.poisonAttid = attid;
            this.poisonHurt = (float)Math.Floor(this.configuration.maxHealth / 20.0f);
            this.timeToPoisonHurt = 1.0f;
            poisonTimes++;
            poisonAttid = attid;
            poisonHurt = (float)Math.Floor(configuration.maxHealth / 20.0f);
            timeToPoisonHurt = 1.0f;
        }
        public bool bInPoison { get { return this.poisonHurt > 0; } }
        public bool bInPoison { get { return poisonHurt > 0; } }
        /// <summary>
        /// 处理中毒相关的数据
@@ -294,11 +334,11 @@
        /// <param name="time"></param>
        protected void updatePoison(float time)
        {
            this.timeToPoisonHurt -= time;
            if (this.timeToPoisonHurt <= 0)
            timeToPoisonHurt -= time;
            if (timeToPoisonHurt <= 0)
            {
                Vector3 backPos = this.transform.position;
                this.TakeDamage(poisonHurt, this.transform.position, null, poisonAttid);
                Vector3 backPos = transform.position;
                TakeDamage(poisonHurt, transform.position, null, poisonAttid);
                // if ((poisonHurt > 0) && (!opponentAgent))
                // {
                //     if (GameUI.instanceExists)
@@ -322,7 +362,7 @@
        public bool bInDeathState
        {
            get { return this.bInDeathAct; }
            get { return bInDeathAct; }
            set { bInDeathAct = value; }
        }
@@ -343,7 +383,7 @@
        public NavMeshAgent navMeshAgent
        {
            get { return this.m_NavMeshAgent; }
            get { return m_NavMeshAgent; }
        }
        /// <summary>
@@ -377,7 +417,7 @@
        public float distanceToDest
        {
            get { return this.m_DisToDestination; }
            get { return m_DisToDestination; }
        }
        /// <summary>
@@ -433,6 +473,11 @@
        public int WoodAimCount { get; set; }
        /// <summary>
        /// 血条的引用
        /// </summary>
        public AgentBlood BloodBar { get; set; }
        /// <summary>
        /// 更新怪物的移动速度。
        /// </summary>
        /// <param name="fscale"></param>
@@ -449,11 +494,13 @@
        /// <param name="coinDrop"></param>     设置当前小怪的金币掉落数据
        public void SetAgentData(float health, float moveSpeed = -1, int coinDrop = 0)
        {
            this.configuration.maxHealth = health;
            this.configuration.startingHealth = health;
            this.configuration.SetMaxHealth(health);
            this.configuration.Init();
            this.configuration.SetHealth(health);
            SpawnBlood();
            BloodBar.SetCurrentBlood(1f, false);
            configuration.maxHealth = health;
            configuration.startingHealth = health;
            configuration.SetMaxHealth(health);
            configuration.Init();
            configuration.SetHealth(health);
            // 只有在速度大于零的情况下才会设置.
            if (moveSpeed > 0)
@@ -462,11 +509,11 @@
            mLootDrop.lootDropped = coinDrop;
            mAgentData.speed = fMoveSpeed;
            mAgentData.hp = this.configuration.maxHealth;
            mAgentData.hp = configuration.maxHealth;
            //
            mStartYVal = this.transform.position.y;
            mStartZVal = this.transform.position.z;
            mStartYVal = transform.position.y;
            mStartZVal = transform.position.z;
            speedSlowRate = 0.0f;
            poisonHurt = 0;
@@ -474,6 +521,28 @@
            poisonTimes = 0;
            timeToPoisonHurt = 0;
            CanMove = true;
        }
        /// <summary>
        /// 生成血条
        /// </summary>
        /// <param name="agent"></param>
        private void SpawnBlood()
        {
            if (BloodBar == null)
            {
                GameObject prefab = Resources.Load<GameObject>("Prefabs/AgentBlood");
                GameObject obj = Instantiate(prefab);
                BloodBar = obj.GetComponent<AgentBlood>();
                GameObject bloodUI = GameObject.Find("MainUI/BloodUI");
                obj.transform.SetParent(bloodUI.transform, false);
                AgentBlood agentBlood = obj.GetComponent<AgentBlood>();
                agentBlood.Target = this;
                configuration.died += agentBlood.OnDied;
                configuration.healthChanged += agentBlood.OnHealthChanged;
            }
            BloodBar.Hide();
        }
        /// <summary>
@@ -531,7 +600,7 @@
            // 需要设置移动的目标Node.
            m_NextNode = m_CurrentNode.GetNextNode();
            this.MoveToNode();
            MoveToNode();
        }
        /// <summary>
@@ -541,14 +610,6 @@
        protected virtual void SetAgentStopBuff(buffinfo binfo)
        {
            MoveStopTime = binfo.last / 1000;
            if (!isFrost)
            {
                isFrost = true;
                if (FrostParticle != null)
                    FrostParticle.Play();
            }
        }
        /// <summary>
@@ -629,6 +690,8 @@
                ps?.Play();
            }
            if (BloodBar)
                Destroy(BloodBar.gameObject);
            // 统一管理器内删除当前的Agent:
            AgentInsManager.instance.removeAgent(this);
            if (EnemyData != null)
@@ -644,27 +707,25 @@
            m_NavMeshAgent.enabled = false;
            // 必须要重置数据,不然会有一系列的小Bug.
            this.m_CurrentNode = null;
            m_CurrentNode = null;
            m_NextNode = null;
            this.liveID = this.liveID + 1;
            liveID = liveID + 1;
            speedSlowRate = 0.0f;
            poisonHurt = 0;
            poisonAttid = 0;
            poisonTimes = 0;
            timeToPoisonHurt = 0;
            isFrost = false;
            IsFrost = false;
            bInDeathAct = false;
            ChangeState(AgentActionState.Move);
            configuration.ClearShieldWall();
            StopFrostParticle();
            if (WoodAimCount > 0)
                WoodAimCount = 0;
            StopWoodAimEffect();
            //this.SetTargetableMatColor(Color.white);
            //SetTargetableMatColor(Color.white);
            // 删除当前停止特效和状态.
            if (MoveStopTime > 0)
@@ -677,15 +738,8 @@
            }
            // 停止DoTween动画.
            this.transform.DOKill();
            Poolable.TryPool(gameObject);
        }
        private void StopFrostParticle()
        {
            if (FrostParticle != null)
                FrostParticle.Stop();
            transform.DOKill();
            Destroy(gameObject);
        }
        /// <summary>   
@@ -708,24 +762,25 @@
            // 
            // 设置一个DOTween队列,让场景内刷出来的Agent有一个变大淡出的效果,此时是无敌效果加持.
            this.configuration.bInvincible = true;
            mDefaultScale = this.transform.localScale;
            configuration.bInvincible = true;
            mDefaultScale = transform.localScale;
            Sequence agentTweenSeq = DOTween.Sequence();
            var ss = mDefaultScale * 0.3f;
            this.transform.localScale = this.transform.localScale * 0.3f;
            Tweener agScale = this.transform.DOScale(mDefaultScale, 0.3f);
            transform.localScale = transform.localScale * 0.3f;
            Tweener agScale = transform.DOScale(mDefaultScale, 0.3f);
            agentTweenSeq.Append(agScale);
            agentTweenSeq.AppendCallback(beDamageStart);
            this.nodeMoveUpdate = false;
            nodeMoveUpdate = false;
            // 获取移动速度
            fMoveSpeed = this.m_NavMeshAgent.speed / 2.0f;
            fMoveSpeed = m_NavMeshAgent.speed / 2.0f;
            _HasSlowDownText = false;
        }
        protected void beDamageStart()
        {
            this.configuration.bInvincible = false;
            configuration.bInvincible = false;
        }
        /// <summary>
@@ -733,16 +788,16 @@
        /// </summary>
        public void ResetAgentToInitialPos()
        {
            if (!this.initNode) return;
            if (!initNode) return;
            Vector3 initPos = this.initNode.GetRandomPointInNodeArea();
            Vector3 initPos = initNode.GetRandomPointInNodeArea();
            transform.position = initPos;
            ResetPositionData();
            this.SetNode(initNode, -1);
            SetNode(initNode, -1);
            // 更新AgentMgr,确保不再攻击回到原点的Agent.
            AgentInsManager.instance.updateInsMgrPos(this.opponentAgent);
            AgentInsManager.instance.updateInsMgrPos(opponentAgent);
        }
@@ -755,10 +810,10 @@
            if (configuration.bInvincible) return;
            // 只有处于非PosEffect状态才可以有这个状态.
            var posResetEff = this.GetComponent<AgentResetPosEffect>();
            var posResetEff = GetComponent<AgentResetPosEffect>();
            if (posResetEff == null)
            {
                posResetEff = this.gameObject.AddComponent<AgentResetPosEffect>();
                posResetEff = gameObject.AddComponent<AgentResetPosEffect>();
                // 初始化当前的PosResetEffect
                posResetEff.Initialize(3, AgentInsManager.instance.posResetFx);
@@ -878,6 +933,21 @@
            MoveStopTime = 0.0f;
        }
        private void PlaySlowDownEffect()
        {
            if (SlowDownParticle != null)
                SlowDownParticle.Play();
        }
        private void StopSlowDownEffect()
        {
            if (SlowDownParticle != null && !isSlowDown && !IsFrost)
            {
                SlowDownParticle.Stop();
                SlowDownParticle.Clear();
            }
        }
        /// <summary>
        /// 根据帧间的时间,来更新Agent的位置信息,其它信息直接删除.
        /// 1: 
@@ -885,46 +955,19 @@
        /// <param name="deltaTime"></param>
        protected void updateAgentPos(float deltaTime)
        {
            if ((!m_CurrentNode) || (!m_NextNode))
                return;
            // 是否处于停止状态.
            if (MoveStopTime > 0)
            {
                MoveStopTime -= Time.deltaTime;
                if (MoveStopTime <= 0)
                {
                    StopFrostParticle();
                    MoveStopTime = 0;
                }
                else
                    return;
            }
            if (this.fMoveSpeed == 0)
            {
                Debug.Log("当前的移动为零:" + fMoveSpeed);
                return;
            }
            if (!m_CurrentNode || !m_NextNode || fMoveSpeed <= 0.0001f) return;
            float finalSpeed = fMoveSpeed * (1 - speedSlowRate);
            if (speedSlowRate > 0 && !isSlowDown)
            {
                isSlowDown = true;
                if (SlowDownParticle != null)
                    SlowDownParticle.Play();
                PlaySlowDownEffect();
            }
            else if (speedSlowRate == 0 && isSlowDown)
            {
                isSlowDown = false;
                if (SlowDownParticle != null)
                {
                    SlowDownParticle.Stop();
                    SlowDownParticle.Clear();
                }
                StopSlowDownEffect();
            }
            Vector3 curPos = m_NavMeshAgent.transform.position;
@@ -977,13 +1020,13 @@
            // 根据是否到达一个结点来进行不同的处理
            if (swithNode)
            {
                this.MoveToNextNode(m_NextNode);
                MoveToNextNode(m_NextNode);
            }
            else
            {
                // Y值插值.
                float lerpVal = (curPos.z - mStartZVal) / (m_Destination.transform.position.z - mStartZVal);
                if (!this.opponentAgent)
                if (!opponentAgent)
                    // curPos.y = mStartYVal + lerpVal * 60f;
                    curPos.y = mStartYVal;
                m_NavMeshAgent.transform.position = curPos;
@@ -1043,8 +1086,9 @@
        /// </summary>
        protected virtual void LateUpdate()
        {
            this.UpdateAction();
            UpdateAction();
            HandleShieldWall();
            HandleFrost();
            // 处理死亡状态了,不必再移动:
            if (bInDeathAct || !CanMove) return;
@@ -1053,11 +1097,27 @@
            updateAgentPos(Time.deltaTime);
            // 计算离目标的距离,非最终距离,只用于比较大小
            this.m_DisToDestination = this.GetDisToDestination();
            m_DisToDestination = GetDisToDestination();
            // 计算中毒相关.
            if ((poisonTimes > 0) && (poisonHurt > 0))
                updatePoison(Time.deltaTime);
        }
        private void HandleFrost()
        {
            if (IsFrost)
            {
                if (FrostRemainTime > 0f)
                {
                    FrostRemainTime -= Time.deltaTime;
                    if (FrostRemainTime <= 0f)
                        IsFrost = false;
                }
                else
                    IsFrost = false;
            }
        }
        protected virtual void Update()
@@ -1091,7 +1151,7 @@
        {
            /*         // 设置最大的Z值,确保按路线移动
                        Vector3 pos = m_NavMeshAgent.transform.position;
                        if (this.m_PositiveZ)
                        if (m_PositiveZ)
                        {
                            if (pos.z > m_ZMaxValue)
                            {
@@ -1108,7 +1168,7 @@
                            }
                        }
                        // 设置X值的限制,确保按路线移动
                        if(this.m_PositiveX)
                        if(m_PositiveX)
                        {
                            if( pos.x > m_XMaxValue)
                            {
@@ -1160,9 +1220,9 @@
                tmpDis += NODE_DIS;
                tmpNode = tmpNode.GetNextNode();
            }
            Vector3 tpos = this.position;
            Vector3 tpos = position;
            tpos.y = mStartYVal;
            tmpDis += Vector3.Distance(tpos, this.m_Destination.transform.position);
            tmpDis += Vector3.Distance(tpos, m_Destination.transform.position);
            return tmpDis;
        }