Files
MRCS/Assets/_MrCs/Scripts/UI/HUDPanel.cs

303 lines
8.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using DarkTonic.MasterAudio;
using DragonLi.Core;
using LitJson;
using Mirror;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class HUDPanel : MonoBehaviour
{
/// <summary>
/// 倒计时文本
/// </summary>
public TextMeshProUGUI LessTimeText;
public Image hand;
public Image team;
public Sprite[] teamSprites;
public Sprite[] teamHands;
/// <summary>
/// 得分文本
/// </summary>
public TextMeshProUGUI ScoreText;
public GameObject GetScore;
public GameObject Round;
public Sprite[] RoundIcon;
public Image icon;
[SoundGroup]
public string killSound;
/// <summary>
/// 击杀UI
/// </summary>
public Slider slider;
public GameObject dieBlueBg;
public GameObject dieRedBg;
public Image sliderFill;
public TextMeshProUGUI sliderText;
public Sprite[] sliderIcon;
public GameObject roundUI;
public GameObject roundBlueWin;
public GameObject roundBlueLose;
public GameObject roundRedWin;
public GameObject roundRedLose;
public Image roundIndexUI;
public TextMeshProUGUI roundBlueScore;
public TextMeshProUGUI roundRedScore;
public Sprite[] roundIndexIcon;
public GameObject gameStart;
/// <summary>
/// 最终结算
/// </summary>
public GameObject gameEnd;
public TextMeshProUGUI blueEndScore;
public TextMeshProUGUI redEndScore;
public TextMeshProUGUI blueEndTxt;
public TextMeshProUGUI redEndTxt;
public GameObject blueEndWin;
public GameObject redEndWin;
public GameObject ping;
/// <summary>
/// 血条
/// </summary>
public Image blood2;
public GameObject hitEffect;
/// <summary>
/// 复活倒计时
/// </summary>
public GameObject dieUI;
public static void Show()
{
OverlayUIManager.Ins.Cover("UI/HUDPanel", false);
}
private void Awake()
{
GameLocal.Ins.HitUI = hitEffect;
GameLocal.Ins.DieUI = dieUI;
GameLocal.Ins.DiKillUIeUI = GetScore;
GameLocal.Ins.RoundUI = roundUI;
}
public void Start()
{
EventDispatcher.AddEventListener<int>("ScoreSound", UpdateScore);
EventDispatcher.AddEventListener<float, float>("HpChange", HpChange);
EventDispatcher.AddEventListener<int, float>("Revive", Revive);
EventDispatcher.AddEventListener("NewWaveStart", NewWaveStart);
EventDispatcher.AddEventListener<int, int,int>("NewRoundStart", NewRoundStart);
EventDispatcher.AddEventListener("GameStart", GameStart);
EventDispatcher.AddEventListener<int>("ChangeTeam", ChangeTeam);
LessTimeText.text = GameManager.Ins.GetLessTimeStr();
ScoreText.text = "0";
GetScore.SetActive(false);
gameStart.SetActive(false);
dieUI.SetActive(false);
roundUI.SetActive(false);
gameEnd.SetActive(false);
hand.gameObject.SetActive(false);
team.gameObject.SetActive(false);
NewWaveStart();
}
public void HpChange(float currentHp, float maxHp)
{
blood2.fillAmount = currentHp / maxHp;
//DOTween.To(() => blood1.fillAmount, x => blood1.fillAmount = x, currentHp / maxHp, 0.5f);//血量变化
}
public float scaleDuration = 0.3f; // 放大缩小动画时间
public float shakeAmount = 0.2f; // 放大最大比例
private float countDuration = 0.5f; // 分数累加动画时间
public void UpdateScore(int score)
{
GetScore.SetActive(true);
MasterAudio.PlaySound(killSound);
StopAllCoroutines();
StartCoroutine(ScoreEffectRoutine(score));
}
private IEnumerator ScoreEffectRoutine(int score)
{
// ===== 放大缩小震撼效果 =====
Vector3 originalScale = GetScore.transform.localScale;
float timer = 0f;
// 放大
while (timer < scaleDuration)
{
timer += Time.deltaTime;
float t = timer / scaleDuration;
float scale = Mathf.Lerp(1f, 1f + shakeAmount, Mathf.Sin(t * Mathf.PI * 0.5f)); // 缓入
GetScore.transform.localScale = originalScale * scale;
yield return null;
}
// 缩回
timer = 0f;
while (timer < scaleDuration)
{
timer += Time.deltaTime;
float t = timer / scaleDuration;
float scale = Mathf.Lerp(1f + shakeAmount, 1f, Mathf.Sin(t * Mathf.PI * 0.5f)); // 缓出
GetScore.transform.localScale = originalScale * scale;
yield return null;
}
GetScore.transform.localScale = originalScale;
// ===== 分数累加效果 =====
float current = 0;
timer = 0f;
while (timer < countDuration)
{
timer += Time.deltaTime;
float t = timer / countDuration;
current = Mathf.Lerp(0, score, t);
ScoreText.text = "得分:" + Mathf.RoundToInt(current).ToString();
yield return null;
}
ScoreText.text = "得分:" + score.ToString();
// 等待3秒后隐藏
yield return new WaitForSeconds(3f);
GetScore.SetActive(false);
}
public void NewWaveStart()
{
Round.SetActive(true);
StartCoroutine(RoundWaveTime());
}
public void NewRoundStart(int blueScore, int redScore, int roundIndex)
{
if (roundIndex == 3)
{
GameEnd(blueScore, redScore);
Debug.Log("游戏结束5秒后退出");
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
Debug.Log("正在退出游戏...");
Application.Quit();
}, 60f);
return;
}
MasterAudio.PlaySound("3.9");
roundUI.SetActive(true);
roundBlueWin.SetActive(blueScore>=redScore);
roundBlueLose.SetActive(blueScore < redScore);
roundRedWin.SetActive(redScore >= blueScore);
roundRedLose.SetActive(redScore < blueScore);
roundIndexUI.sprite = roundIndexIcon[roundIndex-1];
roundBlueScore.text = blueScore.ToString();
roundRedScore.text = redScore.ToString();
}
public void GameEnd(int blueScore, int redScore)
{
GameLocal.Ins.BGMState.StateChange(4);
gameEnd.SetActive(true);
blueEndScore.text = blueScore.ToString();
redEndScore.text = redScore.ToString();
blueEndWin.SetActive(blueScore>redScore);
redEndWin.SetActive(redScore > blueScore);
ping.SetActive(blueScore==redScore);
blueEndTxt.text = blueScore >= redScore ? "赢" : "输";
redEndTxt.text = redScore >= blueScore ? "赢" : "输";
}
public void GameStart()
{
gameStart.SetActive(true);
}
public void ChangeTeam(int teamId)
{
hand.gameObject.SetActive(true);
team.gameObject.SetActive(true);
hand.sprite= teamHands[teamId-1];
team.sprite = teamSprites[teamId-1];
}
IEnumerator RoundWaveTime()
{
//GameManager.Ins.PlaySound2DRPC("1.1");
int time = 3;
while (time>0)
{
icon.sprite = RoundIcon[time-1];
yield return new WaitForSeconds(1f);
time--;
}
Round.SetActive(false);
}
public void Revive(int team, float time)
{
slider.value = 0;
dieBlueBg.SetActive(team == 2);
dieRedBg.SetActive(team == 1);
sliderFill.sprite = sliderIcon[team - 1];
sliderText.text = "0%";
StopAllCoroutines();
StartCoroutine(ReviveProgress(time));
}
private IEnumerator ReviveProgress(float reviveTime)
{
float timer = reviveTime;
float total = reviveTime;
while (timer > 0)
{
timer -= Time.deltaTime;
// 进度 0~1
float progress = Mathf.Clamp01(1 - (timer / total));
slider.value = progress;
// 转百分比并更新
sliderText.text = Mathf.RoundToInt(progress * 100f) + "%";
yield return null;
}
// 完成时强制 100%
slider.value = 1f;
sliderText.text = "100%";
}
void Update()
{
if (GameManager.Ins.gameState== GameState.Playing)
{
if (GameManager.Ins.GetLessTimeSeconds() <= 0)
{
LessTimeText.text = "00:00";
}
else
{
LessTimeText.text = GameManager.Ins.GetLessTimeStr();
}
}
}
}