chenxin
2020-11-09 172d8ffbf5fe3bdd60b3d71a3a60feeed1cb1762
Assets/Scripts/Core/Health/Damageable.cs
@@ -28,6 +28,10 @@
      // 当前的Entity用于响应各种血量相关的事件。
      public event Action<HealthChangeInfo> damaged, healed, died, healthChanged;
        /// <summary>
        /// 魔法护盾值改变
        /// </summary>
        public event Action<HealthChangeInfo> ShieldWallHealthChanged;
      /// <summary>
      /// Gets the current health.
@@ -38,6 +42,31 @@
      /// 设置无敌状态.
      /// </summary>
      public bool bInvincible { get; set; }
        /// <summary>
        /// 是否存在魔法护盾
        /// </summary>
        public bool IsExistShieldWall { get; set; }
        /// <summary>
        /// 0 -> 和agent的生命周期一致,直到死亡, > 0 (单位s)*时间内生效
        /// </summary>
        public float ShieldWallEffectiveTime { get; set; }
        /// <summary>
        /// 魔法护盾生效剩余时间,这个只针对 ShieldWallEffectTime > 0 才生效
        /// </summary>
        public float ShieldWallRemainTime { get; set; }
        /// <summary>
        /// 魔法护盾最大生命值,即初始生命值
        /// </summary>
        public float ShieldWallMaxHealth { get; set; }
        /// <summary>
        /// 魔法护盾当前生命值,当 <= 0时直接移除护盾效果
        /// </summary>
        public float ShieldWallCurrentHealth { get; set; }
      /// <summary>
      /// Gets the normalised health.
@@ -53,6 +82,18 @@
            }
            return currentHealth / maxHealth;
         }
        }
        /// <summary>
        /// 清理魔法护盾
        /// </summary>
        public void ClearShieldWall()
        {
            IsExistShieldWall = false;
            ShieldWallMaxHealth = 0;
            ShieldWallCurrentHealth = 0;
            ShieldWallRemainTime = 0;
            ShieldWallEffectiveTime = 0;
      }
      /// <summary>
@@ -141,6 +182,36 @@
      }
      /// <summary>
        /// 获得一个魔法护盾
        /// </summary>
        /// <param name="maxHealth">护盾的最大生命值</param>
        /// <param name="effectiveTime">生效时间 0 -> 和Agent生命周期一致,> 0 *(单位ms)时间内生效</param>
        public void AddShieldWall(float maxHealth, float effectiveTime)
        {
            // 如果重复获得护盾,直接全部重置数据,移除之前的护盾效果,重新获得一个护盾
            IsExistShieldWall = true;
            ShieldWallEffectiveTime = effectiveTime;
            if (effectiveTime > 0)
                ShieldWallRemainTime = effectiveTime / 1000f;
            ShieldWallMaxHealth = ShieldWallCurrentHealth = maxHealth;
            HealthChangeInfo info = new HealthChangeInfo
            {
                damageable = this,
                ShieldWallMaxHealth = maxHealth,
                ShieldWallCurrentHealth = maxHealth,
                IsExistShieldWall = true
            };
            if (ShieldWallHealthChanged != null)
            {
                ShieldWallHealthChanged(info);
            }
        }
        /// <summary>
      /// Use the alignment to see if taking damage is a valid action
      /// </summary>
      /// <param name="damage">
@@ -214,8 +285,32 @@
      /// <param name="info">HealthChangeInfo for this change</param>
      protected void ChangeHealth(float healthIncrement, ref HealthChangeInfo info)
      {
            bool changeHealth = false;
            float increment = healthIncrement;
            if (IsExistShieldWall)
            {
                info.ShieldWallOldHealth = ShieldWallCurrentHealth;
                ShieldWallCurrentHealth += healthIncrement;
                if (ShieldWallCurrentHealth == 0)
                    info.IsExistShieldWall = IsExistShieldWall = false;
                else if (ShieldWallCurrentHealth < 0)
                {
                    changeHealth = true;
                    info.IsExistShieldWall = IsExistShieldWall = false;
                    // 护盾血量不够减
                    increment = ShieldWallCurrentHealth;
                    info.ShieldWallCurrentHealth = ShieldWallCurrentHealth = 0;
                }
            }
            else
                changeHealth = true;
            if (changeHealth)
            {
         info.oldHealth = currentHealth;
         currentHealth += healthIncrement;
                currentHealth += increment;
         currentHealth = Mathf.Clamp(currentHealth, 0f, maxHealth);
         info.newHealth = currentHealth;
         
@@ -224,6 +319,7 @@
            healthChanged(info);
         }
      }
        }
      /// <summary>
      /// A helper method for null checking actions