River Jiang
2020-10-28 d2bc86161bf01b9ac01ba7b4b6ee7e341778c0c2
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
using Core.Utilities;
using UnityEngine;
 
namespace Core.Health
{
    /// <summary>
    /// Simple class to instantiate a ParticleSystem on a given Damageable's death
    /// 这个类只有一个功能,播放死亡特效。
    /// 这个类的函数附加到Damageable的died Action上.
    /// </summary>
    public class DeathEffect : MonoBehaviour
    {
        /// <summary>
        /// The DamageableBehaviour that will be used to assign the damageable
        /// </summary>
        [Tooltip("This field does not need to be populated here, it can be set up in code using AssignDamageable")]
        public DamageableBehaviour damageableBehaviour;
        
        /// <summary>
        /// Death particle system
        /// </summary>
        public ParticleSystem deathParticleSystemPrefab;
 
        /// <summary>
        /// World space offset of the <see cref="deathParticleSystemPrefab"/> position
        /// </summary>
        public Vector3 deathEffectOffset;
 
        /// <summary>
        /// The damageable
        /// </summary>
        protected Damageable m_Damageable;
 
        /// <summary>
        /// Subscribes to the damageable's died event
        /// </summary>
        /// <param name="damageable"></param>
        public void AssignDamageable(Damageable damageable)
        {
            if (m_Damageable != null)
            {
                m_Damageable.died -= OnDied;
            }
            m_Damageable = damageable;
            m_Damageable.died += OnDied;
        }
 
        /// <summary>
        /// If damageableBehaviour is populated, assigns the damageable
        /// </summary>
        protected virtual void Awake () 
        {
            if (damageableBehaviour != null)
            {
                AssignDamageable(damageableBehaviour.configuration);
            }
        }
 
        /// <summary>
        /// Instantiate a death particle system
        /// </summary>
        void OnDied(HealthChangeInfo healthChangeInfo)
        {
            if (deathParticleSystemPrefab == null)
            {
                return;
            }
 
            var pfx = Poolable.TryGetPoolable<ParticleSystem>(deathParticleSystemPrefab.gameObject);
            pfx.transform.position = transform.position + deathEffectOffset;
            // TEST CODE TO DELTE: ATTENTION TO OPP: 加入更加成熟的机制来加强效果:
            //pfx.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            pfx.Simulate(1.2f); // TEST CODE TO DELETE:
            pfx.Play();
        }
    }
}