Petition-to-the-Gods-V3/Assets/Scripts/Client/ClientGameBackToRealSceneController.cs
2025-11-19 19:30:13 +08:00

98 lines
2.9 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 UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.Video;
public class ClientGameBackToRealSceneController : MonoBehaviour
{
public static ClientGameBackToRealSceneController Instance;
public VideoPlayer videoPlayer;
public RawImage videoDisplay;
// --- 【新增變數】用於追蹤兩個條件是否滿足 ---
private bool isVideoFinished = false;
private bool isSignalReceived = false;
private void Awake()
{
Instance = this;
}
void Start()
{
SetupVideoPlayer();
// 開始播放影片
if (videoPlayer != null)
{
videoPlayer.Play();
Debug.Log("開始播放影片");
}
}
void SetupVideoPlayer()
{
if (videoPlayer != null)
{
// 設置影片結束事件
videoPlayer.loopPointReached += OnVideoFinished;
// 設置影片顯示 (略)
if (videoDisplay != null)
{
videoPlayer.targetTexture = null;
videoPlayer.renderMode = VideoRenderMode.RenderTexture;
RenderTexture rt = new RenderTexture(1080, 1920, 24);
videoPlayer.targetTexture = rt;
videoDisplay.texture = rt;
}
}
}
// 影片播放結束時呼叫
void OnVideoFinished(VideoPlayer vp)
{
Debug.Log("影片播放完成。");
isVideoFinished = true; // 影片結束標記設為 true
CheckAndLoadScene(); // 檢查是否可以跳轉
}
// --- 【新增方法】供 ClientMessageHandler 呼叫,標記訊號已收到 ---
public void ReceiveSignalFromPlayerB()
{
Debug.Log("收到玩家B的訊號 (gameFinalWords)。");
isSignalReceived = true; // 訊號接收標記設為 true
CheckAndLoadScene(); // 檢查是否可以跳轉
}
// --- 【新增方法】核心檢查跳轉邏輯 ---
private void CheckAndLoadScene()
{
// 確保 ClientLastWordsSceneController.words 已經有值
bool hasWords = !string.IsNullOrEmpty(ClientLastWordsSceneController.words);
if (isVideoFinished && isSignalReceived && hasWords)
{
Debug.Log("🎉 影片和訊號條件滿足,準備跳轉到下一場景...");
StartCoroutine(LoadNextScene());
}
else if (isVideoFinished && !isSignalReceived)
{
// 影片已結束,等待訊號
Debug.Log("影片已結束正在等待玩家B訊號...");
}
else if (!isVideoFinished && isSignalReceived)
{
// 訊號已收到,等待影片結束
Debug.Log("已收到玩家B訊號正在等待影片播放完成...");
}
}
public IEnumerator LoadNextScene()
{
yield return new WaitForSeconds(0f);
SceneManager.LoadScene("ClientLastWordsScene");
}
}