_xiaofang/xiaofang/Assets/Res/gsj/scripts/Starthost.cs
2024-12-24 20:21:15 +08:00

279 lines
9.1 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 UnityEngine;
using UnityEngine.UI;
using System.Threading.Tasks;
using UnityEngine.SceneManagement;
using Unity.VisualScripting;
public class Starthost : MonoBehaviour
{
// 定义委托事件
public delegate void BoolValueChanged(bool newValue);
public static event BoolValueChanged OnBoolValueChanged;
private bool isRotating = false; // 默认旋转状态
private GameObject panel;
private Button posiBtn;//人员到位情况按钮
private Button start;//开始演练按钮
public bool isReady;//在线玩家是否准备
public bool isSatisfy;//人员是否满足
public bool isStart;//演习是否开始
private GameObject time;//计时
private Text timeText;//计时时间
private bool isTimerRunning=false;//是否开始计时
private float timer = 0f;
public float moveDuration = 2f; // 移动时间
public float scaleDuration = 2f; // 缩放时间
private RectTransform rectTransform;
private Canvas canvas;
private Panel2 GetPanel2;
public Alertwindow alertwindow;
private bool isButtonClicked = false; // 监听的变量
private GameObject peopleposition;//人员的位置信息
//非主持人的画面
public bool ishost=false;
private Button ready;
private Image image;
//语音内容
public GameObject walkieTalkieIcon; // 对讲机图标(动态效果)
public Text dialogueText; // 显示语音内容的文本框
private AudioSource audioSource; // 用于播放语音的 AudioSource
public AudioClip staticNoiseClip; // 对讲机噪音音效
public AudioClip[] voiceClips; // 语音音频数组
public string[] dialogueLines; // 对应的语音内容
public float textSpeed = 0.1f; // 逐字显示的速度(每个字的间隔时间)
private int currentLineIndex = 0; // 当前语音和文本索引
private GameObject right;//右边图标
void Start()
{
audioSource= GetComponent<AudioSource>();
// 确保对讲机图标默认隐藏
walkieTalkieIcon.SetActive(false);
right=transform.Find("Panel1/right").gameObject;
panel = transform.Find("Panel1").gameObject;
posiBtn = transform.Find("Panel1/right/under/btnRenYuanDaoWei").GetComponent<Button>();
start = transform.Find("Panel1/right/under/btnRenYanLianStart").GetComponent<Button>();
posiBtn.onClick.AddListener(OnClickPosiBtn);
start.onClick.AddListener(OnClickStartBtn);
time = transform.Find("time").gameObject;
timeText = transform.Find("time/txtCountdown").GetComponent<Text>();
timeText.text = "00:00";
rectTransform = time.GetComponent<RectTransform>();
canvas = time.GetComponentInParent<Canvas>();
time.gameObject.SetActive(false);
peopleposition = GameObject.Find("peopleposition").gameObject;
ready = transform.Find("Panel1/right/under/Ready").GetComponent<Button>();
image= transform.Find("Panel1/right/under/Image").GetComponent<Image>();
ready.gameObject.SetActive(false);
image.gameObject.SetActive(false);
if (!ishost)
{
ready.gameObject.SetActive(true);
posiBtn.gameObject.SetActive(false);
start.gameObject.SetActive(false);
ready.onClick.AddListener(OnReadyBtn);
}
else
{
ready.gameObject.SetActive(false);
posiBtn.gameObject.SetActive(true);
start.gameObject.SetActive(true);
}
}
void OnEnable()
{
Alertwindow.BoolValueChanged += OnBoolChanged;
}
void OnDisable()
{
Alertwindow.BoolValueChanged -= OnBoolChanged;
}
void OnBoolChanged(bool newBoolValue)
{
isButtonClicked=newBoolValue;
if (isButtonClicked)
{
Debug.Log("回到开始界面");
}
}
void Update()
{
if(!isReady)
{
start.interactable = false;
}
else
{
start.interactable = true;
}
if (isTimerRunning)
{
timer += Time.deltaTime;
int minutes = Mathf.FloorToInt(timer / 60f);
int seconds = Mathf.FloorToInt(timer % 60f);
string formattedTime = string.Format("{0:00}:{1:00}", minutes, seconds);
timeText.text = formattedTime;
}
if (isButtonClicked)
{
panel.SetActive(true);
start.interactable = true;
rectTransform.position = new Vector2(960, 540);
StopTimer();
rectTransform.localScale = new Vector3(1,1,1);
}
}
void OnReadyBtn()
{
image.gameObject.SetActive(true);
ready.gameObject.SetActive(false);
}
// 停止计时并重置时间显示的方法
public void StopTimer()
{
isTimerRunning = false;
timer = 0f;
if (timeText != null)
{
timeText.text = "00:00";
}
isButtonClicked = false;
time.SetActive(false);
//peopleposition.SetActive(false);
}
void OnClickPosiBtn()
{
//peopleposition.SetActive(true );
//// 给每个按钮添加点击监听事件
//foreach (Button button in buttons)
//{
// button.onClick.AddListener(ShowPopupPanel);
//}
Game.uiManager.ShowUI<Image>("Panel");
}
// 显示弹出界面的方法
void ShowPopupPanel()
{
Game.uiManager.ShowUI<Image>("Panel");
}
//开始进行演习
void OnClickStartBtn()
{
isStart = true;
if (isSatisfy)
{
peopleposition.SetActive(false);
right.SetActive(false);
AnimateUIElementAsync();
start.interactable = false; // 防止点击事件
//总指挥接警动画
// 开始对讲机效果流程
PlayDialogueSequenceAsync();
//视野开始旋转
isRotating = !isRotating; // 切换 bool 状态
Debug.Log($"旋转状态改变: {isRotating}");
// 触发事件,将新状态广播出去
OnBoolValueChanged?.Invoke(isRotating);
//SceneManager.LoadScene(4);
//开始计时
isTimerRunning = true;
time.SetActive(true);
}
else
{
Game.uiManager.ShowUI<Image>("Schedule_host03");
}
}
// 异步动画
async void AnimateUIElementAsync()
{
// 等待两秒
await Task.Delay(2000);
// 获取画布的尺寸
Canvas canvas = GetComponentInParent<Canvas>();
float canvasHeight = canvas.GetComponent<RectTransform>().rect.height;
// 初始位置是画布中心
Vector2 startPosition = rectTransform.anchoredPosition;
Vector2 targetPosition = new Vector2(startPosition.x, canvasHeight / 2-50); // 顶部位置
// 缩放动画
Vector3 startScale = rectTransform.localScale;
Vector3 targetScale = startScale * 0.5f; // 缩小两倍
// 同时进行缩放和移动
float elapsedTime = 0f;
while (elapsedTime < Mathf.Max(moveDuration, scaleDuration)) // 确保同时进行,选择较长的时间作为循环的上限
{
float tScale = Mathf.Clamp01(elapsedTime / scaleDuration); // 计算缩放进度
float tMove = Mathf.Clamp01(elapsedTime / moveDuration); // 计算移动进度
// 缩放
rectTransform.localScale = Vector3.Lerp(startScale, targetScale, tScale);
// 移动
rectTransform.anchoredPosition = Vector2.Lerp(startPosition, targetPosition, tMove);
elapsedTime += Time.deltaTime;
await Task.Yield(); // 等待到下一帧类似于协程的yield return null
}
// 确保最终达到目标值
rectTransform.localScale = targetScale;
rectTransform.anchoredPosition = targetPosition;
}
async void PlayDialogueSequenceAsync()
{
// 1. 播放对讲机噪音
walkieTalkieIcon.SetActive(true); // 显示对讲机图标
audioSource.clip = staticNoiseClip;
audioSource.Play();
await Task.Delay(1500); // 对讲机噪音持续时间1.5秒)
// 2. 播放语音并逐字显示文字
audioSource.clip = voiceClips[0];
audioSource.Play();
// 获取语音持续时间
float voiceDuration = audioSource.clip.length * 1000; // 转换为毫秒
Task voicePlayTask = Task.Delay((int)voiceDuration); // 等待语音播放完成的任务
// 逐条显示对话文本
foreach (string line in dialogueLines)
{
await DisplayTextAsync(line);
// 检查语音是否播放完
if (!audioSource.isPlaying)
{
Debug.Log("语音播放已结束,但仍有未显示的文本。");
}
}
// 等待语音播放完成
await voicePlayTask;
// 对话完成,清理界面
walkieTalkieIcon.SetActive(false);
dialogueText.text = ""; // 清空文字
peopleposition.SetActive(true);
panel.SetActive(false);
right.SetActive(true);
Game.uiManager.ShowUI<Image>("Panel1_2");
}
async Task DisplayTextAsync(string line)
{
dialogueText.text = ""; // 清空当前文本
foreach (char c in line)
{
dialogueText.text += c;
await Task.Delay((int)(textSpeed * 2000)); // 等待 `textSpeed` 毫秒
}
}
}