Ejemplo n.º 1
0
        /// <summary>
        /// The object has been damaged. Update the health and shield. Execute the OnDeath events if the health and shield is equal to zero.
        /// </summary>
        /// <param name="amount">The amount of damage taken.</param>
        /// <param name="position">The position of the damage.</param>
        /// <param name="force">The amount of force applied to the object while taking the damage.</param>
        /// <param name="radius">The radius of the explosive damage. If 0 then a non-exposive force will be used.</param>
        /// <param name="attacker">The GameObject that did the damage.</param>
        /// <param name="hitGameObject">The GameObject that was hit.</param>
        private void DamageLocal(float amount, Vector3 position, Vector3 force, float radius, GameObject attacker, GameObject hitGameObject)
        {
            // Add a multiplier if a particular GameObject was hit. Do not apply a multiplier if the damage is applied through a radius because multiple
            // GameObjects are hit.
            if (radius == 0 && hitGameObject != null)
            {
                DamageMultiplier damageMultiplier;
                if (m_DamageMultiplierMap != null && m_DamageMultiplierMap.TryGetValue(hitGameObject, out damageMultiplier))
                {
                    amount *= damageMultiplier.Multiplier;
                }
            }

            // Apply the damage to the shield first because the shield can regenrate.
            if (m_CurrentShield > 0)
            {
                var shieldAmount = Mathf.Min(amount, m_CurrentShield);
                amount -= shieldAmount;
                SetShieldAmount(m_CurrentShield - shieldAmount);
            }

            // Decrement the health by remaining amount after the shield has taken damage.
            if (m_CurrentHealth > 0)
            {
                SetHealthAmount(m_CurrentHealth - Mathf.Min(amount, m_CurrentHealth));
            }

            // Apply a force to the hit rigidbody if the force is greater than 0.
            if (m_Rigidbody != null && !m_Rigidbody.isKinematic && force.sqrMagnitude > 0)
            {
                if (radius == 0)
                {
                    m_Rigidbody.AddForceAtPosition(force, position);
                }
                else
                {
                    m_Rigidbody.AddExplosionForce(force.magnitude, position, radius);
                }
            }

            // Let other interested objects know that the object took damage.
            EventHandler.ExecuteEvent <float, Vector3, Vector3, GameObject>(m_GameObject, "OnHealthDamageDetails", amount, position, force, attacker);

            // The shield should stop regenerating when the object is taking damage.
            Scheduler.Cancel(ref m_ShieldRegenerativeEvent);

            // The object is dead when there is no more health or shield.
            if (!IsAlive())
            {
                Die(position, force, attacker);
                // Regenerate the shield if the unit is still alive after taking damage. The server should be the only object regenerating the shield if on the network.
#if ENABLE_MULTIPLAYER
            }
            else if (m_MaxShield > 0 && isServer)
            {
#else
            }
            else if (m_MaxShield > 0)
            {
#endif
                m_ShieldRegenerativeEvent = Scheduler.Schedule(m_ShieldRegenerativeInitialWait, RegenerateShield);
            }
        }