wangguan
2020-12-12 c6c7994aa62fc9a619b78b57043905538610ab0b
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
using DG.Tweening;
using UnityEngine.UI;
using UnityEngine;
using TowerDefense.Agents;
using Core.Health;
 
/**
 * Agent血条
 * @Author: chenxin
 * @Date: 2020-12-03 17:09:45
 */
namespace KTGMGemClient
{
    public class AgentBlood : MonoBehaviour
    {
        private Agent target;
 
        public Agent Target
        {
            get { return target; }
            set
            {
                target = value;
 
                if (target != null)
                    SetBase();
            }
        }
 
        public Image BloodImg;
 
        private float offsetY = 50f;
 
        private float scale = 1f;
 
        private Tweener tween;
 
        /// <summary>
        /// 简单设置一下血条大小的适配
        /// </summary>
        private void SetBase()
        {
            if (Target != null)
            {
                if (Target.EnemyData != null)
                {
                    switch (Target.EnemyData.id)
                    {
                        case 1: // 火
                            scale = 0.8f;
                            offsetY = 40f;
                            break;
                        case 2: // 水
                            scale = 1f;
                            break;
                        case 3: // 木
                            scale = 0.5f;
                            offsetY = 20f;
                            break;
                    }
                }
 
                transform.localScale = new Vector3(scale, 0.7f, scale);
            }
        }
 
        private void Start()
        {
            BloodImg.fillAmount = 1;
        }
 
        // Update is called once per frame
        private void Update()
        {
            if (!gameObject.activeInHierarchy) return;
 
            Camera camera = GameObject.Find("SceneCamera3D").GetComponent<Camera>();
            Vector3 screenPos = camera.WorldToScreenPoint(Target.position);
            screenPos.y += offsetY;
            transform.position = screenPos;
        }
 
        public void SetCurrentBlood(float value, bool useTween = true)
        {
            if (useTween)
                tween = DOTween.To(() => BloodImg.fillAmount, (float v) => BloodImg.fillAmount = v, value, 0.3f);
            else
                BloodImg.fillAmount = value;
        }
 
        public void OnDied(HealthChangeInfo info)
        {
            if (tween != null)
                tween.Kill();
 
            if (gameObject != null)
                Destroy(gameObject);
        }
 
        public void OnHealthChanged(HealthChangeInfo info)
        {
            if (info.absHealthDifference > 0)
            {
                if (!gameObject.activeInHierarchy && info.newHealth > 0.0001f)
                    gameObject.SetActive(true);
 
                float val = Mathf.Clamp01(info.newHealth / info.damageable.maxHealth);
                SetCurrentBlood(val);
            }
        }
 
        public void Hide()
        {
            gameObject.SetActive(false);
        }
    }
}