using DG.Tweening;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace Core.Health
{
///
/// Class to visualizer the health of a damageable
///
public class HealthVisualizer : MonoBehaviour
{
///
/// The DamageableBehaviour that will be used to assign the damageable
///
[Tooltip("This field does not need to be populated here, it can be set up in code using AssignDamageable")]
public DamageableBehaviour damageableBehaviour;
///
/// The object whose X-scale we change to decrease the health bar. Should have a default uniform scale
///
public Transform healthBar;
///
/// The object whose X-scale we change to increase the health bar background. Should have a default uniform scale
///
public Transform backgroundBar;
///
/// 处理当前的TextMeshPro.
///
public TextMeshPro textMesh;
///
/// 替换levelMaterial对应的GameObject.
///
public GameObject levelQuad;
public List levelMaterial;
///
/// Whether to show this health bar even when it is full
///
public bool showWhenFull;
///
/// Camera to face the visualization at
///
protected Transform m_CameraToFace;
///
/// Damageable whose health is visualized
///
protected Damageable m_Damageable;
///
/// 是否反方的HealthBar.
///
public bool bOpponent { get; set; }
//protected int BloodTest = 1234;
///
/// Updates the visualization of the health
///
/// Normalized health value
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));
}
///
/// Sets the visibility status of this visualiser
///
public void SetVisible(bool visible)
{
gameObject.SetActive(visible);
}
///
/// Assigns the damageable, subscribing to the damaged event
///
/// Damageable to assign
public void AssignDamageable(Damageable damageable)
{
if (m_Damageable != null)
{
m_Damageable.healthChanged -= OnHealthChanged;
m_Damageable.ShieldWallHealthChanged -= OnShieldWallHealthChanged;
}
m_Damageable = damageable;
m_Damageable.healthChanged += OnHealthChanged;
m_Damageable.ShieldWallHealthChanged += OnShieldWallHealthChanged;
}
///
/// Turns us to face the camera,公告板的算法原理:
///
protected virtual void Update()
{
Vector3 direction = m_CameraToFace.transform.forward;
transform.forward = -direction;
}
///
/// Assigns a damageable if damageableBehaviour is populated
///
protected virtual void Awake()
{
if (damageableBehaviour != null)
{
AssignDamageable(damageableBehaviour.configuration);
}
}
///
/// Caches the main camera
///
protected virtual void Start()
{
m_CameraToFace = UnityEngine.Camera.main.transform;
}
///
/// 设置血条对应的等级数据。
///
///
public void SetHealthLevel(int lvl)
{
if (levelQuad.activeSelf)
{
levelQuad.gameObject.SetActive(false);
}
return;
if (lvl < 0)
{
levelQuad.gameObject.SetActive(false);
return;
}
if (this.levelQuad)
levelQuad.GetComponent().material = levelMaterial[lvl];
}
void OnHealthChanged(HealthChangeInfo healthChangeInfo)
{
UpdateHealth(m_Damageable.normalisedHealth);
}
private void OnShieldWallHealthChanged(HealthChangeInfo healthChangeInfo)
{
Debug.Log("--------------------- 获得魔法护盾 ---------------------");
}
}
}