From 2ae9edd5bfd8b8baabb8cd995485f9ade4faebc9 Mon Sep 17 00:00:00 2001
From: wangguan <wangguan@kt007.com>
Date: Wed, 04 Nov 2020 20:22:50 +0800
Subject: [PATCH] 增加音效

---
 Assets/Scripts/TowerDefense/Affectors/AttackAffector.cs |  707 ++++++++++++++++++++++++++++++----------------------------
 1 files changed, 366 insertions(+), 341 deletions(-)

diff --git a/Assets/Scripts/TowerDefense/Affectors/AttackAffector.cs b/Assets/Scripts/TowerDefense/Affectors/AttackAffector.cs
index 1e7e957..f2aa107 100644
--- a/Assets/Scripts/TowerDefense/Affectors/AttackAffector.cs
+++ b/Assets/Scripts/TowerDefense/Affectors/AttackAffector.cs
@@ -12,43 +12,43 @@
 
 namespace TowerDefense.Affectors
 {
-	/// <summary>
-	/// The common effect for handling firing projectiles to attack
-	/// 
-	/// Requires an ILauncher but it is not automatically added
-	/// Add an ILauncher implementation to this GameObject before you add this script
-	/// </summary>
-	[RequireComponent(typeof(ILauncher))]
-	public class AttackAffector : Affector, ITowerRadiusProvider
-	{
-		/// <summary>
-		/// The projectile used to attack
-		/// </summary>
-		public GameObject projectile;
+    /// <summary>
+    /// The common effect for handling firing projectiles to attack
+    /// 
+    /// Requires an ILauncher but it is not automatically added
+    /// Add an ILauncher implementation to this GameObject before you add this script
+    /// </summary>
+    [RequireComponent(typeof(ILauncher))]
+    public class AttackAffector : Affector, ITowerRadiusProvider
+    {
+        /// <summary>
+        /// The projectile used to attack
+        /// </summary>
+        public GameObject projectile;
 
-		// 
-		protected GameObject projectile1;
-		protected GameObject projectile2;
+        // 
+        protected GameObject projectile1;
+        protected GameObject projectile2;
 
-		/// <summary>
-		/// The list of points to launch the projectiles from
-		/// </summary>
-		public Transform[] projectilePoints;
+        /// <summary>
+        /// The list of points to launch the projectiles from
+        /// </summary>
+        public Transform[] projectilePoints;
 
-		/// <summary>
-		/// The reference to the center point where the tower will search from
-		/// </summary>
-		public Transform epicenter;
+        /// <summary>
+        /// The reference to the center point where the tower will search from
+        /// </summary>
+        public Transform epicenter;
 
-		/// <summary>
-		/// Configuration for when the tower does splash damage
-		/// </summary>
-		public bool isMultiAttack;
+        /// <summary>
+        /// Configuration for when the tower does splash damage
+        /// </summary>
+        public bool isMultiAttack;
 
-		/// <summary>
-		/// 如果是多目标攻击,最多攻击目标
-		/// </summary>
-		public int maxAttackNum = 1;
+        /// <summary>
+        /// 如果是多目标攻击,最多攻击目标
+        /// </summary>
+        public int maxAttackNum = 1;
 
 
         /// <summary>
@@ -57,356 +57,381 @@
         public float fireRate;
 
 
-		/// <summary>
-		/// 是否木属性数据
-		/// </summary>
-		public bool bWoodAffector = false;
+        /// <summary>
+        /// 是否木属性数据
+        /// </summary>
+        public bool bWoodAffector = false;
 
-		/// <summary>
-		/// The audio source to play when firing
-		/// </summary>
-		public RandomAudioSource randomAudioSource;
+        /// <summary>
+        /// The audio source to play when firing
+        /// </summary>
+        public RandomAudioSource randomAudioSource;
 
-		/// <summary>
-		/// Gets the targetter
-		/// </summary>
-		public Targetter towerTargetter;
+        /// <summary>
+        /// Gets the targetter
+        /// </summary>
+        public Targetter towerTargetter;
 
-		/// <summary>
-		/// Color of effect radius visualization
-		/// </summary>
-		public Color radiusEffectColor;
+        /// <summary>
+        /// Color of effect radius visualization
+        /// </summary>
+        public Color radiusEffectColor;
 
-		/// <summary>
-		/// Search condition
-		/// </summary>
-		public Filter searchCondition;
+        /// <summary>
+        /// Search condition
+        /// </summary>
+        public Filter searchCondition;
 
-		/// <summary>
-		/// Fire condition
-		/// </summary>
-		public Filter fireCondition;
+        /// <summary>
+        /// Fire condition
+        /// </summary>
+        public Filter fireCondition;
 
-		/// <summary>
-		/// The reference to the attached launcher
-		/// </summary>
-		protected ILauncher m_Launcher;
+        /// <summary>
+        /// The reference to the attached launcher
+        /// </summary>
+        protected ILauncher m_Launcher;
 
-		/// <summary>
-		/// The time before firing is possible
-		/// </summary>
-		protected float m_FireTimer;
+        /// <summary>
+        /// The time before firing is possible
+        /// </summary>
+        protected float m_FireTimer;
 
-		/// <summary>
-		/// Reference to the current tracked enemy
-		/// </summary>
-		protected Targetable m_TrackingEnemy;
+        /// <summary>
+        /// Reference to the current tracked enemy
+        /// </summary>
+        protected Targetable m_TrackingEnemy;
 
-		/// <summary>
-		/// 处理装弹时间.
-		/// </summary>
-		protected float fillBulletTime = 0.0f;
+        /// <summary>
+        /// 处理装弹时间.
+        /// </summary>
+        protected float fillBulletTime = 0.0f;
 
         /// <summary>
         /// 充能时间
         /// </summary>
         protected float energyCalTime = 0;
-		protected float fInEnergy = 0;
-		protected float fBackupTimer = 0.0f;
-		/// <summary>
-		/// Gets the search rate from the targetter
-		/// </summary>
-		public float searchRate
-		{
-			get { return towerTargetter.searchRate; }
-			set { towerTargetter.searchRate = value; }
-		}
-
-		/// <summary>
-		/// Gets the targetable
-		/// </summary>
-		public Targetable trackingEnemy
-		{
-			get { return m_TrackingEnemy; }
-		}
-
-		/// <summary>
-		/// Gets or sets the attack radius
-		/// </summary>
-		public float effectRadius
-		{
-			get { return towerTargetter.effectRadius; }
-		}
-
-		public Color effectColor 
-		{
-			get { return radiusEffectColor; }
-		}
-
-		public Targetter targetter 
-		{
-			get { return towerTargetter; }
-		}
-
-		/// <summary>
-		/// Initializes the attack affector
-		/// </summary>
-		public override void Initialize(IAlignmentProvider affectorAlignment)
-		{
-			Initialize(affectorAlignment, -1);
-		}
-
-		/// <summary>
-		/// 返回可能存在的Targetter.
-		/// </summary>
-		/// <returns></returns>
-		public override TowerDefense.Targetting.Targetter GetTargetter()
-		{
-			return targetter;
-		}
-
-		/// <summary>
-		/// Initialises the  attack affector with a layer mask
-		/// </summary>
-		public override void Initialize(IAlignmentProvider affectorAlignment, LayerMask mask)
-		{
-			base.Initialize(affectorAlignment, mask);
-			SetUpTimers();
-
-			towerTargetter.ResetTargetter();
-			towerTargetter.alignment = affectorAlignment;
-			towerTargetter.acquiredTarget += OnAcquiredTarget;
-			towerTargetter.lostTarget += OnLostTarget;
-		}
-
-		void OnDestroy()
-		{
-			towerTargetter.acquiredTarget -= OnAcquiredTarget;
-			towerTargetter.lostTarget -= OnLostTarget;
-		}
-
-		void OnLostTarget()
-		{
-			m_TrackingEnemy = null;
-		}
-
-		void OnAcquiredTarget(Targetable acquiredTarget)
-		{
-			m_TrackingEnemy = acquiredTarget;
-		}
-
-		public Damager damagerProjectile
-		{
-			get { return projectile == null ? null : projectile.GetComponent<Damager>(); }
-		}
-
-		public Damager damagerProjectile1
-		{
-			get { return projectile == null ? null : projectile.GetComponent<Damager>(); }
-		}
-
-		public Damager damagerProjectile2
-		{
-			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>
-		protected virtual void SetUpTimers()
-		{
-			m_FireTimer = 1 / fireRate;
-			m_Launcher = GetComponent<ILauncher>();
-		}
-
-
-		protected void updateTowerSkillData()
+        protected float fInEnergy = 0;
+        protected float fBackupTimer = 0.0f;
+        /// <summary>
+        /// Gets the search rate from the targetter
+        /// </summary>
+        public float searchRate
         {
-			// 
-			// 预留出来装填子弹的时间.
-			if (fillBulletTime > 0)
-			{
-				fillBulletTime -= Time.deltaTime;
-				if (fillBulletTime <= 0.3f)
-				{
-					if (towerPtr && towerPtr.bulletCtl)
-						towerPtr.bulletCtl.resetToMaxBullet();
-				}
+            get { return towerTargetter.searchRate; }
+            set { towerTargetter.searchRate = value; }
+        }
 
-				if (fillBulletTime <= 0)
-				{
-					fillBulletTime = 0;
-				}
-			}
+        /// <summary>
+        /// Gets the targetable
+        /// </summary>
+        public Targetable trackingEnemy
+        {
+            get { return m_TrackingEnemy; }
+        }
 
-			// 
-			// 充能时间的处理
-			if( towerPtr && towerPtr.energyCtl)
+        /// <summary>
+        /// Gets or sets the attack radius
+        /// </summary>
+        public float effectRadius
+        {
+            get { return towerTargetter.effectRadius; }
+        }
+
+        public Color effectColor
+        {
+            get { return radiusEffectColor; }
+        }
+
+        public Targetter targetter
+        {
+            get { return towerTargetter; }
+        }
+
+        /// <summary>
+        /// Initializes the attack affector
+        /// </summary>
+        public override void Initialize(IAlignmentProvider affectorAlignment)
+        {
+            Initialize(affectorAlignment, -1);
+        }
+
+        /// <summary>
+        /// 返回可能存在的Targetter.
+        /// </summary>
+        /// <returns></returns>
+        public override TowerDefense.Targetting.Targetter GetTargetter()
+        {
+            return targetter;
+        }
+
+        /// <summary>
+        /// Initialises the  attack affector with a layer mask
+        /// </summary>
+        public override void Initialize(IAlignmentProvider affectorAlignment, LayerMask mask)
+        {
+            base.Initialize(affectorAlignment, mask);
+            SetUpTimers();
+
+            towerTargetter.ResetTargetter();
+            towerTargetter.alignment = affectorAlignment;
+            towerTargetter.acquiredTarget += OnAcquiredTarget;
+            towerTargetter.lostTarget += OnLostTarget;
+			GetAudioEnum();
+        }
+        private AudioEnum audioEnum;//当前音乐的种类
+
+        void GetAudioEnum()
+        {
+            if (transform.parent.name.StartsWith("GrowUpTower"))
             {
-				if( this.fInEnergy <= 0)
+                //火元素
+                audioEnum = AudioEnum.FireTAttack;
+            }
+            else if (transform.parent.name.StartsWith("BlinkTower"))
+            {
+                //木元素
+                audioEnum = AudioEnum.WoodTAttack;
+            }
+            else if (transform.parent.name.StartsWith("CopyCatTower"))
+            {
+                //水元素
+                audioEnum = AudioEnum.WaterTAttack;
+            }
+        }
+
+        void OnDestroy()
+        {
+            towerTargetter.acquiredTarget -= OnAcquiredTarget;
+            towerTargetter.lostTarget -= OnLostTarget;
+        }
+
+        void OnLostTarget()
+        {
+            m_TrackingEnemy = null;
+        }
+
+        void OnAcquiredTarget(Targetable acquiredTarget)
+        {
+            m_TrackingEnemy = acquiredTarget;
+        }
+
+        public Damager damagerProjectile
+        {
+            get { return projectile == null ? null : projectile.GetComponent<Damager>(); }
+        }
+
+        public Damager damagerProjectile1
+        {
+            get { return projectile == null ? null : projectile.GetComponent<Damager>(); }
+        }
+
+        public Damager damagerProjectile2
+        {
+            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>
+        protected virtual void SetUpTimers()
+        {
+            m_FireTimer = 1 / fireRate;
+            m_Launcher = GetComponent<ILauncher>();
+        }
+
+
+        protected void updateTowerSkillData()
+        {
+            // 
+            // 预留出来装填子弹的时间.
+            if (fillBulletTime > 0)
+            {
+                fillBulletTime -= Time.deltaTime;
+                if (fillBulletTime <= 0.3f)
                 {
-					this.energyCalTime += Time.deltaTime;
-					float process = energyCalTime % 11.0f;
-					int proint = (int)Math.Floor(process);
-					proint += towerPtr.uiProOffset;
-					towerPtr.energyCtl.SetEnergyProcessFloat( process );
-					if (proint == 10)
-					{
-						fInEnergy = 5.0f;
+                    if (towerPtr && towerPtr.bulletCtl)
+                        towerPtr.bulletCtl.resetToMaxBullet();
+                }
 
-						// 设置多倍攻击速度
-						fBackupTimer = m_FireTimer;
-						m_FireTimer = m_FireTimer / 3.0f;
+                if (fillBulletTime <= 0)
+                {
+                    fillBulletTime = 0;
+                }
+            }
 
-						towerPtr.uiProOffset = 0;
-						towerPtr.PlayEnergyEffect(true);
-					}
+            // 
+            // 充能时间的处理
+            if (towerPtr && towerPtr.energyCtl)
+            {
+                if (this.fInEnergy <= 0)
+                {
+                    this.energyCalTime += Time.deltaTime;
+                    float process = energyCalTime % 11.0f;
+                    int proint = (int)Math.Floor(process);
+                    proint += towerPtr.uiProOffset;
+                    towerPtr.energyCtl.SetEnergyProcessFloat(process);
+                    if (proint == 10)
+                    {
+                        fInEnergy = 5.0f;
+
+                        // 设置多倍攻击速度
+                        fBackupTimer = m_FireTimer;
+                        m_FireTimer = m_FireTimer / 3.0f;
+
+                        towerPtr.uiProOffset = 0;
+                        towerPtr.PlayEnergyEffect(true);
+                    }
                 }
                 else
                 {
-					fInEnergy -= Time.deltaTime;
-					if( fInEnergy <= 0)
+                    fInEnergy -= Time.deltaTime;
+                    if (fInEnergy <= 0)
                     {
                         EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.FireTowerChargeEnd);
-						fInEnergy = 0.0f;
-						this.energyCalTime = 0.0f;	
-						towerPtr.energyCtl.SetEnergyProgress(0);
+                        fInEnergy = 0.0f;
+                        this.energyCalTime = 0.0f;
+                        towerPtr.energyCtl.SetEnergyProgress(0);
 
-						// 恢复正常攻击速度 
-						m_FireTimer = fBackupTimer;
+                        // 恢复正常攻击速度 
+                        m_FireTimer = fBackupTimer;
 
-						towerPtr.PlayEnergyEffect(false);
+                        towerPtr.PlayEnergyEffect(false);
 
-					}
-				}
+                    }
+                }
 
-			}
-		}
+            }
+        }
 
