142 lines
4.0 KiB
C#
142 lines
4.0 KiB
C#
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using DG.Tweening;
|
||
using DragonLi.Core;
|
||
using Common;
|
||
using BehaviorDesigner.Runtime;
|
||
using Mirror;
|
||
using Pico.Platform;
|
||
|
||
namespace Valheim
|
||
{
|
||
public class EnemyUI : NetworkBehaviour
|
||
{
|
||
public State uiState;
|
||
public GameObject found;
|
||
public TextMeshPro[] lvText;
|
||
public TextMeshPro lvName;
|
||
public SpriteRenderer[] lvIcon;
|
||
public SpriteRenderer bloodBg;
|
||
public SpriteRenderer blood2;
|
||
|
||
private Enemy _enemy;
|
||
private float _lastHp = 0.0f;
|
||
private Tweener _lastTween = null;
|
||
|
||
[ClientRpc]
|
||
public void Init(Enemy enemy)
|
||
{
|
||
_enemy = enemy;
|
||
enemy.ui = gameObject;
|
||
lvName.text = enemy.enemyName;
|
||
if (isServer)
|
||
{
|
||
if (enemy.type != EnemyType.FishGiant)
|
||
{
|
||
enemy.behaviorTree.RegisterEvent("FoundEnemy", FoundEnemyRpc);
|
||
enemy.behaviorTree.RegisterEvent("Back", BackRpc);
|
||
enemy.behaviorTree.RegisterEvent("Ghost", GhostRpc);
|
||
enemy.behaviorTree.RegisterEvent("TrueDie", DestroyUI);
|
||
}
|
||
//EnemyInfo enemyInfo = GameManager.Ins.EnemyInfos[enemy.type][enemy.lvl];
|
||
}
|
||
found.SetActive(false);
|
||
uiState.StateChange(1);
|
||
}
|
||
|
||
|
||
public void Update()
|
||
{
|
||
if (isClient && _enemy != null)
|
||
{
|
||
Vector3 directionToCamera = GameInit.Ins.MRCamera.transform.position - this.transform.position;
|
||
directionToCamera.y = 0;
|
||
// 计算新的旋转方向,固定Y轴方向
|
||
Quaternion targetRotation = Quaternion.LookRotation(directionToCamera, Vector3.up);
|
||
// 应用新的旋转方向
|
||
transform.rotation = targetRotation;
|
||
|
||
LvChange(_enemy.lvl);
|
||
if (_enemy.health != _lastHp)
|
||
{
|
||
HpChange(_enemy.health, _enemy.OriginHealth);
|
||
}
|
||
}
|
||
if (isServer && _enemy != null)
|
||
{
|
||
if (_enemy.type == EnemyType.FishGiant && _enemy.GetComponent<FishGiant>().bossState == BossState.WakeUp)
|
||
{
|
||
ShowBossUi();
|
||
}
|
||
|
||
transform.localPosition = Vector3.zero;
|
||
}
|
||
}
|
||
|
||
[ClientRpc]
|
||
public void ShowBossUi()
|
||
{
|
||
uiState.StateChange(1);
|
||
}
|
||
|
||
|
||
|
||
[ClientRpc]
|
||
public void FoundEnemyRpc()
|
||
{
|
||
// 惊叹号
|
||
//found.SetActive(true);
|
||
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
|
||
{
|
||
found.SetActive(false);
|
||
}, 0.5f);
|
||
uiState.StateChange(1);
|
||
}
|
||
|
||
[ClientRpc]
|
||
public void BackRpc()
|
||
{
|
||
// 亮出ui
|
||
uiState.StateChange(2);
|
||
}
|
||
|
||
[ClientRpc]
|
||
public void GhostRpc()
|
||
{
|
||
// 隐藏ui
|
||
uiState.StateChange(1);
|
||
}
|
||
|
||
public void LvChange(int lv)
|
||
{
|
||
for (int i = 0; i < lvIcon.Length; i++)
|
||
{
|
||
//lvIcon[i].sprite = Resources.Load<Sprite>($"BloodIcon/Enemy/lvl{lv}");
|
||
lvText[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);
|
||
});
|
||
}
|
||
|
||
public void DestroyUI()
|
||
{
|
||
NetworkServer.Destroy(gameObject);
|
||
}
|
||
}
|
||
}
|