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)
|
{
|
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();
|
Hide();
|
}
|
|
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);
|
}
|
}
|
}
|