Update minigame switcher to only unlock when all puzzles completed
This commit is contained in:
@@ -4,7 +4,10 @@ using Core;
|
||||
using Input;
|
||||
using Interactions;
|
||||
using System.Threading.Tasks;
|
||||
using Bootstrap;
|
||||
using PuzzleS;
|
||||
using UnityEngine;
|
||||
using Core.SaveLoad;
|
||||
|
||||
// Added for IInteractionSettings
|
||||
|
||||
@@ -32,6 +35,9 @@ namespace Levels
|
||||
/// </summary>
|
||||
void Awake()
|
||||
{
|
||||
gameObject.SetActive(false); // Start inactive
|
||||
BootCompletionService.RegisterInitAction(InitializePostBoot);
|
||||
|
||||
_isActive = true;
|
||||
if (_iconRenderer == null)
|
||||
_iconRenderer = GetComponent<SpriteRenderer>();
|
||||
@@ -40,22 +46,71 @@ namespace Levels
|
||||
{
|
||||
_interactable.characterArrived.AddListener(OnCharacterArrived);
|
||||
}
|
||||
|
||||
// Initialize settings reference
|
||||
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
|
||||
|
||||
ApplySwitchData();
|
||||
|
||||
// --- Save state loading logic ---
|
||||
if (SaveLoadManager.Instance != null)
|
||||
{
|
||||
if (SaveLoadManager.Instance.IsSaveDataLoaded)
|
||||
{
|
||||
ApplySavedMinigameStateIfAvailable();
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveLoadManager.Instance.OnLoadCompleted += OnSaveDataLoadedHandler;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity OnDestroy callback. Cleans up event handlers.
|
||||
/// </summary>
|
||||
void OnDestroy()
|
||||
private void OnDestroy()
|
||||
{
|
||||
PuzzleManager.Instance.OnAllPuzzlesComplete -= HandleAllPuzzlesComplete;
|
||||
if (_interactable != null)
|
||||
{
|
||||
_interactable.characterArrived.RemoveListener(OnCharacterArrived);
|
||||
}
|
||||
if (SaveLoadManager.Instance != null)
|
||||
{
|
||||
SaveLoadManager.Instance.OnLoadCompleted -= OnSaveDataLoadedHandler;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply saved state if present in the SaveLoadManager
|
||||
private void ApplySavedMinigameStateIfAvailable()
|
||||
{
|
||||
var data = SaveLoadManager.Instance?.currentSaveData;
|
||||
if (data?.unlockedMinigames != null && switchData != null &&
|
||||
data.unlockedMinigames.Contains(switchData.targetLevelSceneName))
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
// Event handler for when save data load completes
|
||||
private void OnSaveDataLoadedHandler(string slot)
|
||||
{
|
||||
ApplySavedMinigameStateIfAvailable();
|
||||
if (SaveLoadManager.Instance != null)
|
||||
{
|
||||
SaveLoadManager.Instance.OnLoadCompleted -= OnSaveDataLoadedHandler;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleAllPuzzlesComplete(PuzzleS.PuzzleLevelDataSO _)
|
||||
{
|
||||
// Unlock and save
|
||||
if (switchData != null)
|
||||
{
|
||||
var unlocked = SaveLoadManager.Instance.currentSaveData.unlockedMinigames;
|
||||
string minigameName = switchData.targetLevelSceneName;
|
||||
if (!unlocked.Contains(minigameName))
|
||||
{
|
||||
unlocked.Add(minigameName);
|
||||
}
|
||||
}
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
@@ -131,5 +186,10 @@ namespace Levels
|
||||
_isActive = true; // Allow interaction again if cancelled
|
||||
InputManager.Instance.SetInputMode(InputMode.GameAndUI);
|
||||
}
|
||||
|
||||
private void InitializePostBoot()
|
||||
{
|
||||
PuzzleManager.Instance.OnAllPuzzlesComplete += HandleAllPuzzlesComplete;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user