Create a simple dialogue authoring system, tied into our items (#10)
- Editor dialogue graph - Asset importer for processing the graph into runtime data - DialogueComponent that steers the dialogue interactions - DialogueCanbas with a scalable speech bubble to display everything - Brief README overview of the system Co-authored-by: AlexanderT <alexander@foolhardyhorizons.com> Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Reviewed-on: #10
This commit is contained in:
@@ -1,196 +1,219 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using PuzzleS;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Manages puzzle step registration, dependency management, and step completion for the puzzle system.
|
||||
/// </summary>
|
||||
public class PuzzleManager : MonoBehaviour
|
||||
namespace PuzzleS
|
||||
{
|
||||
private static PuzzleManager _instance;
|
||||
private static bool _isQuitting = false;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the PuzzleManager.
|
||||
/// Manages puzzle step registration, dependency management, and step completion for the puzzle system.
|
||||
/// </summary>
|
||||
public static PuzzleManager Instance
|
||||
public class PuzzleManager : MonoBehaviour
|
||||
{
|
||||
get
|
||||
private static PuzzleManager _instance;
|
||||
private static bool _isQuitting;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the PuzzleManager.
|
||||
/// </summary>
|
||||
public static PuzzleManager Instance
|
||||
{
|
||||
if (_instance == null && Application.isPlaying && !_isQuitting)
|
||||
get
|
||||
{
|
||||
_instance = FindAnyObjectByType<PuzzleManager>();
|
||||
if (_instance == null)
|
||||
if (_instance == null && Application.isPlaying && !_isQuitting)
|
||||
{
|
||||
var go = new GameObject("PuzzleManager");
|
||||
_instance = go.AddComponent<PuzzleManager>();
|
||||
// DontDestroyOnLoad(go);
|
||||
_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;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
BuildRuntimeDependencies();
|
||||
UnlockInitialSteps();
|
||||
}
|
||||
|
||||
/// <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);
|
||||
Debug.Log($"[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);
|
||||
Debug.Log($"[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])
|
||||
{
|
||||
Debug.Log($"[Puzzles] Step {step.stepId} depends on {dep.stepId}");
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
Debug.Log($"[Puzzles] Runtime dependencies built. Total steps: {_stepBehaviours.Count}");
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
BuildRuntimeDependencies();
|
||||
UnlockInitialSteps();
|
||||
}
|
||||
|
||||
/// <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))
|
||||
/// <summary>
|
||||
/// Unlocks all initial steps (those with no dependencies).
|
||||
/// </summary>
|
||||
private void UnlockInitialSteps()
|
||||
{
|
||||
stepBehaviours.Add(behaviour.stepData, behaviour);
|
||||
Debug.Log($"[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);
|
||||
Debug.Log($"[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])
|
||||
var initialSteps = PuzzleGraphUtility.FindInitialSteps(_runtimeDependencies);
|
||||
foreach (var step in initialSteps)
|
||||
{
|
||||
Debug.Log($"[Puzzles] Step {step.stepId} depends on {dep.stepId}");
|
||||
Debug.Log($"[Puzzles] Initial step unlocked: {step.stepId}");
|
||||
UnlockStep(step);
|
||||
}
|
||||
}
|
||||
Debug.Log($"[Puzzles] Runtime dependencies built. Total steps: {stepBehaviours.Count}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unlocks all initial steps (those with no dependencies).
|
||||
/// </summary>
|
||||
private void UnlockInitialSteps()
|
||||
{
|
||||
var initialSteps = PuzzleGraphUtility.FindInitialSteps(runtimeDependencies);
|
||||
foreach (var step in initialSteps)
|
||||
/// <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)
|
||||
{
|
||||
Debug.Log($"[Puzzles] Initial step unlocked: {step.stepId}");
|
||||
UnlockStep(step);
|
||||
}
|
||||
}
|
||||
if (_completedSteps.Contains(step)) return;
|
||||
_completedSteps.Add(step);
|
||||
Debug.Log($"[Puzzles] Step completed: {step.stepId}");
|
||||
|
||||
/// <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 OnStepCompleted(PuzzleStepSO step)
|
||||
{
|
||||
if (completedSteps.Contains(step)) return;
|
||||
completedSteps.Add(step);
|
||||
Debug.Log($"[Puzzles] Step completed: {step.stepId}");
|
||||
foreach (var unlock in step.unlocks)
|
||||
{
|
||||
if (AreRuntimeDependenciesMet(unlock))
|
||||
// Broadcast completion
|
||||
OnStepCompleted?.Invoke(step);
|
||||
|
||||
foreach (var unlock in step.unlocks)
|
||||
{
|
||||
Debug.Log($"[Puzzles] Unlocking step {unlock.stepId} after completing {step.stepId}");
|
||||
UnlockStep(unlock);
|
||||
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");
|
||||
}
|
||||
}
|
||||
else
|
||||
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])
|
||||
{
|
||||
Debug.Log($"[Puzzles] Step {unlock.stepId} not unlocked yet, waiting for other dependencies");
|
||||
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();
|
||||
}
|
||||
Debug.Log($"[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)
|
||||
{
|
||||
Debug.Log("[Puzzles] Puzzle complete! All steps finished.");
|
||||
// TODO: Fire puzzle complete event or trigger outcome logic
|
||||
}
|
||||
}
|
||||
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])
|
||||
/// <summary>
|
||||
/// Returns whether a step is already unlocked.
|
||||
/// </summary>
|
||||
public bool IsStepUnlocked(PuzzleStepSO step)
|
||||
{
|
||||
if (!completedSteps.Contains(dep)) return false;
|
||||
BuildRuntimeDependencies();
|
||||
UnlockInitialSteps();
|
||||
return _unlockedSteps.Contains(step);
|
||||
}
|
||||
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))
|
||||
/// <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)
|
||||
{
|
||||
behaviour.UnlockStep();
|
||||
return _completedSteps.Any(step => step.stepId == stepId);
|
||||
}
|
||||
Debug.Log($"[Puzzles] Step unlocked: {step.stepId}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the puzzle is complete (all steps finished).
|
||||
/// </summary>
|
||||
private void CheckPuzzleCompletion()
|
||||
{
|
||||
if (completedSteps.Count == stepBehaviours.Count)
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
Debug.Log("[Puzzles] Puzzle complete! All steps finished.");
|
||||
// TODO: Fire puzzle complete event or trigger outcome logic
|
||||
_isQuitting = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether a step is already unlocked.
|
||||
/// </summary>
|
||||
public bool IsStepUnlocked(PuzzleStepSO step)
|
||||
{
|
||||
BuildRuntimeDependencies();
|
||||
UnlockInitialSteps();
|
||||
return unlockedSteps.Contains(step);
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
_isQuitting = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user