-		/// <summary>
-		/// Update the timers
-		/// </summary>
-		protected virtual void Update()
-		{
-			// 处理当前Affector所在Tower对应的技能
-			updateTowerSkillData();
+        /// <summary>
+        /// Update the timers
+        /// </summary>
+        protected virtual void Update()
+        {
+            // 处理当前Affector所在Tower对应的技能
+            updateTowerSkillData();
 
-			m_FireTimer -= Time.deltaTime;
-			if( trackingEnemy == null )
-				m_TrackingEnemy = targetter.GetTarget(waveLineID, bWoodAffector);
-			if (trackingEnemy != null && m_FireTimer <= 0.0f)
-			{
-				OnFireTimer();
-				m_FireTimer = 1 / fireRate;
-
-				// 多倍攻速:
-				if (fInEnergy > 0.0f)
-					m_FireTimer = m_FireTimer / 3.0f;
-			}
-		}
-
-		/// <summary>
-		/// Fired at every poll of the fire rate timer
-		/// </summary>
-		protected virtual void OnFireTimer()
-		{
-			if (fireCondition != null)
-			{
-				if (!fireCondition())
-				{
-					return;
-				}
-			}
-			FireProjectile();
-		}
-
-		/// <summary>
-		/// Common logic when attacking
-		/// 调用攻击的核心函数,由这个函数发起真正的攻击,多目标或者单目标
-		/// </summary>
-		protected virtual void FireProjectile()
-		{
-			// 不再处理多子弹攻击,确保只有一个弹道
-			isMultiAttack = false; 
-			m_TrackingEnemy = targetter.GetTarget( waveLineID, bWoodAffector );
-			if ( (m_TrackingEnemy == null) || (fillBulletTime>0) )
-			{
-				if (this.towerPtr) 
-					towerPtr.setTowerState(false);
-				return;
-			}else
+            m_FireTimer -= Time.deltaTime;
+            if (trackingEnemy == null)
+                m_TrackingEnemy = targetter.GetTarget(waveLineID, bWoodAffector);
+            if (trackingEnemy != null && m_FireTimer <= 0.0f)
             {
-				if (this.towerPtr) 
-					towerPtr.setTowerState(true);
+                OnFireTimer();
+                m_FireTimer = 1 / fireRate;
+
+                // 多倍攻速:
+                if (fInEnergy > 0.0f)
+                    m_FireTimer = m_FireTimer / 3.0f;
+            }
+        }
+
+        /// <summary>
+        /// Fired at every poll of the fire rate timer
+        /// </summary>
+        protected virtual void OnFireTimer()
+        {
+            if (fireCondition != null)
+            {
+                if (!fireCondition())
+                {
+                    return;
+                }
+            }
+            FireProjectile();
+        }
+
+        /// <summary>
+        /// Common logic when attacking
+        /// 调用攻击的核心函数,由这个函数发起真正的攻击,多目标或者单目标
+        /// </summary>
+        protected virtual void FireProjectile()
+        {
+            // 不再处理多子弹攻击,确保只有一个弹道
+            isMultiAttack = false;
+            m_TrackingEnemy = targetter.GetTarget(waveLineID, bWoodAffector);
+            if ((m_TrackingEnemy == null) || (fillBulletTime > 0))
+            {
+                if (this.towerPtr)
+                    towerPtr.setTowerState(false);
+                return;
+            }
+            else
+            {
+                if (this.towerPtr)
+                    towerPtr.setTowerState(true);
             }
 
-			// 
-			// 处理子弹充能相关的内容
-			if( towerPtr && (towerPtr.bulletCtl != null))
+            // 
+            // 处理子弹充能相关的内容
+            if (towerPtr && (towerPtr.bulletCtl != null))
             {
-				int bnum = towerPtr.bulletCtl.decBullet();
-				// 
-				if (bnum == 0)
-				{
-					damagerProjectile.damageMulti = 5.0f;	
-					fillBulletTime = 2.0f;
-				}
+                int bnum = towerPtr.bulletCtl.decBullet();
+                // 
+                if (bnum == 0)
+                {
+                    damagerProjectile.damageMulti = 5.0f;
+                    fillBulletTime = 2.0f;
+                }
             }
 
-			if (isMultiAttack)
-			{
-				List<Targetable> enemies = towerTargetter.GetAllTargets();
-				if( (enemies != null)&&(Targetter.bSearchTarget) )
-					m_Launcher.Launch(enemies, projectile, projectilePoints,this.maxAttackNum);
-			}
-			else
-			{
-				if(Targetter.bSearchTarget )
-					m_Launcher.Launch(m_TrackingEnemy, damagerProjectile.gameObject, projectilePoints);
-			}
-			if (randomAudioSource != null)
-			{
-				if( Targetter.bSearchTarget )
-					randomAudioSource.PlayRandomClip();
-			}
-		}
+            if (isMultiAttack)
+            {
+                List<Targetable> enemies = towerTargetter.GetAllTargets();
+                if ((enemies != null) && (Targetter.bSearchTarget))
+                    m_Launcher.Launch(enemies, projectile, projectilePoints, this.maxAttackNum);
+            }
+            else
+            {
+                if (Targetter.bSearchTarget)
+                {
+                    m_Launcher.Launch(m_TrackingEnemy, damagerProjectile.gameObject, projectilePoints);
+                    AudioSourceManager.Ins.Play(audioEnum);
+                }
+            }
+            if (randomAudioSource != null)
+            {
+                if (Targetter.bSearchTarget)
+                    randomAudioSource.PlayRandomClip();
+            }
+        }
 
