wangguan
2020-12-04 d41516e30a9f1137be32d1477220f6ab262f6ed5
Assets/Scripts/TowerDefense/Affectors/AttackAffector.cs
@@ -8,6 +8,8 @@
using TowerDefense.Towers.Projectiles;
using UnityEngine;
using KTGMGemClient;
using TowerDefense.Agents;
using TowerDefense.Level;
namespace TowerDefense.Affectors
{
@@ -29,9 +31,6 @@
        /// 木塔最后一击是特殊攻击,需要替换projectile
        /// </summary>
        public GameObject woodProjectile_SP;
        //
        protected GameObject projectile1;
        protected GameObject projectile2;
        /// <summary>
        /// The list of points to launch the projectiles from
@@ -44,25 +43,9 @@
        public Transform epicenter;
        /// <summary>
        /// Configuration for when the tower does splash damage
        /// </summary>
        public bool isMultiAttack;
        /// <summary>
        /// 如果是多目标攻击,最多攻击目标
        /// </summary>
        public int maxAttackNum = 1;
        /// <summary>
        /// The fire rate in fires-per-second
        /// </summary>
        [SerializeField]
        private float projectileFireRate = 1;
        public float FireRate
        {
            get { return 1 / GetFireDuration(); }
        }
        /// <summary>
        /// 是否木属性数据
@@ -145,6 +128,30 @@
        protected float fireSpeed { get; set; } = 5f;
        /// <summary>
        /// 木属性精灵蓄力时间
        /// </summary>
        protected float woodChargeTime { get; set; } = 1.5f;
        protected float woodRemainChargeTime { get; set; }
        /// <summary>
        /// 蓄力特效时间
        /// </summary>
        protected float woodChargeEffectTime { get; set; }
        /// <summary>
        /// 木属性精灵蓄力特效
        /// </summary>
        public ParticleSystem WoodChargeEffect;
        public Transform WoodChargeTransform;
        /// <summary>
        /// 木属性正在瞄准的Agent
        /// </summary>
        protected Agent woodAimAgent;
        /// <summary>
        /// 火精灵攻击最终攻击倍速,里面计算了buff增加的倍速
        /// </summary>
        /// <value></value>
@@ -153,8 +160,10 @@
            get
            {
                FireRateAdd fireRateAdd = (FireRateAdd)EndlessBuffManager.instance.GetBuffInstanceByType(EndlessBuffEffectType.FireRateAdd);
                float rateAdd = fireRateAdd.GetFireSpeedAdd(towerPtr.attributeId);
                float rateAdd = 0;
                if (fireRateAdd != null)
                    rateAdd = fireRateAdd.GetFireSpeedAdd(towerPtr.ElfId);
                return rateAdd > 1 ? rateAdd : fireSpeed;
            }
        }
@@ -275,17 +284,6 @@
            get { return projectile == null ? null : projectile.GetComponent<Damager>(); }
        }
        /// <summary>
        /// Returns the total projectile damage
        /// </summary>
        public float GetProjectileDamage()
        {
            var splash = projectile.GetComponent<SplashDamager>();
            float splashDamage = splash != null ? splash.damage : 0;
            return damagerProjectile.finalDamage + splashDamage;
        }
        /// <summary>
        /// Initialise the RepeatingTimer
        /// </summary>
