Files
AppleHillsProduction/Assets/Scripts/PuzzleGraphUtility.cs

51 lines
1.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using UnityEngine;
public static class PuzzleGraphUtility
{
// Builds a dependency graph: for each step, lists the steps it depends on
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;
}
// Finds initial steps (no dependencies)
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;
}
// Finds all steps unlocked by a given step
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;
}
}