Files
AppleHillsProduction/Assets/Scripts/PuzzleS/PuzzleManager.cs

342 lines
13 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using AppleHills.Core.Settings;
using Core; // Added for IInteractionSettings
namespace PuzzleS
{
/// <summary>
/// Manages puzzle step registration, dependency management, and step completion for the puzzle system.
/// </summary>
public class PuzzleManager : MonoBehaviour
{
private static PuzzleManager _instance;
private static bool _isQuitting;
[SerializeField] private float proximityCheckInterval = 0.02f;
// Reference to player transform for proximity calculations
private Transform _playerTransform;
private Coroutine _proximityCheckCoroutine;
// Settings reference
private IInteractionSettings _interactionSettings;
/// <summary>
/// Singleton instance of the PuzzleManager.
/// </summary>
public static PuzzleManager Instance
{
get
{
if (_instance == null && Application.isPlaying && !_isQuitting)
{
_instance = FindAnyObjectByType<PuzzleManager>();
if (_instance == null)
{
var go = new GameObject("PuzzleManager");
_instance = go.AddComponent<PuzzleManager>();
// DontDestroyOnLoad(go);
}
}
return _instance;
}
}
// Events to notify about step lifecycle
public event Action<PuzzleStepSO> OnStepCompleted;
public event Action<PuzzleStepSO> OnStepUnlocked;
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>>();
void Awake()
{
_instance = this;
// DontDestroyOnLoad(gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
// Initialize settings reference
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
}
void Start()
{
// Find player transform
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
// Start proximity check coroutine
StartProximityChecks();
}
void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
StopProximityChecks();
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
SceneManager.sceneLoaded -= OnSceneLoaded;
Logging.Debug("[MDPI] OnSceneLoaded");
_runtimeDependencies.Clear();
BuildRuntimeDependencies();
UnlockInitialSteps();
// 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)
{
// Get the proximity threshold from settings directly using our settings reference
float proximityThreshold = _interactionSettings?.DefaultPuzzlePromptRange ?? 3.0f;
// Check distance to each step behavior
foreach (var kvp in _stepBehaviours)
{
if (kvp.Value == null) continue;
if (IsPuzzleStepCompleted(kvp.Value.stepData.stepId)) 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;
}
}
/// <summary>
/// Registers a step behaviour with the manager.
/// </summary>
/// <param name="behaviour">The step behaviour to register.</param>
public void RegisterStepBehaviour(ObjectiveStepBehaviour behaviour)
{
if (behaviour?.stepData == null) return;
if (!_stepBehaviours.ContainsKey(behaviour.stepData))
{
_stepBehaviours.Add(behaviour.stepData, behaviour);
_runtimeDependencies.Clear();
foreach (var step in _stepBehaviours.Values)
{
step.LockStep();
}
_unlockedSteps.Clear();
BuildRuntimeDependencies();
UnlockInitialSteps();
Logging.Debug($"[Puzzles] Registered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
}
}
/// <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);
Logging.Debug($"[Puzzles] Unregistered step: {behaviour.stepData.stepId} on {behaviour.gameObject.name}");
}
/// <summary>
/// Builds the runtime dependency graph for all registered steps.
/// </summary>
private void BuildRuntimeDependencies()
{
_runtimeDependencies = PuzzleGraphUtility.BuildDependencyGraph(_stepBehaviours.Keys);
foreach (var step in _runtimeDependencies.Keys)
{
foreach (var dep in _runtimeDependencies[step])
{
Logging.Debug($"[Puzzles] Step {step.stepId} depends on {dep.stepId}");
}
}
Logging.Debug($"[Puzzles] Runtime dependencies built. Total steps: {_stepBehaviours.Count}");
}
/// <summary>
/// Unlocks all initial steps (those with no dependencies) and any steps whose dependencies are already met.
/// </summary>
private void UnlockInitialSteps()
{
// First, unlock all steps with no dependencies (initial steps)
var initialSteps = PuzzleGraphUtility.FindInitialSteps(_runtimeDependencies);
foreach (var step in initialSteps)
{
Logging.Debug($"[Puzzles] Initial step unlocked: {step.stepId}");
UnlockStep(step);
}
// 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))
{
Logging.Debug($"[Puzzles] Chain step unlocked: {step.stepId}");
UnlockStep(step);
madeProgress = true;
}
}
} while (madeProgress);
}
/// <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)
{
if (_completedSteps.Contains(step)) return;
_completedSteps.Add(step);
Logging.Debug($"[Puzzles] Step completed: {step.stepId}");
// Broadcast completion
OnStepCompleted?.Invoke(step);
foreach (var unlock in step.unlocks)
{
if (AreRuntimeDependenciesMet(unlock))
{
Logging.Debug($"[Puzzles] Unlocking step {unlock.stepId} after completing {step.stepId}");
UnlockStep(unlock);
}
else
{
Logging.Debug($"[Puzzles] Step {unlock.stepId} not unlocked yet, waiting for other dependencies");
}
}
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])
{
if (!_completedSteps.Contains(dep)) return false;
}
return true;
}
/// <summary>
/// Unlocks a specific step and notifies its behaviour.
/// </summary>
/// <param name="step">The step to unlock.</param>
private void UnlockStep(PuzzleStepSO step)
{
if (_unlockedSteps.Contains(step)) return;
_unlockedSteps.Add(step);
if (_stepBehaviours.TryGetValue(step, out var behaviour))
{
behaviour.UnlockStep();
}
Logging.Debug($"[Puzzles] Step unlocked: {step.stepId}");
// Broadcast unlock
OnStepUnlocked?.Invoke(step);
}
/// <summary>
/// Checks if the puzzle is complete (all steps finished).
/// </summary>
private void CheckPuzzleCompletion()
{
if (_completedSteps.Count == _stepBehaviours.Count)
{
Logging.Debug("[Puzzles] Puzzle complete! All steps finished.");
// TODO: Fire puzzle complete event or trigger outcome logic
}
}
/// <summary>
/// Returns whether a step is already unlocked.
/// </summary>
public bool IsStepUnlocked(PuzzleStepSO step)
{
// _runtimeDependencies.Clear();
// BuildRuntimeDependencies();
// UnlockInitialSteps();
return _unlockedSteps.Contains(step);
}
/// <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);
}
void OnApplicationQuit()
{
_isQuitting = true;
}
}
}