@@ -306,6 +304,22 @@
        // 处理木精灵装填子弹
        private void HandleBullet()
        {
            if (woodRemainChargeTime > 0f)
                woodRemainChargeTime -= Time.deltaTime;
            if (woodChargeEffectTime > 0f)
            {
                woodChargeEffectTime -= Time.deltaTime;
                UpdateWoodAim();
                if (woodChargeEffectTime <= 0)
                {
                    towerPtr.IsWoodCharge = false;
                    CancelWoodAim();
                    WoodChargeEffect.Stop();
                }
            }
            // 预留出来装填子弹的时间.
            if (fillBulletTime > 0)
            {
@@ -321,6 +335,68 @@
                    fillBulletTime = 0;
                }
            }
        }
        /// <summary>
        /// 更新木属性瞄准
        /// </summary>
        private void UpdateWoodAim()
        {
            // 离得最近的 Agent
            Agent agent = AgentInsManager.instance.GetMinDisAgent(waveLineID, false);
            if (agent != null)
            {
                // 还没有瞄准目标,直接分配
                if (woodAimAgent == null)
                {
                    woodAimAgent = agent;
                    towerPtr.WoodAimAgent = agent;
                    if (agent.WoodAimCount == 0)
                        agent.PlayWoodAimEffect();
                    ++agent.WoodAimCount;
                }
                // 有小怪走到之前瞄准目标的前面 或者 之前瞄准的目标死亡,切换瞄准目标
                else if (woodAimAgent.Id != agent.Id)
                {
                    if (woodAimAgent.WoodAimCount > 0)
                    {
                        --woodAimAgent.WoodAimCount;
                        if (woodAimAgent.WoodAimCount == 0)
                            woodAimAgent.StopWoodAimEffect();
                    }
                    woodAimAgent = agent;
                    towerPtr.WoodAimAgent = agent;
                    if (agent.WoodAimCount == 0)
                        agent.PlayWoodAimEffect();
                    ++agent.WoodAimCount;
                }
            }
        }
        /// <summary>
        /// 取消木属性瞄准
        /// </summary>
        private void CancelWoodAim()
        {
            if (woodAimAgent != null)
            {
                if (woodAimAgent.WoodAimCount > 0)
                {
                    --woodAimAgent.WoodAimCount;
                    if (woodAimAgent.WoodAimCount == 0)
                        woodAimAgent.StopWoodAimEffect();
                }
            }
            woodAimAgent = null;
        }
        // 处理火精灵充能
@@ -340,7 +416,6 @@
                    {
                        fireState = true;
                        fInEnergy = finalFireSpeed;
                        myTower.SetFireMatSpeed(true);//设置了火宝石快速攻击
                        // 设置多倍攻击速度
                        fBackupTimer = m_FireTimer;
                        m_FireTimer = m_FireTimer / finalFireSpeed;
@@ -354,10 +429,8 @@
                    fInEnergy -= Time.deltaTime;
                    if (fInEnergy <= 0)
                    {
                        myTower.SetFireMatSpeed(false);//恢复了火宝石攻击速度
                        fireState = false;
                        EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.FireTowerChargeEnd);
                        //EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.FireTowerChargeEnd);
                        fInEnergy = 0.0f;
                        energyCalTime = 0.0f;
                        towerPtr.energyCtl.SetEnergyProgress(0);
@@ -380,7 +453,7 @@
                Damager damager = projectile.gameObject.GetComponent<Damager>();
                float finalDamage = damager.damage;
                List<EndlessBuffConfig> list = EndlessBuffManager.instance.GetBuffListByEffectType(EndlessBuffEffectType.AttackAdd, towerPtr.attributeId);
                List<EndlessBuffConfig> list = EndlessBuffManager.instance.GetBuffListByEffectType(EndlessBuffEffectType.AttackAdd, towerPtr.ElfId);
                float ratio = 0;
                float add = 0;
@@ -446,11 +519,13 @@
            {
                if (fireState)
                {
                    myTower.SetFireMatSpeed(true);//设置了火宝石快速攻击
                    towerPtr.PlayEnergyEffect(true);
                }
            }
            // 如果在木属性蓄力期间,让瞄准动画播放
            if (towerPtr && towerPtr.IsWoodCharge && woodAimAgent != null)
                woodAimAgent.PlayWoodAimEffect();
        }
        /// <summary>
