wangguan
2020-10-26 439510ca0d2da7fa0af0496d4a05096841f01f8f
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
using DG.Tweening;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
 
namespace Core.Health
{
    /// <summary>
    /// Class to visualizer the health of a damageable
    /// </summary>
    public class HealthVisualizer : 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>
        /// The object whose X-scale we change to decrease the health bar. Should have a default uniform scale
        /// </summary>
        public Transform healthBar;
        
        /// <summary>
        /// The object whose X-scale we change to increase the health bar background. Should have a default uniform scale
        /// </summary>
        public Transform backgroundBar;
 
 
        /// <summary>
        /// 处理当前的TextMeshPro.
        /// </summary>
        public TextMeshPro textMesh;
 
        /// <summary>
        /// 替换levelMaterial对应的GameObject.
        /// </summary>
        public GameObject levelQuad;
 
        public List<Material> levelMaterial;
 
        /// <summary>
        /// Whether to show this health bar even when it is full
        /// </summary>
        public bool showWhenFull;
 
        /// <summary>
        /// Camera to face the visualization at
        /// </summary>
        protected Transform m_CameraToFace;
 
        /// <summary>
        /// Damageable whose health is visualized
        /// </summary>
        protected Damageable m_Damageable;
 
 
        /// <summary>
        /// 是否反方的HealthBar.
        /// </summary>
        public bool bOpponent { get; set; }
 
        //protected int BloodTest = 1234;
 
        /// <summary>
        /// Updates the visualization of the health
        /// </summary>
        /// <param name="normalizedHealth">Normalized health value</param>
        public void UpdateHealth(float normalizedHealth)
        {
            Vector3 scale = Vector3.one;
 
            if (healthBar != null)
            {
                //scale.x = normalizedHealth;
                //healthBar.transform.localScale = scale;
                healthBar.transform.DOScaleX(normalizedHealth, 0.3f);
            }
 
            if (backgroundBar != null)
            {
                scale.x = 1;// - normalizedHealth;
                backgroundBar.transform.localScale = scale;
            }
 
            // TEST CODE: 测试场景内飘字的效果,可以继续加强这一块的效果.
            if (this.textMesh)
                this.textMesh.text = Math.Floor(m_Damageable.currentHealth).ToString();
 
            SetVisible((showWhenFull || normalizedHealth < 1.0f) && (!this.bOpponent));
        }
 
        /// <summary>
        /// Sets the visibility status of this visualiser
        /// </summary>
        public void SetVisible(bool visible)
        {
            gameObject.SetActive(visible);
        }
 
        /// <summary>
        /// Assigns the damageable, subscribing to the damaged event
        /// </summary>
        /// <param name="damageable">Damageable to assign</param>
        public void AssignDamageable(Damageable damageable)
        {
            if (m_Damageable != null)
            {
                m_Damageable.healthChanged -= OnHealthChanged;
            }
            m_Damageable = damageable;
            m_Damageable.healthChanged += OnHealthChanged;
        }
 
        /// <summary>
        /// Turns us to face the camera,公告板的算法原理:
        /// </summary>
        protected virtual void Update()
        {
            Vector3 direction = m_CameraToFace.transform.forward;
            transform.forward = -direction;
        }
 
        /// <summary>
        /// Assigns a damageable if damageableBehaviour is populated
        /// </summary>
        protected virtual void Awake()
        {
            if (damageableBehaviour != null)
            {
                AssignDamageable(damageableBehaviour.configuration);
            }
        }
 
        /// <summary>
        /// Caches the main camera
        /// </summary>
        protected virtual void Start()
        {
            m_CameraToFace = UnityEngine.Camera.main.transform;
 
        }
 
        /// <summary>
        /// 设置血条对应的等级数据。
        /// </summary>
        /// <param name="lvl"></param>
        public void SetHealthLevel( int lvl)
        {
            if (lvl < 0)
            {
                levelQuad.gameObject.SetActive(false);
                return;
            }
 
            if( this.levelQuad)
                levelQuad.GetComponent<MeshRenderer>().material = levelMaterial[lvl];
        }
 
        void OnHealthChanged(HealthChangeInfo healthChangeInfo)
        {
            UpdateHealth(m_Damageable.normalisedHealth);
        }
    }
}