Files
FutureMen2/Assets/_FutureMen2/Scripts/UI/PlayerUI.cs
2026-01-05 11:00:50 +08:00

254 lines
6.0 KiB
C#

using DG.Tweening;
using DragonLi.Core;
using DragonLi.Frame;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class PlayerUI : MonoBehaviour
{
public GameObject hitEffect;
public TMP_Text timeTxt;
public float second;
public GameObject[] teachUIs;
public TMP_Text dieTxt;
public TMP_Text info;
public GameObject dieUI;
public GameObject WeaponIcons;
public TextMeshProUGUI bulletAmount;
private bool IsAlive = true;
public float fadeInDuration = 1.0f;
private CanvasGroup canvasGroup;
public Image blood1;
public Image blood2;
public TMP_Text hpTxt;
private int currentIndex = 0; // 当前对话索引
private string[] dialogueTexts; // 存储对话文本的数组
public void Awake()
{
GameInit.Ins.HitUI = hitEffect;
GameInit.Ins.DieUI = dieUI;
GameInit.Ins.dieTxt = dieTxt;
}
void Start()
{
EventDispatcher.AddEventListener<float, float>("HpChange", HpChange);
EventDispatcher.AddEventListener<Transform>("PlayerDeath", PlayerDeath);
EventDispatcher.AddEventListener<Transform>("PlayerAlive", PlayerAlive);
EventDispatcher.AddEventListener<int>("PromptMessage", ShowMessage);
EventDispatcher.AddEventListener<int, float>("ShowTeachUI", ShowTeachUI);
EventDispatcher.AddEventListener<PlayerWeaponType, int>("ChangeWeaponIcon", ChangeWeaponIcon);
EventDispatcher.AddEventListener<int>("ShowBulletAmount", ShowBulletAmount);
canvasGroup = GetComponent<CanvasGroup>();
// 设置初始透明度为0
canvasGroup.alpha = 0f;
// 使用DoTween实现透明度从0到1的渐显效果
canvasGroup.DOFade(1f, fadeInDuration);
hpTxt.text = 100 + "%";
#if PICO_
#endif
}
public void ShowBulletAmount(int amount)
{
if (amount == 0)
{
bulletAmount.text = "";
}
else
{
if (amount > 1000)
{
bulletAmount.text = "无限";
return;
}
bulletAmount.text = amount.ToString();
}
}
public void ChangeWeaponIcon(PlayerWeaponType weaponType, int amount)
{
ShowWeaponIcon((int)weaponType-1, amount);
}
public void ShowWeaponIcon(int index, int amount)
{
for (int i = 0; i < WeaponIcons.transform.childCount; i++)
{
GameObject weaponIcon = WeaponIcons.transform.GetChild(i).gameObject;
if (i == index)
{
weaponIcon.SetActive(true);
if (bulletAmount.text == "无限")
{
bulletAmount.text = "0";
}
bulletAmount.text =amount.ToString();
}
else
{
weaponIcon.SetActive(false);
}
}
if (index == 0)
{
bulletAmount.text = "无限";
}
}
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+"%";
}
public void ShowMessage(int index)
{
switch (index)
{
case 0:
Step("扣动扳机键开始射击!", 0, 2.0f, "1.3");
ShowTeachUI(0, 0);
ShowTeachUI(1, 2.0f);
ShowTeachUI(2, 4.0f);
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
{
Step("勇士,快消灭传送门!", 0, 2.0f, "1.1");
},3f);
break;
case 1:
Step("太好了,你们收集到了能源魔方!", 0, 2.0f, "1.9");
break;
}
}
//显示消息,按间隔消失
private void Step(string text, float wait, float time, string clipName,Transform tran=null)
{
if(tran == null)
tran=GameInit.Ins.self.transform;
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
info.gameObject.SetActive(true);
info.text = text;
GameInit.Ins.PlayAudio(clipName,tran,true);
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
info.gameObject.SetActive(false);
}, time);
}, wait);
}
//显示教学UI
public void ShowTeachUI(int index, float wait)
{
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
teachUIs[index].SetActive(true);
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
teachUIs[index].SetActive(false);
}, 2.0f);
}, wait);
}
private void Update()
{
if (!GameManager.Ins.GameStart|| 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.LoseEndGame();
timeTxt.text = "00:00";
}
}
}