@@ -468,20 +543,11 @@
        }
        /// <summary>
        /// 获取子弹发射时间间隔
        /// </summary>
        public float GetFireDuration()
        {
            DecreaseTowerAttackCD endlessBuff = (DecreaseTowerAttackCD)EndlessBuffManager.instance.GetBuffInstanceByType(EndlessBuffEffectType.DecreaseTowerAttackCD);
            return endlessBuff.GetDecreaseCD(towerPtr.attributeId, 1 / projectileFireRate);
        }
        /// <summary>
        /// Update the timers
        /// </summary>
        protected virtual void Update()
        {
            if (m_Launcher == null) return;
            if (m_Launcher == null || towerPtr != null && !towerPtr.CanAttack) return;
            // 处理当前Affector所在Tower对应的技能
            updateTowerSkillData();
@@ -491,7 +557,7 @@
            if (m_TrackingEnemy != null && m_FireTimer < 0)
            {
                m_FireTimer = GetFireDuration();
                m_FireTimer = towerLevel.GetFireRate();
                if (fInEnergy > 0)
                    m_FireTimer /= finalFireSpeed;
@@ -501,7 +567,9 @@
                if (towerPtr && towerPtr.bulletCtl != null)
                {
                    int bnum = towerPtr.bulletCtl.GetCtlProgress();
                    if (bnum == 0) return;
                    // 蓄力时间内不攻击
                    if (bnum == 0 || woodRemainChargeTime > 0f) return;
                }
                if (towerPtr && towerPtr.FreezeBreathCtrl != null)
@@ -520,16 +588,17 @@
        /// </summary>
        public virtual void FireProjectile()
        {
            // 不再处理多子弹攻击,确保只有一个弹道
            isMultiAttack = false;
            m_TrackingEnemy = targetter.GetTarget(waveLineID, bWoodAffector);
            GameObject go = damagerProjectile.gameObject;
            if (m_TrackingEnemy == null || fillBulletTime > 0) return;
            Damager goDamager = go.GetComponent<Damager>();
            goDamager.IsEnhancedBullet = false;
            goDamager.TowerPtr = towerPtr;
            // 处理子弹充能相关的内容
            if (towerPtr && (towerPtr.bulletCtl != null))
            if (towerPtr && towerPtr.bulletCtl != null)
            {
                int bnum = towerPtr.bulletCtl.decBullet();
                // 暴击子弹的数量,如果获得相应buff可能会修改暴击子弹数量
@@ -538,7 +607,8 @@
                if (bnum < critBulletNum)
                {
                    if (bnum == 0)
                        fillBulletTime = 2.0f;
                        // 不需要装填时间
                        fillBulletTime = 0.1f;
                    //这里需要替换特效
                    var poolable = Core.Utilities.Poolable.TryGetPoolable<Core.Utilities.Poolable>(woodProjectile_SP);
@@ -546,33 +616,30 @@
                    Damager tmpDamager = go.GetComponent<Damager>();
                    tmpDamager.damageMulti = 10.0f;
                    tmpDamager.damage = damagerProjectile.damage;
                    tmpDamager.IsEnhancedBullet = true;
                    tmpDamager.TowerPtr = towerPtr;
                }
            }
            else
            {
                if (towerPtr)
                    towerPtr.setTowerState(true);
                // 下一颗子弹是强化子弹,然后直接蓄力
                if (bnum - 1 >= 0 && bnum - 1 < critBulletNum)
                {
                    woodRemainChargeTime = woodChargeTime;
                    towerPtr.IsWoodCharge = true;
                    DecreaseWoodChargeTime decreaseWoodChargeTime = (DecreaseWoodChargeTime)EndlessBuffManager.instance.GetBuffInstanceByType(EndlessBuffEffectType.DecreaseWoodChargeTime);
                    if (decreaseWoodChargeTime != null)
                        woodRemainChargeTime = decreaseWoodChargeTime.GetWoodChargeTime(woodChargeTime);
                    woodChargeEffectTime = woodRemainChargeTime + 0.5f / towerLevel.ActionAnimator.speed;
                    WoodChargeEffect.Play();
                }
            }
            if (isMultiAttack)
            if (Targetter.bSearchTarget)
            {
                List<Targetable> enemies = towerTargetter.GetAllTargets();
                if ((enemies != null) && (Targetter.bSearchTarget))
                    m_Launcher.Launch(enemies, projectile, projectilePoints, maxAttackNum);
            }
            else
            {
                if (Targetter.bSearchTarget)
                {
                    m_Launcher.Launch(m_TrackingEnemy, go, projectilePoints);
                    if (AudioSourceManager.Ins)
                        AudioSourceManager.Ins.Play(audioEnum);
                }
            }
            if (randomAudioSource != null)
            {
                if (Targetter.bSearchTarget)
                    randomAudioSource.PlayRandomClip();
                m_Launcher.Launch(m_TrackingEnemy, go, projectilePoints);
                if (AudioSourceManager.Ins)
                    AudioSourceManager.Ins.Play(audioEnum);
            }
        }