Files
valheim/Assets/_Valheim/Scripts/UI/PetUI.cs
2025-07-04 14:16:14 +08:00

118 lines
3.4 KiB
C#
Raw 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.
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using BehaviorDesigner.Runtime;
using Mirror;
namespace Valheim
{
public class PetUI : NetworkBehaviour
{
public TextMeshPro[] lvIcon;
public TextMeshPro lvName;
public SpriteRenderer[] lvIconBg;
public SpriteRenderer bloodBG;
public SpriteRenderer blood2;
public Common.State uiState;
public Pet _pet;
private float _lastHp = 0.0f;
private Tweener _lastTween = null;
[ClientRpc]
public void Init(Pet pet)
{
_pet = pet;
lvName.text = _pet.petName;
uiState.StateChange(1);
}
public void Update()
{
if (isClient && _pet != null)
{
// Debug.Log("客户端执行");
Vector3 directionToCamera = GameInit.Ins.MRCamera.transform.position - this.transform.position;
directionToCamera.y = 0;
// 计算新的旋转方向固定Y轴方向
Quaternion targetRotation = Quaternion.LookRotation(directionToCamera, Vector3.up);
// 应用新的旋转方向
transform.rotation = targetRotation;
// // 亮出完整ui时更新等级和血量
// if (uiState.nowState == 1)
// {
//
// } // 不完整ui更新等级
// else if (uiState.nowState == 2)
// {
// LvChange(_pet.lvl);
// }
if (_pet.health != _lastHp)
{
LvChange(_pet.lvl);
HpChange(_pet.health, _pet.OriginHealth);
}
if (_pet.state == PetState.NotRescue)
{
uiState.StateChange(0);
}
else
{
if (_pet.order == OrderType.Attack)
{
uiState.StateChange(1);
}
else if (_pet.order == OrderType.Retreat)
{
uiState.StateChange(2);
}
else if (_pet.order == OrderType.Null)
{
uiState.StateChange(1);
}
}
}
if (isServer && _pet != null)
{
transform.position = new Vector3(_pet.transform.position.x, _pet.uiPos.position.y, _pet.transform.position.z);
}
}
public void LvChange(int lv)
{
for (int i = 0; i < lvIconBg.Length; i++)
{
//lvIconBg[i].sprite = Resources.Load<Sprite>($"BloodIcon/Pet/lvl{lv}");
lvIcon[i].text = lv.ToString();
}
}
public void HpChange(float currentBlood, float totalBlood)
{
float lastValue = _lastHp / totalBlood;
float value = currentBlood / totalBlood;
blood2.size = new Vector2(value * 8.06f, 0.8f);
_lastHp = currentBlood;
if (_lastTween != null)
{
_lastTween.Kill();
}
_lastTween = DOVirtual.Float(lastValue, value, 0.5f, res =>
{
bloodBG.size = new Vector2(res * 8.06f, 0.8f);
});
}
}
}