107 lines
2.8 KiB
C#
107 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using DG.Tweening;
|
|
using DragonLi.Core;
|
|
using LitJson;
|
|
using Mirror;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class HUDPanel : MonoBehaviour
|
|
{
|
|
public TMP_Text timeTxt;
|
|
|
|
public float second;
|
|
|
|
public GameObject hitEffect;
|
|
|
|
public TMP_Text dieTxt;
|
|
public GameObject dieUI;
|
|
|
|
private bool IsAlive = true;
|
|
|
|
public float fadeInDuration = 1.0f;
|
|
|
|
private CanvasGroup canvasGroup;
|
|
|
|
public Image blood1;
|
|
public Image blood2;
|
|
|
|
public TMP_Text hpTxt;
|
|
|
|
public void Awake()
|
|
{
|
|
GameLocal.Ins.HitUI = hitEffect;
|
|
GameLocal.Ins.DieUI = dieUI;
|
|
GameLocal.Ins.dieTxt = dieTxt;
|
|
}
|
|
public static void Show()
|
|
{
|
|
OverlayUIManager.Ins.Cover("UI/HUDPanel", false);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
EventDispatcher.AddEventListener<float, float>("HpChange", HpChange);
|
|
EventDispatcher.AddEventListener<Transform>("PlayerDeath", PlayerDeath);
|
|
EventDispatcher.AddEventListener<Transform>("PlayerAlive", PlayerAlive);
|
|
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|
// 设置初始透明度为0
|
|
canvasGroup.alpha = 0f;
|
|
// 使用DoTween实现透明度从0到1的渐显效果
|
|
canvasGroup.DOFade(1f, fadeInDuration);
|
|
hpTxt.text = 100 + "%";
|
|
second = GameManager.Ins.vistAllTime;
|
|
timeTxt.text = string.Format("{0:d2}:{1:d2}", (int)second / 60, (int)second % 60);
|
|
hitEffect.SetActive(false);
|
|
dieUI.SetActive(false);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!GameManager.Ins.isGameStart|| GameManager.Ins.isGameEnd) return;
|
|
if (second > 0)
|
|
{
|
|
second = second - Time.deltaTime;
|
|
if (second / 60 < 1)
|
|
{
|
|
if (second < 4)
|
|
{
|
|
}
|
|
timeTxt.text = string.Format("00:{0:d2}", (int)second % 60);
|
|
}
|
|
else
|
|
{
|
|
timeTxt.text = string.Format("{0:d2}:{1:d2}", (int)second / 60, (int)second % 60);
|
|
}
|
|
|
|
GameManager.Ins.curTime = second;
|
|
}
|
|
else
|
|
{
|
|
GameManager.Ins.curTime = second;
|
|
//GameManager.Ins.GameOver(GameState.Failur);
|
|
timeTxt.text = "00:00";
|
|
}
|
|
}
|
|
|
|
public void PlayerDeath(Transform transform)
|
|
{
|
|
IsAlive = false;
|
|
}
|
|
|
|
public void PlayerAlive(Transform transform)
|
|
{
|
|
IsAlive = true;
|
|
}
|
|
public void HpChange(float currentHp, float maxHp)
|
|
{
|
|
blood2.fillAmount = currentHp / maxHp;
|
|
DOTween.To(() => blood1.fillAmount, x => blood1.fillAmount = x, currentHp / maxHp, 0.5f);//血量变化
|
|
hpTxt.text = Mathf.FloorToInt((currentHp/maxHp)*100) + "%";
|
|
if (Mathf.FloorToInt((currentHp / maxHp) * 100) < 0)
|
|
hpTxt.text = 0+"%";
|
|
}
|
|
}
|