Big script cleanup. Remove the examples from Ropes' external package
This commit is contained in:
65
Assets/Scripts/PuzzleS/PuzzleGraphUtility.cs
Normal file
65
Assets/Scripts/PuzzleS/PuzzleGraphUtility.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Utility functions for building and querying puzzle step dependency graphs.
|
||||
/// </summary>
|
||||
public static class PuzzleGraphUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// Builds a dependency graph: for each step, lists the steps it depends on.
|
||||
/// </summary>
|
||||
/// <param name="steps">All puzzle steps.</param>
|
||||
/// <returns>Dictionary mapping each step to the list of steps it depends on.</returns>
|
||||
public static Dictionary<PuzzleStepSO, List<PuzzleStepSO>> BuildDependencyGraph(IEnumerable<PuzzleStepSO> steps)
|
||||
{
|
||||
var graph = new Dictionary<PuzzleStepSO, List<PuzzleStepSO>>();
|
||||
foreach (var step in steps)
|
||||
{
|
||||
graph[step] = new List<PuzzleStepSO>();
|
||||
}
|
||||
foreach (var step in steps)
|
||||
{
|
||||
foreach (var unlocked in step.unlocks)
|
||||
{
|
||||
if (!graph.ContainsKey(unlocked))
|
||||
graph[unlocked] = new List<PuzzleStepSO>();
|
||||
graph[unlocked].Add(step);
|
||||
}
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds initial steps (no dependencies).
|
||||
/// </summary>
|
||||
/// <param name="graph">Dependency graph.</param>
|
||||
/// <returns>List of steps with no dependencies.</returns>
|
||||
public static List<PuzzleStepSO> FindInitialSteps(Dictionary<PuzzleStepSO, List<PuzzleStepSO>> graph)
|
||||
{
|
||||
var initial = new List<PuzzleStepSO>();
|
||||
foreach (var kvp in graph)
|
||||
{
|
||||
if (kvp.Value.Count == 0)
|
||||
initial.Add(kvp.Key);
|
||||
}
|
||||
return initial;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds all steps unlocked by a given step.
|
||||
/// </summary>
|
||||
/// <param name="step">The step to check unlocks for.</param>
|
||||
/// <param name="allSteps">All puzzle steps.</param>
|
||||
/// <returns>List of steps unlocked by the given step.</returns>
|
||||
public static List<PuzzleStepSO> FindUnlocks(PuzzleStepSO step, IEnumerable<PuzzleStepSO> allSteps)
|
||||
{
|
||||
var unlocks = new List<PuzzleStepSO>();
|
||||
foreach (var s in allSteps)
|
||||
{
|
||||
if (s.unlocks.Contains(step))
|
||||
unlocks.Add(s);
|
||||
}
|
||||
return unlocks;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user