103 lines
2.8 KiB
C#
103 lines
2.8 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Common;
|
||
using DG.Tweening;
|
||
using DragonLi.Core;
|
||
using Mirror;
|
||
using UnityEngine;
|
||
|
||
public class EnemyUI : NetworkBehaviour
|
||
{
|
||
public SpriteRenderer[] bg;
|
||
public SpriteRenderer[] blood1;
|
||
public SpriteRenderer[] blood2;
|
||
|
||
public Sprite[] bgs;
|
||
public Sprite[] blood1s;
|
||
|
||
public State UIState;
|
||
|
||
private Enemy _enemy;
|
||
private float _lastHp = 0.0f;
|
||
private Tweener _lastTween = null;
|
||
|
||
[Server]
|
||
public void Init(Enemy enemy)
|
||
{
|
||
_enemy = enemy;
|
||
HPstyleChange((int)enemy.type);
|
||
// 0.2秒后展示ui,防止在原点出现
|
||
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
|
||
{
|
||
if (_enemy.type == EnemyType.ZombieBoss)
|
||
{
|
||
UIStateChange(2);
|
||
}
|
||
else
|
||
{
|
||
UIStateChange(1);
|
||
}
|
||
}, 0.2f);
|
||
}
|
||
|
||
public void Update()
|
||
{
|
||
if (isClient)
|
||
{
|
||
Vector3 directionToCamera = GameLocal.Ins.MRCamera.transform.position - transform.position;
|
||
directionToCamera.y = 0;
|
||
// 计算新的旋转方向,固定Y轴方向
|
||
Quaternion targetRotation = Quaternion.LookRotation(directionToCamera, Vector3.up);
|
||
// 应用新的旋转方向
|
||
transform.rotation = targetRotation;
|
||
}
|
||
|
||
if (isServer && _enemy != null)
|
||
{
|
||
transform.position = new Vector3(_enemy.transform.position.x, 0, _enemy.transform.position.z);
|
||
if (UIState.nowState == 1 || UIState.nowState == 2)
|
||
{
|
||
if (_enemy.health != _lastHp)
|
||
{
|
||
HpChange(_enemy.type, _enemy.health, _enemy.originHealth);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
[ClientRpc]
|
||
public void HPstyleChange(int type)
|
||
{
|
||
bg[0].sprite = bgs[type];
|
||
blood1[0].sprite = blood1s[type];
|
||
blood2[0].sprite = blood1s[type];
|
||
UIState.StateChange(0);
|
||
}
|
||
|
||
[ClientRpc]
|
||
public void UIStateChange(int state)
|
||
{
|
||
UIState.StateChange(state);
|
||
}
|
||
|
||
[ClientRpc]
|
||
public void HpChange(EnemyType type, float currentBlood, float totalBlood)
|
||
{
|
||
float allLength = type == EnemyType.ZombieBoss ? 3f : 1.58f;
|
||
float lastValue = _lastHp / totalBlood;
|
||
float value = currentBlood / totalBlood;
|
||
blood2[0].size = new Vector2(value * allLength, 0.14f);
|
||
blood2[1].size = new Vector2(value * allLength, 0.14f);
|
||
_lastHp = currentBlood;
|
||
if (_lastTween != null)
|
||
{
|
||
_lastTween.Kill();
|
||
}
|
||
_lastTween = DOVirtual.Float(lastValue, value, 0.5f, res =>
|
||
{
|
||
blood1[0].size = new Vector2(res * allLength, 0.14f);
|
||
blood1[1].size = new Vector2(res * allLength, 0.14f);
|
||
});
|
||
}
|
||
}
|