2025-09-29 09:34:15 +00:00
|
|
|
|
using System;
|
2025-10-02 05:42:17 +00:00
|
|
|
|
using System.Collections;
|
2025-09-03 15:43:47 +02:00
|
|
|
|
using System.Collections.Generic;
|
2025-09-29 09:34:15 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using UnityEngine;
|
2025-09-11 15:48:44 +02:00
|
|
|
|
using UnityEngine.SceneManagement;
|
2025-10-14 15:53:58 +02:00
|
|
|
|
using AppleHills.Core.Settings;
|
2025-10-16 19:43:19 +02:00
|
|
|
|
using Bootstrap;
|
|
|
|
|
|
using Core;
|
|
|
|
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
|
|
|
|
using Utils;
|
2025-09-03 15:43:47 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
namespace PuzzleS
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// Manages puzzle step registration, dependency management, and step completion for the puzzle system.
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// </summary>
|
2025-09-29 09:34:15 +00:00
|
|
|
|
public class PuzzleManager : MonoBehaviour
|
2025-09-03 16:55:21 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
private static PuzzleManager _instance;
|
|
|
|
|
|
private static bool _isQuitting;
|
|
|
|
|
|
|
2025-10-02 05:42:17 +00:00
|
|
|
|
[SerializeField] private float proximityCheckInterval = 0.02f;
|
|
|
|
|
|
|
|
|
|
|
|
// Reference to player transform for proximity calculations
|
|
|
|
|
|
private Transform _playerTransform;
|
|
|
|
|
|
private Coroutine _proximityCheckCoroutine;
|
|
|
|
|
|
|
2025-10-07 10:44:26 +02:00
|
|
|
|
// Settings reference
|
|
|
|
|
|
private IInteractionSettings _interactionSettings;
|
|
|
|
|
|
|
2025-10-16 19:43:19 +02:00
|
|
|
|
// Current level puzzle data
|
|
|
|
|
|
private PuzzleLevelDataSO _currentLevelData;
|
|
|
|
|
|
private AsyncOperationHandle<PuzzleLevelDataSO> _levelDataLoadOperation;
|
|
|
|
|
|
private bool _isDataLoaded = false;
|
|
|
|
|
|
|
|
|
|
|
|
// Store registered behaviors that are waiting for data to be loaded
|
|
|
|
|
|
private List<ObjectiveStepBehaviour> _registeredBehaviours = new List<ObjectiveStepBehaviour>();
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Singleton instance of the PuzzleManager.
|
|
|
|
|
|
/// </summary>
|
2025-10-16 19:43:19 +02:00
|
|
|
|
public static PuzzleManager Instance => _instance;
|
2025-09-03 16:55:21 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
// Events to notify about step lifecycle
|
|
|
|
|
|
public event Action<PuzzleStepSO> OnStepCompleted;
|
|
|
|
|
|
public event Action<PuzzleStepSO> OnStepUnlocked;
|
2025-10-16 19:43:19 +02:00
|
|
|
|
public event Action<PuzzleLevelDataSO> OnLevelDataLoaded;
|
|
|
|
|
|
public event Action<PuzzleLevelDataSO> OnAllPuzzlesComplete;
|
2025-09-03 15:43:47 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
private HashSet<PuzzleStepSO> _completedSteps = new HashSet<PuzzleStepSO>();
|
|
|
|
|
|
private HashSet<PuzzleStepSO> _unlockedSteps = new HashSet<PuzzleStepSO>();
|
|
|
|
|
|
// Registration for ObjectiveStepBehaviour
|
|
|
|
|
|
private Dictionary<PuzzleStepSO, ObjectiveStepBehaviour> _stepBehaviours = new Dictionary<PuzzleStepSO, ObjectiveStepBehaviour>();
|
2025-09-03 15:43:47 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
_instance = this;
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
2025-10-07 10:44:26 +02:00
|
|
|
|
// Initialize settings reference
|
|
|
|
|
|
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
// Register for post-boot initialization
|
|
|
|
|
|
BootCompletionService.RegisterInitAction(InitializePostBoot);
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
private void InitializePostBoot()
|
2025-10-02 05:42:17 +00:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
// Subscribe to SceneManagerService events after boot is complete
|
|
|
|
|
|
SceneManagerService.Instance.SceneLoadCompleted += OnSceneLoadCompleted;
|
|
|
|
|
|
SceneManagerService.Instance.SceneLoadStarted += OnSceneLoadStarted;
|
|
|
|
|
|
|
2025-10-02 05:42:17 +00:00
|
|
|
|
// Find player transform
|
|
|
|
|
|
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
|
|
|
|
|
|
|
|
|
|
|
|
// Start proximity check coroutine
|
|
|
|
|
|
StartProximityChecks();
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
// Load puzzle data for the current scene if not already loading
|
|
|
|
|
|
if (_currentLevelData == null && !_isDataLoaded)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadPuzzleDataForCurrentScene();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Logging.Debug("[PuzzleManager] Subscribed to SceneManagerService events");
|
2025-10-02 05:42:17 +00:00
|
|
|
|
}
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
void OnDestroy()
|
|
|
|
|
|
{
|
2025-10-02 05:42:17 +00:00
|
|
|
|
StopProximityChecks();
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
// Unsubscribe from scene manager events
|
|
|
|
|
|
if (SceneManagerService.Instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SceneManagerService.Instance.SceneLoadCompleted -= OnSceneLoadCompleted;
|
|
|
|
|
|
SceneManagerService.Instance.SceneLoadStarted -= OnSceneLoadStarted;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Release addressable handle if needed
|
|
|
|
|
|
if (_levelDataLoadOperation.IsValid())
|
|
|
|
|
|
{
|
|
|
|
|
|
Addressables.Release(_levelDataLoadOperation);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Called when a scene is starting to load
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void OnSceneLoadStarted(string sceneName)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Reset data loaded state when changing scenes to avoid using stale data
|
|
|
|
|
|
_isDataLoaded = false;
|
|
|
|
|
|
Logging.Debug($"[Puzzles] Scene load started: {sceneName}, marked puzzle data as not loaded");
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-09-11 15:48:44 +02:00
|
|
|
|
|
2025-10-16 19:43:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Called when a scene is loaded
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void OnSceneLoadCompleted(string sceneName)
|
2025-09-29 09:34:15 +00:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
// Skip for non-gameplay scenes
|
|
|
|
|
|
if (sceneName == "BootstrapScene" || string.IsNullOrEmpty(sceneName))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-10-02 07:16:39 +02:00
|
|
|
|
|
2025-10-16 19:43:19 +02:00
|
|
|
|
Logging.Debug($"[Puzzles] Scene loaded: {sceneName}, loading puzzle data");
|
|
|
|
|
|
LoadPuzzleDataForCurrentScene(sceneName);
|
2025-10-02 05:42:17 +00:00
|
|
|
|
|
|
|
|
|
|
// Find player transform again in case it changed with scene load
|
|
|
|
|
|
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
|
|
|
|
|
|
|
|
|
|
|
|
// Restart proximity checks
|
|
|
|
|
|
StartProximityChecks();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-16 19:43:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Load puzzle data for the current scene
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void LoadPuzzleDataForCurrentScene(string sceneName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string currentScene = sceneName ?? SceneManagerService.Instance.CurrentGameplayScene;
|
|
|
|
|
|
if (string.IsNullOrEmpty(currentScene))
|
|
|
|
|
|
{
|
|
|
|
|
|
Logging.Warning("[Puzzles] Cannot load puzzle data: Current scene name is empty");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_isDataLoaded = false;
|
|
|
|
|
|
string addressablePath = $"Puzzles/{currentScene}";
|
|
|
|
|
|
|
|
|
|
|
|
Logging.Debug($"[Puzzles] Loading puzzle data from addressable: {addressablePath}");
|
|
|
|
|
|
|
|
|
|
|
|
// Release previous handle if needed
|
|
|
|
|
|
if (_levelDataLoadOperation.IsValid())
|
|
|
|
|
|
{
|
|
|
|
|
|
Addressables.Release(_levelDataLoadOperation);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the addressable exists before trying to load it
|
|
|
|
|
|
if (!AppleHillsUtils.AddressableKeyExists(addressablePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Logging.Warning($"[Puzzles] Puzzle data does not exist for scene: {currentScene}");
|
|
|
|
|
|
_isDataLoaded = true; // Mark as loaded but with no data
|
|
|
|
|
|
_currentLevelData = null;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Load the level data asset
|
|
|
|
|
|
_levelDataLoadOperation = Addressables.LoadAssetAsync<PuzzleLevelDataSO>(addressablePath);
|
|
|
|
|
|
_levelDataLoadOperation.Completed += handle =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
_currentLevelData = handle.Result;
|
|
|
|
|
|
Logging.Debug($"[Puzzles] Loaded level data: {_currentLevelData.levelId} with {_currentLevelData.allSteps.Count} steps");
|
|
|
|
|
|
|
|
|
|
|
|
// Reset state
|
|
|
|
|
|
_completedSteps.Clear();
|
|
|
|
|
|
_unlockedSteps.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
// Unlock initial steps
|
|
|
|
|
|
UnlockInitialSteps();
|
|
|
|
|
|
|
|
|
|
|
|
// Update all registered behaviors now that data is loaded
|
|
|
|
|
|
UpdateAllRegisteredBehaviors();
|
|
|
|
|
|
|
|
|
|
|
|
// Mark data as loaded
|
|
|
|
|
|
_isDataLoaded = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Notify listeners
|
|
|
|
|
|
OnLevelDataLoaded?.Invoke(_currentLevelData);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Logging.Warning($"[Puzzles] Failed to load puzzle data for {currentScene}: {handle.OperationException?.Message}");
|
|
|
|
|
|
_isDataLoaded = true; // Mark as loaded but with error
|
|
|
|
|
|
_currentLevelData = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 05:42:17 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Start the proximity check coroutine.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void StartProximityChecks()
|
|
|
|
|
|
{
|
|
|
|
|
|
StopProximityChecks();
|
|
|
|
|
|
_proximityCheckCoroutine = StartCoroutine(CheckProximityRoutine());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Stop the proximity check coroutine.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void StopProximityChecks()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_proximityCheckCoroutine != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
StopCoroutine(_proximityCheckCoroutine);
|
|
|
|
|
|
_proximityCheckCoroutine = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Coroutine that periodically checks player proximity to all puzzle steps.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private IEnumerator CheckProximityRoutine()
|
|
|
|
|
|
{
|
|
|
|
|
|
WaitForSeconds wait = new WaitForSeconds(proximityCheckInterval);
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_playerTransform != null)
|
|
|
|
|
|
{
|
2025-10-07 10:44:26 +02:00
|
|
|
|
// Get the proximity threshold from settings directly using our settings reference
|
|
|
|
|
|
float proximityThreshold = _interactionSettings?.DefaultPuzzlePromptRange ?? 3.0f;
|
2025-10-02 05:42:17 +00:00
|
|
|
|
|
|
|
|
|
|
// Check distance to each step behavior
|
|
|
|
|
|
foreach (var kvp in _stepBehaviours)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (kvp.Value == null) continue;
|
2025-10-16 19:43:19 +02:00
|
|
|
|
if (IsPuzzleStepCompleted(kvp.Key.stepId)) continue;
|
2025-10-02 05:42:17 +00:00
|
|
|
|
|
|
|
|
|
|
float distance = Vector3.Distance(_playerTransform.position, kvp.Value.transform.position);
|
|
|
|
|
|
|
|
|
|
|
|
// Determine the proximity state - only Close or Far now
|
|
|
|
|
|
ObjectiveStepBehaviour.ProximityState state =
|
|
|
|
|
|
(distance <= proximityThreshold)
|
|
|
|
|
|
? ObjectiveStepBehaviour.ProximityState.Close
|
|
|
|
|
|
: ObjectiveStepBehaviour.ProximityState.Far;
|
|
|
|
|
|
|
|
|
|
|
|
// Update the step's proximity state
|
|
|
|
|
|
kvp.Value.UpdateProximityState(state);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
yield return wait;
|
|
|
|
|
|
}
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
|
2025-10-16 19:43:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Update all registered behaviors with their current state
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UpdateAllRegisteredBehaviors()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var behaviour in _registeredBehaviours)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (behaviour == null) continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Only update if the step is in our dictionary
|
|
|
|
|
|
bool stepUnlocked = IsStepUnlocked(behaviour.stepData);
|
|
|
|
|
|
if (stepUnlocked)
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdateStepState(behaviour);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Registers a step behaviour with the manager.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="behaviour">The step behaviour to register.</param>
|
|
|
|
|
|
public void RegisterStepBehaviour(ObjectiveStepBehaviour behaviour)
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (behaviour?.stepData == null) return;
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
// Always add to our registered behaviors list
|
|
|
|
|
|
if (!_registeredBehaviours.Contains(behaviour))
|
|
|
|
|
|
{
|
|
|
|
|
|
_registeredBehaviours.Add(behaviour);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add to the step behaviours dictionary if not already there
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (!_stepBehaviours.ContainsKey(behaviour.stepData))
|
|
|
|
|
|
{
|
|
|
|
|
|
_stepBehaviours.Add(behaviour.stepData, behaviour);
|
2025-10-16 19:43:19 +02:00
|
|
|
|
Logging.Debug($"[Puzzles] Registered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
|
|
|
|
|
|
|
|
|
|
|
|
// Only update state if data is already loaded
|
|
|
|
|
|
if (_isDataLoaded && _currentLevelData != null)
|
2025-10-02 07:16:39 +02:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
UpdateStepState(behaviour);
|
2025-10-02 07:16:39 +02:00
|
|
|
|
}
|
2025-10-16 19:43:19 +02:00
|
|
|
|
// Otherwise, the state will be updated when data loads in UpdateAllRegisteredBehaviors
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Updates a step's state based on the current puzzle state.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UpdateStepState(ObjectiveStepBehaviour behaviour)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (behaviour?.stepData == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
// If step is already completed, ignore
|
|
|
|
|
|
if (_completedSteps.Contains(behaviour.stepData))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// If step is already unlocked, update the behaviour
|
|
|
|
|
|
if (_unlockedSteps.Contains(behaviour.stepData))
|
|
|
|
|
|
{
|
|
|
|
|
|
behaviour.UnlockStep();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Make sure it's locked
|
|
|
|
|
|
behaviour.LockStep();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unregisters a step behaviour from the manager.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="behaviour">The step behaviour to unregister.</param>
|
|
|
|
|
|
public void UnregisterStepBehaviour(ObjectiveStepBehaviour behaviour)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (behaviour?.stepData == null) return;
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
_stepBehaviours.Remove(behaviour.stepData);
|
2025-10-16 19:43:19 +02:00
|
|
|
|
_registeredBehaviours.Remove(behaviour);
|
|
|
|
|
|
|
2025-10-14 15:53:58 +02:00
|
|
|
|
Logging.Debug($"[Puzzles] Unregistered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
2025-10-16 19:43:19 +02:00
|
|
|
|
/// Unlocks all initial steps (those with no dependencies)
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UnlockInitialSteps()
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
if (_currentLevelData == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
// Unlock initial steps
|
|
|
|
|
|
foreach (var step in _currentLevelData.initialSteps)
|
2025-09-29 09:34:15 +00:00
|
|
|
|
{
|
|
|
|
|
|
UnlockStep(step);
|
|
|
|
|
|
}
|
2025-10-07 10:57:11 +00:00
|
|
|
|
|
2025-10-16 19:43:19 +02:00
|
|
|
|
Logging.Debug($"[Puzzles] Unlocked {_unlockedSteps.Count} initial steps");
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Called when a step is completed. Unlocks dependent steps if their dependencies are met.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="step">The completed step.</param>
|
|
|
|
|
|
public void MarkPuzzleStepCompleted(PuzzleStepSO step)
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (_completedSteps.Contains(step)) return;
|
2025-10-16 19:43:19 +02:00
|
|
|
|
if (_currentLevelData == null) return;
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
_completedSteps.Add(step);
|
2025-10-14 15:53:58 +02:00
|
|
|
|
Logging.Debug($"[Puzzles] Step completed: {step.stepId}");
|
2025-09-29 09:34:15 +00:00
|
|
|
|
|
|
|
|
|
|
// Broadcast completion
|
|
|
|
|
|
OnStepCompleted?.Invoke(step);
|
|
|
|
|
|
|
2025-10-16 19:43:19 +02:00
|
|
|
|
// Unlock steps that are unlocked by this step
|
|
|
|
|
|
foreach (var unlockStep in _currentLevelData.GetUnlockedSteps(step))
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
if (AreStepDependenciesMet(unlockStep))
|
2025-09-29 09:34:15 +00:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
Logging.Debug($"[Puzzles] Unlocking step {unlockStep.stepId} after completing {step.stepId}");
|
|
|
|
|
|
UnlockStep(unlockStep);
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
Logging.Debug($"[Puzzles] Step {unlockStep.stepId} not unlocked yet, waiting for other dependencies");
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
// Check if all puzzle steps are now complete
|
2025-09-29 09:34:15 +00:00
|
|
|
|
CheckPuzzleCompletion();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if all dependencies for a step are met.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="step">The step to check.</param>
|
|
|
|
|
|
/// <returns>True if all dependencies are met, false otherwise.</returns>
|
2025-10-16 19:43:19 +02:00
|
|
|
|
private bool AreStepDependenciesMet(PuzzleStepSO step)
|
2025-09-29 09:34:15 +00:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
if (_currentLevelData == null || step == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
// If it's an initial step, it has no dependencies
|
|
|
|
|
|
if (_currentLevelData.IsInitialStep(step)) return true;
|
|
|
|
|
|
|
|
|
|
|
|
// Check if dependencies are met using pre-processed data
|
|
|
|
|
|
if (_currentLevelData.stepDependencies.TryGetValue(step.stepId, out string[] dependencies))
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
foreach (var depId in dependencies)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Find the dependency step
|
|
|
|
|
|
bool dependencyMet = false;
|
|
|
|
|
|
foreach (var completedStep in _completedSteps)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (completedStep.stepId == depId)
|
|
|
|
|
|
{
|
|
|
|
|
|
dependencyMet = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!dependencyMet) return false;
|
|
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
return true;
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unlocks a specific step and notifies its behaviour.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="step">The step to unlock.</param>
|
|
|
|
|
|
private void UnlockStep(PuzzleStepSO step)
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (_unlockedSteps.Contains(step)) return;
|
|
|
|
|
|
_unlockedSteps.Add(step);
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (_stepBehaviours.TryGetValue(step, out var behaviour))
|
|
|
|
|
|
{
|
|
|
|
|
|
behaviour.UnlockStep();
|
|
|
|
|
|
}
|
2025-10-14 15:53:58 +02:00
|
|
|
|
Logging.Debug($"[Puzzles] Step unlocked: {step.stepId}");
|
2025-09-29 09:34:15 +00:00
|
|
|
|
|
|
|
|
|
|
// Broadcast unlock
|
|
|
|
|
|
OnStepUnlocked?.Invoke(step);
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
2025-10-16 19:43:19 +02:00
|
|
|
|
/// Checks if the puzzle is complete (all steps in level finished).
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void CheckPuzzleCompletion()
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
if (_currentLevelData == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (_currentLevelData.IsLevelComplete(_completedSteps))
|
2025-09-29 09:34:15 +00:00
|
|
|
|
{
|
2025-10-16 19:43:19 +02:00
|
|
|
|
Logging.Debug("[Puzzles] All puzzles complete! Level finished.");
|
|
|
|
|
|
|
|
|
|
|
|
// Fire level complete event
|
|
|
|
|
|
OnAllPuzzlesComplete?.Invoke(_currentLevelData);
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns whether a step is already unlocked.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool IsStepUnlocked(PuzzleStepSO step)
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
return _unlockedSteps.Contains(step);
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
2025-09-08 08:45:13 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if a puzzle step with the specified ID has been completed
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="stepId">The ID of the puzzle step to check</param>
|
|
|
|
|
|
/// <returns>True if the step has been completed, false otherwise</returns>
|
|
|
|
|
|
public bool IsPuzzleStepCompleted(string stepId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _completedSteps.Any(step => step.stepId == stepId);
|
|
|
|
|
|
}
|
2025-10-16 19:43:19 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the current level puzzle data
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public PuzzleLevelDataSO GetCurrentLevelData()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _currentLevelData;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if puzzle data is loaded
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if data loading has completed (whether successful or not)</returns>
|
|
|
|
|
|
public bool IsDataLoaded()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _isDataLoaded;
|
|
|
|
|
|
}
|
2025-09-12 14:38:56 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
void OnApplicationQuit()
|
|
|
|
|
|
{
|
|
|
|
|
|
_isQuitting = true;
|
|
|
|
|
|
}
|
2025-09-08 08:45:13 +02:00
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|