160 lines
4.0 KiB
C#
160 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class RoomProgressManager : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class RoomProgressEntry
|
|
{
|
|
public string roomId;
|
|
public RoomState state = RoomState.Unlocked;
|
|
public bool memoryPieceCollected;
|
|
}
|
|
|
|
[Header("Memory Piece Progress")]
|
|
[Min(0)]
|
|
[SerializeField] private int currentMemoryPieces = 0;
|
|
|
|
[Header("Room Progress")]
|
|
[SerializeField] private List<RoomProgressEntry> roomProgressEntries = new List<RoomProgressEntry>();
|
|
|
|
[Header("Events")]
|
|
public UnityEvent onProgressChanged;
|
|
|
|
public int CurrentMemoryPieces => currentMemoryPieces;
|
|
|
|
public void SetMemoryPieceCount(int value)
|
|
{
|
|
currentMemoryPieces = Mathf.Max(0, value);
|
|
onProgressChanged?.Invoke();
|
|
}
|
|
|
|
// MemoryProgressManager의 OnProgressChanged(int current, int max) 같은 이벤트와 연결하기 위한 오버로드입니다.
|
|
public void SetMemoryPieceCount(int current, int max)
|
|
{
|
|
SetMemoryPieceCount(current);
|
|
}
|
|
|
|
public RoomState GetRoomState(RoomData roomData)
|
|
{
|
|
if (roomData == null)
|
|
{
|
|
return RoomState.Locked;
|
|
}
|
|
|
|
RoomProgressEntry entry = FindEntry(roomData.RoomId);
|
|
|
|
if (entry != null)
|
|
{
|
|
if (entry.state == RoomState.Cleared || entry.state == RoomState.MemoryGot)
|
|
{
|
|
return entry.state;
|
|
}
|
|
}
|
|
|
|
if (currentMemoryPieces < roomData.RequiredMemoryPieces)
|
|
{
|
|
return RoomState.Locked;
|
|
}
|
|
|
|
if (entry != null && entry.state != RoomState.Locked)
|
|
{
|
|
return entry.state;
|
|
}
|
|
|
|
if (roomData.DefaultState == RoomState.Locked && currentMemoryPieces >= roomData.RequiredMemoryPieces)
|
|
{
|
|
return RoomState.Unlocked;
|
|
}
|
|
|
|
return roomData.DefaultState;
|
|
}
|
|
|
|
public void SetRoomState(string roomId, RoomState state)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(roomId))
|
|
{
|
|
Debug.LogWarning($"[{nameof(RoomProgressManager)}] roomId가 비어 있습니다.");
|
|
return;
|
|
}
|
|
|
|
RoomProgressEntry entry = FindOrCreateEntry(roomId);
|
|
entry.state = state;
|
|
|
|
if (state == RoomState.MemoryGot)
|
|
{
|
|
entry.memoryPieceCollected = true;
|
|
}
|
|
|
|
onProgressChanged?.Invoke();
|
|
}
|
|
|
|
public void MarkRoomCleared(RoomData roomData)
|
|
{
|
|
if (roomData == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
SetRoomState(roomData.RoomId, RoomState.Cleared);
|
|
}
|
|
|
|
public void MarkMemoryPieceCollected(RoomData roomData)
|
|
{
|
|
if (roomData == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
RoomProgressEntry entry = FindOrCreateEntry(roomData.RoomId);
|
|
entry.memoryPieceCollected = true;
|
|
entry.state = RoomState.MemoryGot;
|
|
onProgressChanged?.Invoke();
|
|
}
|
|
|
|
public bool IsRoomCleared(string roomId)
|
|
{
|
|
RoomProgressEntry entry = FindEntry(roomId);
|
|
return entry != null && (entry.state == RoomState.Cleared || entry.state == RoomState.MemoryGot);
|
|
}
|
|
|
|
public bool HasMemoryPieceCollected(string roomId)
|
|
{
|
|
RoomProgressEntry entry = FindEntry(roomId);
|
|
return entry != null && entry.memoryPieceCollected;
|
|
}
|
|
|
|
public void ResetProgress()
|
|
{
|
|
currentMemoryPieces = 0;
|
|
roomProgressEntries.Clear();
|
|
onProgressChanged?.Invoke();
|
|
}
|
|
|
|
private RoomProgressEntry FindEntry(string roomId)
|
|
{
|
|
return roomProgressEntries.Find(entry => entry.roomId == roomId);
|
|
}
|
|
|
|
private RoomProgressEntry FindOrCreateEntry(string roomId)
|
|
{
|
|
RoomProgressEntry entry = FindEntry(roomId);
|
|
if (entry != null)
|
|
{
|
|
return entry;
|
|
}
|
|
|
|
entry = new RoomProgressEntry
|
|
{
|
|
roomId = roomId,
|
|
state = RoomState.Unlocked,
|
|
memoryPieceCollected = false
|
|
};
|
|
|
|
roomProgressEntries.Add(entry);
|
|
return entry;
|
|
}
|
|
}
|