using System; using UnityEngine; namespace DunGen { [Serializable] public class PathStraighteningSettings { /// /// Should we override the default straightening chance? /// public bool OverrideStraightenChance = false; /// /// The chance (0.0 - 1.0) that the next tile spawned will continue in the same direction as the previous tile /// [Range(0.0f, 1.0f)] public float StraightenChance = 0.0f; /// /// Should we override the default straightening for the main path? /// public bool OverrideCanStraightenMainPath = false; /// /// Whether or not the main path should be straightened using /// public bool CanStraightenMainPath = true; /// /// Should we override the default straightening for branch paths? /// public bool OverrideCanStraightenBranchPaths = false; /// /// Whether or not branch paths should be straightened using /// public bool CanStraightenBranchPaths = false; /// /// Gets the final value of a property from a hierarchy of settings objects. /// /// The type of property to get /// A delegate for getting the property from the settings object /// A delegate for getting whether the property is overridden in the settings object /// The hierarchy of settings, starting with the leaf node and ending with the root /// The overridden value, or the value from the root node if no overrides were applied public static T GetFinalValue(Func valueGetter, Func overriddenGetter, params PathStraighteningSettings[] settingsHierarchy) { // Loop through the hierarchy, starting at the leaf and going up to the root for (int i = 0; i < settingsHierarchy.Length; i++) { bool isRoot = i == settingsHierarchy.Length - 1; var settings = settingsHierarchy[i]; // If we've reached the root, we should return its value if (isRoot) return valueGetter(settings); else { // If the current settings object has overridden the value, return it if (overriddenGetter(settings)) return valueGetter(settings); } } return default; } /// /// Gets a new instance of with the final values from the hierarchy /// /// The hierarchy of settings, starting with the leaf node and ending with the root /// The overridden values, or the values from the root node if no overrides were applied public static PathStraighteningSettings GetFinalValues(params PathStraighteningSettings[] settingsHierarchy) { PathStraighteningSettings finalSettings = new PathStraighteningSettings { StraightenChance = GetFinalValue(s => s.StraightenChance, s => s.OverrideStraightenChance, settingsHierarchy), CanStraightenMainPath = GetFinalValue(s => s.CanStraightenMainPath, s => s.OverrideCanStraightenMainPath, settingsHierarchy), CanStraightenBranchPaths = GetFinalValue(s => s.CanStraightenBranchPaths, s => s.OverrideCanStraightenBranchPaths, settingsHierarchy) }; return finalSettings; } } }