226 lines
5.6 KiB
C#
226 lines
5.6 KiB
C#
using UnityEngine;
|
|
|
|
public class RoomSelectManager : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private RoomButtonUI[] roomButtons;
|
|
[SerializeField] private RoomDetailUI detailUI;
|
|
[SerializeField] private RoomEnterHandler roomEnterHandler;
|
|
[SerializeField] private RoomProgressManager roomProgressManager;
|
|
|
|
[Header("Options")]
|
|
[SerializeField] private bool autoFindButtonsInChildren = true;
|
|
[SerializeField] private bool selectFirstUnlockedOnEnable = true;
|
|
[Min(0)]
|
|
[SerializeField] private int fallbackCurrentMemoryPieces = 0;
|
|
|
|
private RoomButtonUI selectedButton;
|
|
private RoomData selectedRoomData;
|
|
|
|
public RoomData SelectedRoomData => selectedRoomData;
|
|
|
|
private void Reset()
|
|
{
|
|
detailUI = GetComponentInChildren<RoomDetailUI>(true);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (detailUI == null)
|
|
{
|
|
detailUI = GetComponentInChildren<RoomDetailUI>(true);
|
|
}
|
|
|
|
if (autoFindButtonsInChildren && (roomButtons == null || roomButtons.Length == 0))
|
|
{
|
|
roomButtons = GetComponentsInChildren<RoomButtonUI>(true);
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
RefreshAll();
|
|
|
|
if (selectFirstUnlockedOnEnable)
|
|
{
|
|
SelectFirstUnlockedRoom();
|
|
}
|
|
else if (detailUI != null)
|
|
{
|
|
detailUI.ShowEmpty();
|
|
}
|
|
}
|
|
|
|
public void RefreshAll()
|
|
{
|
|
if (roomButtons == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (RoomButtonUI button in roomButtons)
|
|
{
|
|
if (button == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
button.SetManager(this);
|
|
RoomState state = GetRoomState(button.RoomData);
|
|
button.Refresh(state, button == selectedButton);
|
|
}
|
|
|
|
if (selectedButton != null)
|
|
{
|
|
ShowSelectedRoomDetail();
|
|
}
|
|
}
|
|
|
|
public void SelectRoom(RoomButtonUI button)
|
|
{
|
|
if (button == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
selectedButton = button;
|
|
selectedRoomData = button.RoomData;
|
|
|
|
foreach (RoomButtonUI roomButton in roomButtons)
|
|
{
|
|
if (roomButton == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
roomButton.Refresh(GetRoomState(roomButton.RoomData), roomButton == selectedButton);
|
|
}
|
|
|
|
ShowSelectedRoomDetail();
|
|
}
|
|
|
|
public void SelectRoomByIndex(int index)
|
|
{
|
|
if (roomButtons == null || index < 0 || index >= roomButtons.Length)
|
|
{
|
|
Debug.LogWarning($"[{nameof(RoomSelectManager)}] 잘못된 방 인덱스입니다: {index}");
|
|
return;
|
|
}
|
|
|
|
SelectRoom(roomButtons[index]);
|
|
}
|
|
|
|
public void EnterSelectedRoom()
|
|
{
|
|
if (selectedRoomData == null)
|
|
{
|
|
Debug.LogWarning($"[{nameof(RoomSelectManager)}] 선택된 방이 없습니다.");
|
|
return;
|
|
}
|
|
|
|
RoomState state = GetRoomState(selectedRoomData);
|
|
if (state == RoomState.Locked)
|
|
{
|
|
Debug.Log($"[{nameof(RoomSelectManager)}] 잠긴 방입니다: {selectedRoomData.RoomName}");
|
|
return;
|
|
}
|
|
|
|
if (roomEnterHandler != null)
|
|
{
|
|
roomEnterHandler.EnterRoom(selectedRoomData);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"[{nameof(RoomSelectManager)}] 방 입장 테스트: {selectedRoomData.RoomName}");
|
|
}
|
|
}
|
|
|
|
public void SetCurrentMemoryPieces(int current)
|
|
{
|
|
fallbackCurrentMemoryPieces = Mathf.Max(0, current);
|
|
|
|
if (roomProgressManager != null)
|
|
{
|
|
roomProgressManager.SetMemoryPieceCount(current);
|
|
}
|
|
|
|
RefreshAll();
|
|
}
|
|
|
|
// MemoryProgressManager의 OnProgressChanged(int current, int max) 이벤트와 연결하기 위한 오버로드입니다.
|
|
public void SetCurrentMemoryPieces(int current, int max)
|
|
{
|
|
SetCurrentMemoryPieces(current);
|
|
}
|
|
|
|
public RoomState GetRoomState(RoomData roomData)
|
|
{
|
|
if (roomData == null)
|
|
{
|
|
return RoomState.Locked;
|
|
}
|
|
|
|
if (roomProgressManager != null)
|
|
{
|
|
return roomProgressManager.GetRoomState(roomData);
|
|
}
|
|
|
|
int currentPieces = GetCurrentMemoryPieces();
|
|
if (currentPieces < roomData.RequiredMemoryPieces)
|
|
{
|
|
return RoomState.Locked;
|
|
}
|
|
|
|
if (roomData.DefaultState == RoomState.Locked && currentPieces >= roomData.RequiredMemoryPieces)
|
|
{
|
|
return RoomState.Unlocked;
|
|
}
|
|
|
|
return roomData.DefaultState;
|
|
}
|
|
|
|
public int GetCurrentMemoryPieces()
|
|
{
|
|
return roomProgressManager != null ? roomProgressManager.CurrentMemoryPieces : fallbackCurrentMemoryPieces;
|
|
}
|
|
|
|
private void ShowSelectedRoomDetail()
|
|
{
|
|
if (detailUI == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
RoomState state = GetRoomState(selectedRoomData);
|
|
detailUI.ShowRoom(selectedRoomData, state, GetCurrentMemoryPieces(), EnterSelectedRoom);
|
|
}
|
|
|
|
private void SelectFirstUnlockedRoom()
|
|
{
|
|
if (roomButtons == null || roomButtons.Length == 0)
|
|
{
|
|
if (detailUI != null)
|
|
{
|
|
detailUI.ShowEmpty();
|
|
}
|
|
return;
|
|
}
|
|
|
|
foreach (RoomButtonUI button in roomButtons)
|
|
{
|
|
if (button == null || button.RoomData == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (GetRoomState(button.RoomData) != RoomState.Locked)
|
|
{
|
|
SelectRoom(button);
|
|
return;
|
|
}
|
|
}
|
|
|
|
SelectRoom(roomButtons[0]);
|
|
}
|
|
}
|