2025-05-18 01:04:31 +08:00

298 lines
7.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2020-05-31
// Description :
// Last Modified By : Kimch
// Last Modified On :
// ***********************************************************************
// <copyright file= "BloodBoard" company="kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
namespace G
{
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class BloodBoard : KUIWidget
{
[KUIFlag]
#pragma warning disable CS0649 // 从未对字段“BloodBoard._tmpName”赋值字段将一直保持其默认值 null
private TextMeshProUGUI _tmpName;
#pragma warning restore CS0649 // 从未对字段“BloodBoard._tmpName”赋值字段将一直保持其默认值 null
[KUIFlag]
#pragma warning disable CS0649 // 从未对字段“BloodBoard._tmpHpValue”赋值字段将一直保持其默认值 null
private TextMeshProUGUI _tmpHpValue;
#pragma warning restore CS0649 // 从未对字段“BloodBoard._tmpHpValue”赋值字段将一直保持其默认值 null
[KUIFlag]
#pragma warning disable CS0649 // 从未对字段“BloodBoard._imgIcon”赋值字段将一直保持其默认值 null
private Image _imgIcon;
#pragma warning restore CS0649 // 从未对字段“BloodBoard._imgIcon”赋值字段将一直保持其默认值 null
[KUIFlag]
#pragma warning disable CS0649 // 从未对字段“BloodBoard._sliHp”赋值字段将一直保持其默认值 null
private Slider _sliHp;
#pragma warning restore CS0649 // 从未对字段“BloodBoard._sliHp”赋值字段将一直保持其默认值 null
[KUIFlag]
#pragma warning disable CS0649 // 从未对字段“BloodBoard._imgHpFill”赋值字段将一直保持其默认值 null
private Image _imgHpFill;
#pragma warning restore CS0649 // 从未对字段“BloodBoard._imgHpFill”赋值字段将一直保持其默认值 null
[KUIFlag]
#pragma warning disable CS0649 // 从未对字段“BloodBoard._imgHpFill2”赋值字段将一直保持其默认值 null
private Image _imgHpFill2;
#pragma warning restore CS0649 // 从未对字段“BloodBoard._imgHpFill2”赋值字段将一直保持其默认值 null
[KUIFlag]
#pragma warning disable CS0649 // 从未对字段“BloodBoard._tmpName”赋值字段将一直保持其默认值 null
private TextMeshProUGUI _tmpPropValue;
#pragma warning restore CS0649 // 从未对字段“BloodBoard._tmpName”赋值字段将一直保持其默认值 null
private RectTransform _rectTransform;
public bool fixedPoint
{
get;
set;
}
#region Unity
private void Awake()
{
SetViewData();
_rectTransform = this.GetComponent<RectTransform>();
}
public void Reset()
{
this.gameObject.SetActive(false);
}
private void LateUpdate()
{
if (fixedPoint)
{
UpdateMultiHp();
}
else
{
if (_target)
{
_position = _target.position;
_position += _targetOffset;
}
Vector3 screenPosition = RectTransformUtility.WorldToScreenPoint(Camera.main, _position);
_rectTransform.anchoredPosition = BoardManager.ScreenPointToLocalPointInRectangle(screenPosition);
}
}
#endregion
#region Method
public void SetIcon(Sprite sprite)
{
if (_imgIcon)
{
_imgIcon.gameObject.SetActive(true);
_imgIcon.overrideSprite = sprite;
}
}
public void SetName(string name)
{
if (_tmpName)
{
_tmpName.gameObject.SetActive(true);
_tmpName.text = name;
}
}
/// <summary>
///
/// </summary>
public void HideName()
{
_tmpName.gameObject.SetActive(false);
}
public void HidePropValue()
{
_tmpPropValue.gameObject.SetActive(false);
}
/// <summary>
///
/// </summary>
public void HideSlider()
{
_sliHp.gameObject.SetActive(false);
}
public void SetHpValue(int curValue, int maxValue)
{
_tmpHpValue.text = $"{curValue}/{maxValue}";
_sliHp.value = curValue / (float)maxValue;
}
private int _curValue;
private int _maxValue;
private int _unitValue;
private int _targetValue;
private int _maxMulti;
/// <summary>
///
/// </summary>
/// <param name="curValue"></param>
/// <param name="maxValue"></param>
/// <param name="unitValue"></param>
public void SetMultiHp(int curValue, int maxValue, int unitValue = 1000)
{
unitValue = 1000;
_unitValue = unitValue;
_targetValue = Mathf.Max(0, curValue);
_maxValue = Mathf.Max(1, maxValue);
_maxMulti = Mathf.CeilToInt(_maxValue / (float)unitValue);
if (curValue == maxValue)
{
_curValue = Mathf.Max(0, curValue);
UpdateMultiHp(true);
}
}
static readonly Color[] _RomdomColor = new Color[]
{
Color.red,
Color.yellow,
Color.green,
Color.cyan,
Color.magenta,
};
private void UpdateMultiHp(bool force = false)
{
if (_curValue != _targetValue)
{
int unit = (int)(_unitValue * Time.deltaTime);
int diff = _curValue - _targetValue;
if (Mathf.Abs(diff) >= unit)
{
_curValue = (int)Mathf.Lerp(_curValue, _targetValue, 0.1f);
}
else
{
_curValue = _targetValue;
if (_curValue <= 0)
{
}
}
}
else if (!force)
{
return;
}
float cmv_f = _curValue / (float)_unitValue;
float sv = cmv_f - (int)cmv_f;
if (sv == 0)
sv = 1f;
_sliHp.value = sv;
int cmv_i = Mathf.CeilToInt(cmv_f);
if (cmv_f < 1f)
{
_tmpHpValue.text = "";
}
else
{
#if UNITY_EDITOR
_tmpHpValue.text = $"{_targetValue}/{_maxValue}(x{cmv_i})";
#else
_tmpHpValue.text = $"x{cmv_i}";
#endif
}
int col_i = Mathf.Max(0, _maxMulti - cmv_i);
_imgHpFill.color = _RomdomColor[col_i % _RomdomColor.Length];
_imgHpFill2.color = cmv_i <= 1 ? Color.clear : _RomdomColor[(col_i + 1) % _RomdomColor.Length];
}
public void SetMultProp(int curValue)
{
_tmpPropValue.gameObject.SetActive(true);
_tmpPropValue.text = $"+{curValue}";
}
Transform _target;
Vector3 _targetOffset;
public void SetTarget(Transform target, Vector3 offset = default)
{
_target = target;
_targetOffset = offset;
}
Vector3 _position;
public void SetFixedPosition(Vector3 position)
{
fixedPoint = true;
_position = position;
}
public void SetFixedPosition()
{
fixedPoint = true;
}
#endregion
public void SetHpValue(int curValue, int maxValue, Color color)
{
//_tmpHpValue.text = $"{curValue}/{maxValue}";
_sliHp.gameObject.SetActive(true);
_sliHp.value = curValue / (float)maxValue;
if (color != default)
_imgHpFill.color = color;
}
public void SetHpValue(int curValue, int maxValue, float height, int index)
{
//_tmpHpValue.text = $"{curValue}/{maxValue}";
_sliHp.gameObject.SetActive(true);
_sliHp.value = curValue / (float)maxValue;
_targetOffset = Vector3.up * height;
if (index == 0)
{
_imgHpFill.color = Color.red;
}
else if (index == 1)
{
_imgHpFill.color = Color.green;
}
else if (index == 2)
{
_imgHpFill.color = Color.grey;
}
}
public void Init()
{
this.gameObject.SetActive(true);
}
public void Release()
{
BoardManager.Instance.ReturnBoard(this);
}
public void ReleaseWaitFinish()
{
StartCoroutine(ReleaseWaitAnimCO());
}
private System.Collections.IEnumerator ReleaseWaitAnimCO()
{
_targetValue = 0;
yield return new WaitForSeconds(0.2f);
BoardManager.Instance.ReturnBoard(this);
}
}
}