chenxin
2020-11-27 c49c0e39e4c11a438014a0b0732ecf0968fceb5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Core.Health;
using UnityEngine;
 
namespace ActionGameFramework.Health
{
    /// <summary>
    /// Damage zone - an area that receives damage and gives it to the damageable behaviour
    /// Abstract base class because the actual damage mechanic can be based on Collisions or Triggers or some other mechanism
    /// </summary>
    [RequireComponent(typeof(Collider))]
    public abstract class DamageZone : MonoBehaviour
    {
        /// <summary>
        /// Allow the user to define the damageable behaviour that this zone corresponds to.
        /// This allows multiple damage zones to reference one damageable behaviour
        /// allowing locational damage. e.g. headshots.
        /// </summary>
        [Tooltip("If this is empty, DamageZone will try to use a DamageableBehaviour on the same object.")]
        public DamageableBehaviour damageableBehaviour;
 
        /// <summary>
        /// Allow scaling of the damage. This allows different zones to take different damage from same Damager. e.g. headshots
        /// </summary>
        public float damageScale = 1f;
 
        /// <summary>
        /// Scales the damage.
        /// </summary>
        /// <returns>The damage.</returns>
        /// <param name="damage">Damage.</param>
        protected float ScaleDamage(float damage)
        {
            return damageScale * damage;
        }
 
        /// <summary>
        /// Looks for the damageableBehaviour if it is not already assigned
        /// It may be assigned in editor or from a previous LazyLoad() call
        /// </summary>
        protected void LazyLoad()
        {
            if (damageableBehaviour != null)
            {
                return;
            }
 
            damageableBehaviour = GetComponent<DamageableBehaviour>();
        }
    }
}