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; [NonSerialized] public float second=60*15f; public GameObject[] teachUIs; public Image dieImage; public Sprite[] dieTimeSprite; public GameObject dieUI; 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; } void Start() { EventDispatcher.AddEventListener("HpChange", HpChange); EventDispatcher.AddEventListener("PlayerDeath", PlayerDeath); EventDispatcher.AddEventListener("PlayerAlive", PlayerAlive); //EventDispatcher.AddEventListener("PromptMessage", ShowMessage); EventDispatcher.AddEventListener("ShowTeachUI", ShowTeachUI); EventDispatcher.AddEventListener("ShowBulletAmount", ShowBulletAmount); canvasGroup = GetComponent(); // 设置初始透明度为0 canvasGroup.alpha = 0f; // 使用DoTween实现透明度从0到1的渐显效果 canvasGroup.DOFade(1f, fadeInDuration); hpTxt.text = 100 + "%"; } public void ShowBulletAmount(int amount) { if (amount == 0) { bulletAmount.text = ""; } else { if (amount > 1000) { bulletAmount.text = "无限"; return; } bulletAmount.text = amount.ToString(); } } public void PlayerDeath(Transform transform) { IsAlive = false; StartCoroutine(StartCountdown()); } IEnumerator StartCountdown() { foreach (var item in dieTimeSprite) { dieImage.sprite = item; yield return new WaitForSeconds(1.0f); } } 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.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(false); timeTxt.text = "00:00"; } } }