-		/// <summary>
-		/// A delegate to compare distances of components
-		/// </summary>
-		/// <param name="first"></param>
-		/// <param name="second"></param>
-		protected virtual int ByDistance(Targetable first, Targetable second)
-		{
-			float firstSqrMagnitude = Vector3.SqrMagnitude(first.position - epicenter.position);
-			float secondSqrMagnitude = Vector3.SqrMagnitude(second.position - epicenter.position);
-			return firstSqrMagnitude.CompareTo(secondSqrMagnitude);
-		}
+        /// <summary>
+        /// A delegate to compare distances of components
+        /// </summary>
+        /// <param name="first"></param>
+        /// <param name="second"></param>
+        protected virtual int ByDistance(Targetable first, Targetable second)
+        {
+            float firstSqrMagnitude = Vector3.SqrMagnitude(first.position - epicenter.position);
+            float secondSqrMagnitude = Vector3.SqrMagnitude(second.position - epicenter.position);
+            return firstSqrMagnitude.CompareTo(secondSqrMagnitude);
+        }
 
 #if UNITY_EDITOR
-		/// <summary>
-		/// Draws the search area
-		/// </summary>
-		void OnDrawGizmosSelected()
-		{
-			Gizmos.DrawWireSphere(epicenter.position, towerTargetter.effectRadius);
-		}
+        /// <summary>
+        /// Draws the search area
+        /// </summary>
+        void OnDrawGizmosSelected()
+        {
+            Gizmos.DrawWireSphere(epicenter.position, towerTargetter.effectRadius);
+        }
 #endif
-	}
+    }
 
-	/// <summary>
-	/// A delegate for boolean calculation logic
-	/// </summary>
-	public delegate bool Filter();
+    /// <summary>
+    /// A delegate for boolean calculation logic
+    /// </summary>
+    public delegate bool Filter();
 }
\ No newline at end of file

--
Gitblit v1.9.1