Files
WhaleAdventure_VR/Assets/02_Scripts/Managers/change room manager/RandomSceneRouteManager.cs
dldydtn9755-crypto 8d11d54875
2026-06-25 18:37:25 +09:00

247 lines
6.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RandomSceneRouteManager : MonoBehaviour
{
public static RandomSceneRouteManager Instance;
[Header("랜덤으로 이동할 방 Scene 이름들")]
[SerializeField] private string[] roomSceneNames;
[Header("모든 방 방문 후 이동할 마지막 Scene 이름")]
[SerializeField] private string finalSceneName;
private readonly HashSet<string> visitedScenes = new HashSet<string>();
private string nextSceneCode1 = "";
private string nextSceneCode2 = "";
private string nextSceneName1 = "";
private string nextSceneName2 = "";
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
}
else
{
Destroy(gameObject);
}
}
private void OnDestroy()
{
if (Instance == this)
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
string loadedSceneName = scene.name.Trim();
if (IsRoomScene(loadedSceneName))
{
visitedScenes.Add(loadedSceneName);
Debug.Log("방문 처리된 씬: " + loadedSceneName);
}
}
public void PrepareNextSceneChoices()
{
string currentSceneName = SceneManager.GetActiveScene().name.Trim();
if (IsRoomScene(currentSceneName))
{
visitedScenes.Add(currentSceneName);
}
List<string> candidates = new List<string>();
foreach (string sceneName in roomSceneNames)
{
if (string.IsNullOrWhiteSpace(sceneName))
continue;
string cleanSceneName = sceneName.Trim();
// 현재 방은 선택지에서 제외
if (cleanSceneName == currentSceneName)
continue;
// 이미 방문한 방도 제외
if (visitedScenes.Contains(cleanSceneName))
continue;
candidates.Add(cleanSceneName);
}
Shuffle(candidates);
nextSceneCode1 = "";
nextSceneCode2 = "";
nextSceneName1 = "";
nextSceneName2 = "";
// 남은 방이 2개 이상이면 방 2개 선택지 표시
if (candidates.Count >= 2)
{
nextSceneCode1 = candidates[0];
nextSceneName1 = GetDisplayName(nextSceneCode1);
nextSceneCode2 = candidates[1];
nextSceneName2 = GetDisplayName(nextSceneCode2);
}
// 남은 방이 1개면 그 방만 표시
else if (candidates.Count == 1)
{
nextSceneCode1 = candidates[0];
nextSceneName1 = GetDisplayName(nextSceneCode1);
// 중요: 마지막 방이 남아있을 때는 제페토 방을 선택지 2로 넣지 않음
nextSceneCode2 = "";
nextSceneName2 = "";
}
// 남은 방이 0개면 그때만 제페토 방 표시
else
{
if (!string.IsNullOrWhiteSpace(finalSceneName))
{
nextSceneCode1 = finalSceneName.Trim();
nextSceneName1 = GetDisplayName(nextSceneCode1);
nextSceneCode2 = "";
nextSceneName2 = "";
}
else
{
Debug.LogWarning("이동 가능한 다음 씬이 없습니다.");
}
}
Debug.Log("현재 씬: " + currentSceneName);
Debug.Log("방문한 씬 수: " + visitedScenes.Count);
Debug.Log("선택지 1: " + nextSceneName1 + " / " + nextSceneCode1);
Debug.Log("선택지 2: " + nextSceneName2 + " / " + nextSceneCode2);
}
public string GetNextSceneName1()
{
return nextSceneName1;
}
public string GetNextSceneCode1()
{
return nextSceneCode1;
}
public string GetNextSceneName2()
{
return nextSceneName2;
}
public string GetNextSceneCode2()
{
return nextSceneCode2;
}
public string GetNextSceneName()
{
PrepareNextSceneChoices();
if (!string.IsNullOrEmpty(nextSceneCode1))
return nextSceneCode1;
return string.Empty;
}
public void RequestRandomSceneChange()
{
string nextSceneName = GetNextSceneName();
if (string.IsNullOrEmpty(nextSceneName))
{
Debug.LogWarning("이동할 다음 씬 이름이 비어있습니다.");
return;
}
SceneLoadManager loadManager = FindFirstObjectByType<SceneLoadManager>();
if (loadManager != null)
{
Debug.Log("랜덤 씬 이동 요청: " + nextSceneName);
loadManager.RequestSceneChange(nextSceneName);
}
else
{
Debug.LogWarning("SceneLoadManager가 없어서 바로 씬 이동합니다: " + nextSceneName);
SceneManager.LoadScene(nextSceneName);
}
}
public void ResetRoute()
{
visitedScenes.Clear();
nextSceneCode1 = "";
nextSceneCode2 = "";
nextSceneName1 = "";
nextSceneName2 = "";
Debug.Log("랜덤 방 방문 기록 초기화");
}
private void Shuffle(List<string> list)
{
for (int i = 0; i < list.Count; i++)
{
int randomIndex = Random.Range(i, list.Count);
string temp = list[i];
list[i] = list[randomIndex];
list[randomIndex] = temp;
}
}
private string GetDisplayName(string sceneName)
{
switch (sceneName)
{
case "blackjack":
return "블랙잭";
case "CatsRoom":
return "캣룸";
case "MazeRoom":
return "미로방";
case "Cave_Test_2":
return "동굴방";
case "Gepto first":
return "제페토";
default:
return sceneName;
}
}
private bool IsRoomScene(string sceneName)
{
foreach (string roomSceneName in roomSceneNames)
{
if (string.IsNullOrWhiteSpace(roomSceneName))
continue;
if (roomSceneName.Trim() == sceneName.Trim())
return true;
}
return false;
}
}