wangguan
2020-12-10 5f6fb6dccd1330b5b0bcb2d721167a6ac062f3ad
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using UnityEngine;
using KTGMGemClient;
using TowerDefense.Agents;
 
namespace Core.Health
{
    /// <summary>
    /// Abstract class for any MonoBehaviours that can take damage
    /// 任何会受到伤害的MonoBehaviour的抽象类
    /// </summary>
    public class DamageableBehaviour : MonoBehaviour
    {
        /// <summary>
        /// The Damageable object
        /// </summary>
        public Damageable configuration;
 
        /// <summary>
        /// Gets whether this <see cref="DamageableBehaviour" /> is dead.
        /// </summary>
        /// <value>True if dead</value>
        public bool isDead
        {
            get { return configuration.isDead; }
        }
 
        /// <summary>
        /// The position of the transform
        /// </summary>
        public virtual Vector3 position
        {
            get { return transform.position; }
        }
 
        /// <summary>
        /// 获取当前Agent对应的HealthValue.
        /// </summary>
        public float healthVal
        {
            get { return this.configuration.currentHealth; }
        }
 
        /// <summary>
        /// Occurs when damage is taken
        /// </summary>
        public event Action<HitInfo> hit;
 
        /// <summary>
        /// Event that is fired when this instance is removed, such as when pooled or destroyed
        /// </summary>
        public event Action<DamageableBehaviour> removed;
 
        /// <summary>
        /// Event that is fired when this instance is killed
        /// </summary>
        public event Action<DamageableBehaviour> died;
 
        /// <summary>
        /// Takes the damage and also provides a position for the damage being dealt
        /// </summary>
        /// <param name="damageValue">Damage value.</param>
        /// <param name="damagePoint">Damage point.</param>
        /// <param name="alignment">Alignment value</param>
        /// <param name="attributeId">标注产生伤害的塔位属性ID</param>
        public virtual void TakeDamage(float damageValue, Vector3 damagePoint, IAlignmentProvider alignment, int attributeId = 0)
        {
            HealthChangeInfo info = new HealthChangeInfo();
            info.attributeId = attributeId;
            configuration.TakeDamage(damageValue, alignment, ref info);
            var damageInfo = new HitInfo(info, damagePoint);
            EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessAgentTaskDamage, damageInfo.healthChangeInfo.absHealthDifference);
            if (attributeId == 0)
                EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessOneHit);
 
            if (hit != null)
            {
                hit(damageInfo);
            }
        }
 
        protected virtual void Awake()
        {
            configuration.Init();
            configuration.died += OnConfigurationDied;
        }
 
        /// <summary>
        /// Kills this damageable
        /// </summary>
        protected virtual void Kill()
        {
            HealthChangeInfo healthChangeInfo = new HealthChangeInfo();
            configuration.TakeDamage(configuration.currentHealth, null, ref healthChangeInfo);
        }
 
 
        /// <summary>
        /// Removes this damageable without killing it
        /// </summary>
        public virtual void Remove()
        {
            // Set health to zero so that this behaviour appears to be dead. This will not fire death events
            configuration.SetHealth(0);
            OnRemoved();
        }
 
        /// <summary>
        /// Fires kill events
        /// </summary>
        protected virtual void OnDeath()
        {
            if (died != null)
            {
                died(this);
            }
        }
 
        /// <summary>
        /// Fires the removed event
        /// </summary>
        protected void OnRemoved()
        {
            if (removed != null)
            {
                removed(this);
            }
        }
 
        public virtual bool isAgent()
        {
            return false;
        }
 
        public virtual void PlayDeath()
        {
            return;
        }
 
        /// <summary>
        /// Event fired when Damageable takes critical damage
        /// </summary>
        void OnConfigurationDied(HealthChangeInfo changeInfo)
        {
            OnDeath();
 
            if (!this.isAgent())
                Remove();
            else
                this.PlayDeath();
        }
    }
}