wangguan
2020-12-08 3d13f2e8a23602aedb8adf5d2d02e377a330a61b
Assets/Scripts/Core/Health/HealthVisualizer.cs
@@ -6,161 +6,173 @@
namespace Core.Health
{
   /// <summary>
   /// Class to visualizer the health of a damageable
   /// </summary>
   public class HealthVisualizer : MonoBehaviour
   {
      /// <summary>
      /// The DamageableBehaviour that will be used to assign the damageable
      /// </summary>
      [Tooltip("This field does not need to be populated here, it can be set up in code using AssignDamageable")]
      public DamageableBehaviour damageableBehaviour;
      /// <summary>
      /// The object whose X-scale we change to decrease the health bar. Should have a default uniform scale
      /// </summary>
      public Transform healthBar;
      /// <summary>
      /// The object whose X-scale we change to increase the health bar background. Should have a default uniform scale
      /// </summary>
      public Transform backgroundBar;
    /// <summary>
    /// Class to visualizer the health of a damageable
    /// </summary>
    public class HealthVisualizer : MonoBehaviour
    {
        /// <summary>
        /// The DamageableBehaviour that will be used to assign the damageable
        /// </summary>
        [Tooltip("This field does not need to be populated here, it can be set up in code using AssignDamageable")]
        public DamageableBehaviour damageableBehaviour;
        /// <summary>
        /// The object whose X-scale we change to decrease the health bar. Should have a default uniform scale
        /// </summary>
        public Transform healthBar;
        /// <summary>
        /// The object whose X-scale we change to increase the health bar background. Should have a default uniform scale
        /// </summary>
        public Transform backgroundBar;
      /// <summary>
      /// 处理当前的TextMeshPro.
      /// </summary>
      public TextMeshPro textMesh;
        /// <summary>
        /// 处理当前的TextMeshPro.
        /// </summary>
        public TextMeshPro textMesh;
      /// <summary>
      /// 替换levelMaterial对应的GameObject.
      /// </summary>
      public GameObject levelQuad;
        /// <summary>
        /// 替换levelMaterial对应的GameObject.
        /// </summary>
        public GameObject levelQuad;
      public List<Material> levelMaterial;
        public List<Material> levelMaterial;
      /// <summary>
      /// Whether to show this health bar even when it is full
      /// </summary>
      public bool showWhenFull;
        /// <summary>
        /// Whether to show this health bar even when it is full
        /// </summary>
        public bool showWhenFull;
      /// <summary>
      /// Camera to face the visualization at
      /// </summary>
      protected Transform m_CameraToFace;
        /// <summary>
        /// Camera to face the visualization at
        /// </summary>
        protected Transform m_CameraToFace;
      /// <summary>
      /// Damageable whose health is visualized
      /// </summary>
      protected Damageable m_Damageable;
        /// <summary>
        /// Damageable whose health is visualized
        /// </summary>
        protected Damageable m_Damageable;
      /// <summary>
      /// 是否反方的HealthBar.
      /// </summary>
      public bool bOpponent { get; set; }
        /// <summary>
        /// 是否反方的HealthBar.
        /// </summary>
        public bool bOpponent { get; set; }
      //protected int BloodTest = 1234;
        //protected int BloodTest = 1234;
      /// <summary>
      /// Updates the visualization of the health
      /// </summary>
      /// <param name="normalizedHealth">Normalized health value</param>
      public void UpdateHealth(float normalizedHealth)
      {
         Vector3 scale = Vector3.one;
         if (healthBar != null)
         {
            //scale.x = normalizedHealth;
            //healthBar.transform.localScale = scale;
            healthBar.transform.DOScaleX(normalizedHealth, 0.3f);
         }
         if (backgroundBar != null)
         {
            scale.x = 1;// - normalizedHealth;
            backgroundBar.transform.localScale = scale;
         }
         // TEST CODE: 测试场景内飘字的效果,可以继续加强这一块的效果.
         if (this.textMesh)
            this.textMesh.text = Math.Floor(m_Damageable.currentHealth).ToString();
         SetVisible((showWhenFull || normalizedHealth < 1.0f) && (!this.bOpponent));
      }
      /// <summary>
      /// Sets the visibility status of this visualiser
      /// </summary>
      public void SetVisible(bool visible)
      {
         gameObject.SetActive(visible);
      }
      /// <summary>
      /// Assigns the damageable, subscribing to the damaged event
      /// </summary>
      /// <param name="damageable">Damageable to assign</param>
      public void AssignDamageable(Damageable damageable)
      {
         if (m_Damageable != null)
         {
            m_Damageable.healthChanged -= OnHealthChanged;
         }
         m_Damageable = damageable;
         m_Damageable.healthChanged += OnHealthChanged;
      }
      /// <summary>
      /// Turns us to face the camera,公告板的算法原理:
      /// </summary>
      protected virtual void Update()
      {
         Vector3 direction = m_CameraToFace.transform.forward;
         transform.forward = -direction;
      }
      /// <summary>
      /// Assigns a damageable if damageableBehaviour is populated
      /// </summary>
      protected virtual void Awake()
      {
         if (damageableBehaviour != null)
         {
            AssignDamageable(damageableBehaviour.configuration);
         }
      }
      /// <summary>
      /// Caches the main camera
      /// </summary>
      protected virtual void Start()
      {
         m_CameraToFace = UnityEngine.Camera.main.transform;
      }
      /// <summary>
      /// 设置血条对应的等级数据。
      /// </summary>
      /// <param name="lvl"></param>
      public void SetHealthLevel( int lvl)
        /// <summary>
        /// Updates the visualization of the health
        /// </summary>
        /// <param name="normalizedHealth">Normalized health value</param>
        public void UpdateHealth(float normalizedHealth)
        {
         if (lvl < 0)
         {
            levelQuad.gameObject.SetActive(false);
            return;
         }
            Vector3 scale = Vector3.one;
         if( this.levelQuad)
            levelQuad.GetComponent<MeshRenderer>().material = levelMaterial[lvl];
            if (healthBar != null)
            {
                //scale.x = normalizedHealth;
                //healthBar.transform.localScale = scale;
                healthBar.transform.DOScaleX(normalizedHealth, 0.3f);
            }
            if (backgroundBar != null)
            {
                scale.x = 1;// - normalizedHealth;
                backgroundBar.transform.localScale = scale;
            }
            // TEST CODE: 测试场景内飘字的效果,可以继续加强这一块的效果.
            if (this.textMesh)
                this.textMesh.text = Math.Floor(m_Damageable.currentHealth).ToString();
            SetVisible((showWhenFull || normalizedHealth < 1.0f) && (!this.bOpponent));
        }
      void OnHealthChanged(HealthChangeInfo healthChangeInfo)
      {
         UpdateHealth(m_Damageable.normalisedHealth);
      }
   }
        /// <summary>
        /// Sets the visibility status of this visualiser
        /// </summary>
        public void SetVisible(bool visible)
        {
            gameObject.SetActive(visible);
        }
        /// <summary>
        /// Assigns the damageable, subscribing to the damaged event
        /// </summary>
        /// <param name="damageable">Damageable to assign</param>
        public void AssignDamageable(Damageable damageable)
        {
            if (m_Damageable != null)
            {
                m_Damageable.healthChanged -= OnHealthChanged;
                m_Damageable.ShieldWallHealthChanged -= OnShieldWallHealthChanged;
            }
            m_Damageable = damageable;
            m_Damageable.healthChanged += OnHealthChanged;
            m_Damageable.ShieldWallHealthChanged += OnShieldWallHealthChanged;
        }
        /// <summary>
        /// Turns us to face the camera,公告板的算法原理:
        /// </summary>
        protected virtual void Update()
        {
            Vector3 direction = m_CameraToFace.transform.forward;
            transform.forward = -direction;
        }
        /// <summary>
        /// Assigns a damageable if damageableBehaviour is populated
        /// </summary>
        protected virtual void Awake()
        {
            if (damageableBehaviour != null)
            {
                AssignDamageable(damageableBehaviour.configuration);
            }
        }
        /// <summary>
        /// Caches the main camera
        /// </summary>
        protected virtual void Start()
        {
            m_CameraToFace = UnityEngine.Camera.main.transform;
        }
        /// <summary>
        /// 设置血条对应的等级数据。
        /// </summary>
        /// <param name="lvl"></param>
        public void SetHealthLevel(int lvl)
        {
            if (levelQuad.activeSelf)
            {
                levelQuad.gameObject.SetActive(false);
            }
            return;
            if (lvl < 0)
            {
                levelQuad.gameObject.SetActive(false);
                return;
            }
            if (this.levelQuad)
                levelQuad.GetComponent<MeshRenderer>().material = levelMaterial[lvl];
        }
        void OnHealthChanged(HealthChangeInfo healthChangeInfo)
        {
            UpdateHealth(m_Damageable.normalisedHealth);
        }
        private void OnShieldWallHealthChanged(HealthChangeInfo healthChangeInfo)
        {
            Debug.Log("--------------------- 获得魔法护盾 ---------------------");
        }
    }
}