_xiaofang/xiaofang/Assets/Res/gsj/scripts/Starthost.cs

279 lines
9.1 KiB
C#
Raw Normal View History

2024-12-20 10:10:19 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading.Tasks;
using UnityEngine.SceneManagement;
2024-12-23 14:46:56 +08:00
using Unity.VisualScripting;
2024-12-20 10:10:19 +08:00
public class Starthost : MonoBehaviour
{
2024-12-24 20:21:15 +08:00
// <20><><EFBFBD><EFBFBD>ί<EFBFBD><CEAF><EFBFBD>¼<EFBFBD>
public delegate void BoolValueChanged(bool newValue);
public static event BoolValueChanged OnBoolValueChanged;
private bool isRotating = false; // Ĭ<><C4AC><EFBFBD><EFBFBD>ת״̬
2024-12-20 10:10:19 +08:00
private GameObject panel;
private Button posiBtn;//<2F><>Ա<EFBFBD><D4B1>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
private Button start;//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
public bool isReady;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>׼<EFBFBD><D7BC>
public bool isSatisfy;//<2F><>Ա<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
public bool isStart;//<2F><>ϰ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ʼ
private GameObject time;//<2F><>ʱ
private Text timeText;//<2F><>ʱʱ<CAB1><CAB1>
private bool isTimerRunning=false;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ʼ<EFBFBD><CABC>ʱ
private float timer = 0f;
public float moveDuration = 2f; // <20>ƶ<EFBFBD>ʱ<EFBFBD><CAB1>
public float scaleDuration = 2f; // <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
private RectTransform rectTransform;
private Canvas canvas;
private Panel2 GetPanel2;
public Alertwindow alertwindow;
private bool isButtonClicked = false; // <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
2024-12-23 14:46:56 +08:00
private GameObject peopleposition;//<2F><>Ա<EFBFBD><D4B1>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>Ϣ
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵Ļ<CBB5><C4BB><EFBFBD>
public bool ishost=false;
private Button ready;
private Image image;
2024-12-24 20:21:15 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public GameObject walkieTalkieIcon; // <20>Խ<EFBFBD><D4BD><EFBFBD>ͼ<EFBFBD><EFBFBD><EAA3A8>̬Ч<CCAC><D0A7><EFBFBD><EFBFBD>
public Text dialogueText; // <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5>ı<EFBFBD><C4B1><EFBFBD>
private AudioSource audioSource; // <20><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> AudioSource
public AudioClip staticNoiseClip; // <20>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
public AudioClip[] voiceClips; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>
public string[] dialogueLines; // <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public float textSpeed = 0.1f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ٶȣ<D9B6>ÿ<EFBFBD><C3BF><EFBFBD>ֵļ<D6B5><C4BC><EFBFBD>ʱ<EFBFBD>
private int currentLineIndex = 0; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
private GameObject right;//<2F>ұ<EFBFBD>ͼ<EFBFBD><CDBC>
2024-12-20 10:10:19 +08:00
void Start()
{
2024-12-24 20:21:15 +08:00
audioSource= GetComponent<AudioSource>();
// ȷ<><C8B7><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD>ͼ<EFBFBD><CDBC>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
walkieTalkieIcon.SetActive(false);
right=transform.Find("Panel1/right").gameObject;
2024-12-20 10:10:19 +08:00
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);
2024-12-23 14:46:56 +08:00
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);
}
2024-12-20 10:10:19 +08:00
}
void OnEnable()
{
Alertwindow.BoolValueChanged += OnBoolChanged;
}
void OnDisable()
{
Alertwindow.BoolValueChanged -= OnBoolChanged;
}
void OnBoolChanged(bool newBoolValue)
{
isButtonClicked=newBoolValue;
if (isButtonClicked)
{
Debug.Log("<22>ص<EFBFBD><D8B5><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>");
}
}
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);
}
2024-12-23 14:46:56 +08:00
}
void OnReadyBtn()
{
image.gameObject.SetActive(true);
ready.gameObject.SetActive(false);
2024-12-20 10:10:19 +08:00
}
// ֹͣ<CDA3><D6B9>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʾ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
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 );
//// <20><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>ť<EFBFBD><C5A5><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
//foreach (Button button in buttons)
//{
// button.onClick.AddListener(ShowPopupPanel);
//}
Game.uiManager.ShowUI<Image>("Panel");
}
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
void ShowPopupPanel()
{
Game.uiManager.ShowUI<Image>("Panel");
}
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϰ
void OnClickStartBtn()
{
isStart = true;
if (isSatisfy)
{
2024-12-24 20:21:15 +08:00
peopleposition.SetActive(false);
right.SetActive(false);
2024-12-20 10:10:19 +08:00
AnimateUIElementAsync();
start.interactable = false; // <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
//<2F><>ָ<EFBFBD>ӽӾ<D3BD><D3BE><EFBFBD><EFBFBD><EFBFBD>
2024-12-24 20:21:15 +08:00
// <20><>ʼ<EFBFBD>Խ<EFBFBD><D4BD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
PlayDialogueSequenceAsync();
//<2F><>Ұ<EFBFBD><D2B0>ʼ<EFBFBD><CABC>ת
isRotating = !isRotating; // <20>л<EFBFBD> bool ״̬
Debug.Log($"<22><>ת״̬<D7B4>ı<EFBFBD>: {isRotating}");
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><EFBFBD><E3B2A5>ȥ
OnBoolValueChanged?.Invoke(isRotating);
//SceneManager.LoadScene(4);
2024-12-20 10:10:19 +08:00
//<2F><>ʼ<EFBFBD><CABC>ʱ
isTimerRunning = true;
2024-12-24 20:21:15 +08:00
time.SetActive(true);
2024-12-20 10:10:19 +08:00
}
else
{
Game.uiManager.ShowUI<Image>("Schedule_host03");
}
}
// <20><EFBFBD><ECB2BD><EFBFBD><EFBFBD>
async void AnimateUIElementAsync()
{
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
await Task.Delay(2000);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ijߴ<C4B3>
Canvas canvas = GetComponentInParent<Canvas>();
float canvasHeight = canvas.GetComponent<RectTransform>().rect.height;
// <20><>ʼλ<CABC><CEBB><EFBFBD>ǻ<EFBFBD><C7BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Vector2 startPosition = rectTransform.anchoredPosition;
Vector2 targetPosition = new Vector2(startPosition.x, canvasHeight / 2-50); // <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
// <20><><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD>
Vector3 startScale = rectTransform.localScale;
Vector3 targetScale = startScale * 0.5f; // <20><>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
// ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><C5BA>ƶ<EFBFBD>
float elapsedTime = 0f;
while (elapsedTime < Mathf.Max(moveDuration, scaleDuration)) // ȷ<><C8B7>ͬʱ<CDAC><CAB1><EFBFBD>У<EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>ϳ<EFBFBD><CFB3><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ϊѭ<CEAA><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
float tScale = Mathf.Clamp01(elapsedTime / scaleDuration); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD><C5BD><EFBFBD>
float tMove = Mathf.Clamp01(elapsedTime / moveDuration); // <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD>
rectTransform.localScale = Vector3.Lerp(startScale, targetScale, tScale);
// <20>ƶ<EFBFBD>
rectTransform.anchoredPosition = Vector2.Lerp(startPosition, targetPosition, tMove);
elapsedTime += Time.deltaTime;
await Task.Yield(); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD>yield return null
}
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD>մﵽĿ<EFB5BD><C4BF>ֵ
rectTransform.localScale = targetScale;
rectTransform.anchoredPosition = targetPosition;
2024-12-24 20:21:15 +08:00
}
async void PlayDialogueSequenceAsync()
{
// 1. <20><><EFBFBD>ŶԽ<C5B6><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
walkieTalkieIcon.SetActive(true); // <20><>ʾ<EFBFBD>Խ<EFBFBD><D4BD><EFBFBD>ͼ<EFBFBD><CDBC>
audioSource.clip = staticNoiseClip;
audioSource.Play();
await Task.Delay(1500); // <20>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>䣨1.5<EFBFBD>
// 2. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
audioSource.clip = voiceClips[0];
audioSource.Play();
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
float voiceDuration = audioSource.clip.length * 1000; // ת<><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
Task voicePlayTask = Task.Delay((int)voiceDuration); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>Ի<EFBFBD><D4BB>ı<EFBFBD>
foreach (string line in dialogueLines)
{
await DisplayTextAsync(line);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ񲥷<C7B7><F1B2A5B7><EFBFBD>
if (!audioSource.isPlaying)
{
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѽ<EFBFBD><D1BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4>ʾ<EFBFBD><CABE><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>");
}
}
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
await voicePlayTask;
// <20>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
walkieTalkieIcon.SetActive(false);
dialogueText.text = ""; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
peopleposition.SetActive(true);
2024-12-20 10:10:19 +08:00
panel.SetActive(false);
2024-12-24 20:21:15 +08:00
right.SetActive(true);
2024-12-20 10:10:19 +08:00
Game.uiManager.ShowUI<Image>("Panel1_2");
2024-12-24 20:21:15 +08:00
}
async Task DisplayTextAsync(string line)
{
dialogueText.text = ""; // <20><><EFBFBD>յ<EFBFBD>ǰ<EFBFBD>ı<EFBFBD>
foreach (char c in line)
{
dialogueText.text += c;
await Task.Delay((int)(textSpeed * 2000)); // <20>ȴ<EFBFBD> `textSpeed` <20><><EFBFBD><EFBFBD>
}
}
2024-12-20 10:10:19 +08:00
}