chenxin
2020-11-11 f2f4e1e45981bb294a5221ade7b4646cc3e29d35
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using UnityEngine;
 
namespace Core.Health
{
    /// <summary>
    /// Damageable class for handling health using events
    /// Could be used on Players or enemies or even destructable world objects
    /// </summary>
    [Serializable]
    public class Damageable
    {
        /// <summary>
        /// The max health of this instance
        /// </summary>
        public float maxHealth;
 
        public float startingHealth;
 
        /// <summary>
        /// The alignment of the damager
        /// </summary>
        public SerializableIAlignmentProvider alignment;
 
        // events
        public event Action reachedMaxHealth;
 
        // 当前的Entity用于响应各种血量相关的事件。
        public event Action<HealthChangeInfo> damaged, healed, died, healthChanged;
 
        /// <summary>
        /// 魔法护盾值改变
        /// </summary>
        public event Action<HealthChangeInfo> ShieldWallHealthChanged;
 
        /// <summary>
        /// Gets the current health.
        /// </summary>
        public float currentHealth { protected set; get; }
 
        /// <summary>
        /// 设置无敌状态.
        /// </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.
        /// </summary>
        public float normalisedHealth
        {
            get
            {
                if (Math.Abs(maxHealth) <= Mathf.Epsilon)
                {
                    Debug.LogError("Max Health is 0");
                    maxHealth = 1f;
                }
                return currentHealth / maxHealth;
            }
        }
 
        /// <summary>
        /// 清理魔法护盾
        /// </summary>
        public void ClearShieldWall()
        {
            IsExistShieldWall = false;
            ShieldWallMaxHealth = 0;
            ShieldWallCurrentHealth = 0;
            ShieldWallRemainTime = 0;
            ShieldWallEffectiveTime = 0;
        }
 
        /// <summary>
        /// Gets the <see cref="IAlignmentProvider"/> of this instance
        /// </summary>
        public IAlignmentProvider alignmentProvider
        {
            get
            {
                return alignment != null ? alignment.GetInterface() : null;
            }
        }
 
        /// <summary>
        /// Gets whether this instance is dead.
        /// </summary>
        public bool isDead
        {
            get { return currentHealth < 0.00001f; }
        }
 
        /// <summary>
        /// Gets whether this instance is at max health.
        /// </summary>
        public bool isAtMaxHealth
        {
            get { return Mathf.Approximately(currentHealth, maxHealth); }
        }
 
 
        /// <summary>
        /// Init this instance
        /// </summary>
        public virtual void Init()
        {
            currentHealth = startingHealth;
        }
 
        /// <summary>
        /// Sets the max health and starting health to the same value
        /// </summary>
        public void SetMaxHealth(float health)
        {
            if (health <= 0)
            {
                return;
            }
            maxHealth = startingHealth = health;
        }
 
        /// <summary>
        /// Sets the max health and starting health separately
        /// </summary>
        public void SetMaxHealth(float health, float startingHealth)
        {
            if (health <= 0)
            {
                return;
            }
            maxHealth = health;
            this.startingHealth = startingHealth;
        }
 
        /// <summary>
        /// Sets this instance's health directly.
        /// </summary>
        /// <param name="health">
        /// The value to set <see cref="currentHealth"/> to
        /// </param>
        public void SetHealth(float health)
        {
            var info = new HealthChangeInfo
            {
                damageable = this,
                newHealth = health,
                oldHealth = currentHealth
            };
 
            currentHealth = health;
            info.oldHealth = health;
 
            if (healthChanged != null)
            {
                healthChanged(info);
            }
        }
 
        /// <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">
        /// The damage to take
        /// </param>
        /// <param name="damageAlignment">
        /// The alignment of the other combatant
        /// </param>
        /// <param name="output">
        /// The output data if there is damage taken
        /// </param>
        /// <returns>
        /// <value>true if this instance took damage</value>
        /// <value>false if this instance was already dead, or the alignment did not allow the damage</value>
        /// </returns>
        public bool TakeDamage(float damage, IAlignmentProvider damageAlignment, ref HealthChangeInfo output)
        {
 
            /*output = new HealthChangeInfo
            {
                damageAlignment = damageAlignment, damageable = this,
                newHealth = currentHealth, oldHealth = currentHealth
            };*/
            output.damageAlignment = damageAlignment;
            output.damageable = this;
            output.newHealth = currentHealth;
            output.oldHealth = currentHealth;
 
            // 无敌状态下,不可伤害.
            if (this.bInvincible)
                return false;
 
            bool canDamage = damageAlignment == null || alignmentProvider == null ||
                             damageAlignment.CanHarm(alignmentProvider);
 
            if (isDead || !canDamage)
            {
                return false;
            }
 
            ChangeHealth(-damage, ref output);
            SafelyDoAction(damaged, output);
            if (isDead)
            {
                SafelyDoAction(died, output);
            }
            return true;
        }
 
        /// <summary>
        /// Logic for increasing the health.
        /// </summary>
        /// <param name="health">Health.</param>
        public HealthChangeInfo IncreaseHealth(float health)
        {
            var info = new HealthChangeInfo { damageable = this };
            ChangeHealth(health, ref info);
            SafelyDoAction(healed, info);
            if (isAtMaxHealth)
            {
                SafelyDoAction(reachedMaxHealth);
            }
 
            return info;
        }
 
        /// <summary>
        /// Changes the health.
        /// </summary>
        /// <param name="healthIncrement">Health increment.</param>
        /// <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 += increment;
                currentHealth = Mathf.Clamp(currentHealth, 0f, maxHealth);
                info.newHealth = currentHealth;
 
                if (healthChanged != null)
                {
                    healthChanged(info);
                }
            }
        }
 
        /// <summary>
        /// A helper method for null checking actions
        /// </summary>
        /// <param name="action">Action to be done</param>
        protected void SafelyDoAction(Action action)
        {
            if (action != null)
            {
                action();
            }
        }
 
        /// <summary>
        /// A helper method for null checking actions
        /// </summary>
        /// <param name="action">Action to be done</param>
        /// <param name="info">The HealthChangeInfo to be passed to the Action</param>
        protected void SafelyDoAction(Action<HealthChangeInfo> action, HealthChangeInfo info)
        {
            if (action != null)
            {
                action(info);
            }
        }
    }
}