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-07 10:44:26 +02:00
|
|
|
|
using AppleHills.Core.Settings; // Added for IInteractionSettings
|
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-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Singleton instance of the PuzzleManager.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static PuzzleManager Instance
|
2025-09-03 16:55:21 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
get
|
2025-09-03 16:55:21 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (_instance == null && Application.isPlaying && !_isQuitting)
|
2025-09-03 16:55:21 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
_instance = FindAnyObjectByType<PuzzleManager>();
|
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var go = new GameObject("PuzzleManager");
|
|
|
|
|
|
_instance = go.AddComponent<PuzzleManager>();
|
|
|
|
|
|
// DontDestroyOnLoad(go);
|
|
|
|
|
|
}
|
2025-09-03 16:55:21 +02:00
|
|
|
|
}
|
2025-09-29 09:34:15 +00:00
|
|
|
|
return _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-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>();
|
|
|
|
|
|
// Runtime dependency graph
|
|
|
|
|
|
private Dictionary<PuzzleStepSO, List<PuzzleStepSO>> _runtimeDependencies = new Dictionary<PuzzleStepSO, List<PuzzleStepSO>>();
|
2025-09-03 15:43:47 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
_instance = this;
|
|
|
|
|
|
// DontDestroyOnLoad(gameObject);
|
|
|
|
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
2025-10-07 10:44:26 +02:00
|
|
|
|
|
|
|
|
|
|
// Initialize settings reference
|
|
|
|
|
|
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-09-11 15:48:44 +02:00
|
|
|
|
|
2025-10-02 05:42:17 +00:00
|
|
|
|
void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Find player transform
|
|
|
|
|
|
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
|
|
|
|
|
|
|
|
|
|
|
|
// Start proximity check coroutine
|
|
|
|
|
|
StartProximityChecks();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
2025-10-02 05:42:17 +00:00
|
|
|
|
StopProximityChecks();
|
2025-09-29 09:34:15 +00:00
|
|
|
|
}
|
2025-09-11 15:48:44 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
|
|
|
|
{
|
2025-10-02 07:16:39 +02:00
|
|
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log("[MDPI] OnSceneLoaded");
|
|
|
|
|
|
_runtimeDependencies.Clear();
|
2025-09-29 09:34:15 +00:00
|
|
|
|
BuildRuntimeDependencies();
|
|
|
|
|
|
UnlockInitialSteps();
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
|
|
if (!_stepBehaviours.ContainsKey(behaviour.stepData))
|
|
|
|
|
|
{
|
|
|
|
|
|
_stepBehaviours.Add(behaviour.stepData, behaviour);
|
2025-10-02 07:16:39 +02:00
|
|
|
|
_runtimeDependencies.Clear();
|
|
|
|
|
|
foreach (var step in _stepBehaviours.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
step.LockStep();
|
|
|
|
|
|
}
|
|
|
|
|
|
_unlockedSteps.Clear();
|
|
|
|
|
|
BuildRuntimeDependencies();
|
|
|
|
|
|
UnlockInitialSteps();
|
2025-09-29 09:34:15 +00:00
|
|
|
|
Debug.Log($"[Puzzles] Registered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
|
|
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
_stepBehaviours.Remove(behaviour.stepData);
|
|
|
|
|
|
Debug.Log($"[Puzzles] Unregistered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
|
|
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Builds the runtime dependency graph for all registered steps.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void BuildRuntimeDependencies()
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
_runtimeDependencies = PuzzleGraphUtility.BuildDependencyGraph(_stepBehaviours.Keys);
|
|
|
|
|
|
foreach (var step in _runtimeDependencies.Keys)
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
foreach (var dep in _runtimeDependencies[step])
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[Puzzles] Step {step.stepId} depends on {dep.stepId}");
|
|
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
2025-09-29 09:34:15 +00:00
|
|
|
|
Debug.Log($"[Puzzles] Runtime dependencies built. Total steps: {_stepBehaviours.Count}");
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
2025-10-07 10:57:11 +00:00
|
|
|
|
/// Unlocks all initial steps (those with no dependencies) and any steps whose dependencies are already met.
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UnlockInitialSteps()
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-10-07 10:57:11 +00:00
|
|
|
|
// First, unlock all steps with no dependencies (initial steps)
|
2025-09-29 09:34:15 +00:00
|
|
|
|
var initialSteps = PuzzleGraphUtility.FindInitialSteps(_runtimeDependencies);
|
|
|
|
|
|
foreach (var step in initialSteps)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[Puzzles] Initial step unlocked: {step.stepId}");
|
|
|
|
|
|
UnlockStep(step);
|
|
|
|
|
|
}
|
2025-10-07 10:57:11 +00:00
|
|
|
|
|
|
|
|
|
|
// Keep trying to unlock steps as long as we're making progress
|
|
|
|
|
|
bool madeProgress;
|
|
|
|
|
|
do
|
|
|
|
|
|
{
|
|
|
|
|
|
madeProgress = false;
|
|
|
|
|
|
|
|
|
|
|
|
// Check all steps that haven't been unlocked yet
|
|
|
|
|
|
foreach (var step in _runtimeDependencies.Keys.Where(s => !_unlockedSteps.Contains(s)))
|
|
|
|
|
|
{
|
|
|
|
|
|
// Check if all dependencies have been completed
|
|
|
|
|
|
if (AreRuntimeDependenciesMet(step))
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[Puzzles] Chain step unlocked: {step.stepId}");
|
|
|
|
|
|
UnlockStep(step);
|
|
|
|
|
|
madeProgress = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} while (madeProgress);
|
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;
|
|
|
|
|
|
_completedSteps.Add(step);
|
|
|
|
|
|
Debug.Log($"[Puzzles] Step completed: {step.stepId}");
|
|
|
|
|
|
|
|
|
|
|
|
// Broadcast completion
|
|
|
|
|
|
OnStepCompleted?.Invoke(step);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var unlock in step.unlocks)
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (AreRuntimeDependenciesMet(unlock))
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[Puzzles] Unlocking step {unlock.stepId} after completing {step.stepId}");
|
|
|
|
|
|
UnlockStep(unlock);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[Puzzles] Step {unlock.stepId} not unlocked yet, waiting for other dependencies");
|
|
|
|
|
|
}
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
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>
|
|
|
|
|
|
private bool AreRuntimeDependenciesMet(PuzzleStepSO step)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_runtimeDependencies.ContainsKey(step) || _runtimeDependencies[step].Count == 0) return true;
|
|
|
|
|
|
foreach (var dep in _runtimeDependencies[step])
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (!_completedSteps.Contains(dep)) return false;
|
2025-09-03 15:43:47 +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);
|
|
|
|
|
|
if (_stepBehaviours.TryGetValue(step, out var behaviour))
|
|
|
|
|
|
{
|
|
|
|
|
|
behaviour.UnlockStep();
|
|
|
|
|
|
}
|
|
|
|
|
|
Debug.Log($"[Puzzles] Step unlocked: {step.stepId}");
|
|
|
|
|
|
|
|
|
|
|
|
// Broadcast unlock
|
|
|
|
|
|
OnStepUnlocked?.Invoke(step);
|
2025-09-03 15:43:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:34:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if the puzzle is complete (all steps finished).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void CheckPuzzleCompletion()
|
2025-09-03 15:43:47 +02:00
|
|
|
|
{
|
2025-09-29 09:34:15 +00:00
|
|
|
|
if (_completedSteps.Count == _stepBehaviours.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("[Puzzles] Puzzle complete! All steps finished.");
|
|
|
|
|
|
// TODO: Fire puzzle complete event or trigger outcome logic
|
|
|
|
|
|
}
|
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-10-02 07:16:39 +02:00
|
|
|
|
// _runtimeDependencies.Clear();
|
|
|
|
|
|
// BuildRuntimeDependencies();
|
|
|
|
|
|
// UnlockInitialSteps();
|
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-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
|
|
|
|
}
|