PathStraighteningSettings.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using UnityEngine;
  3. namespace DunGen
  4. {
  5. [Serializable]
  6. public class PathStraighteningSettings
  7. {
  8. /// <summary>
  9. /// Should we override the default straightening chance?
  10. /// </summary>
  11. public bool OverrideStraightenChance = false;
  12. /// <summary>
  13. /// The chance (0.0 - 1.0) that the next tile spawned will continue in the same direction as the previous tile
  14. /// </summary>
  15. [Range(0.0f, 1.0f)]
  16. public float StraightenChance = 0.0f;
  17. /// <summary>
  18. /// Should we override the default straightening for the main path?
  19. /// </summary>
  20. public bool OverrideCanStraightenMainPath = false;
  21. /// <summary>
  22. /// Whether or not the main path should be straightened using <see cref="StraightenChance"/>
  23. /// </summary>
  24. public bool CanStraightenMainPath = true;
  25. /// <summary>
  26. /// Should we override the default straightening for branch paths?
  27. /// </summary>
  28. public bool OverrideCanStraightenBranchPaths = false;
  29. /// <summary>
  30. /// Whether or not branch paths should be straightened using <see cref="StraightenChance"/>
  31. /// </summary>
  32. public bool CanStraightenBranchPaths = false;
  33. /// <summary>
  34. /// Gets the final value of a property from a hierarchy of settings objects.
  35. /// </summary>
  36. /// <typeparam name="T">The type of property to get</typeparam>
  37. /// <param name="valueGetter">A delegate for getting the property from the settings object</param>
  38. /// <param name="overriddenGetter">A delegate for getting whether the property is overridden in the settings object</param>
  39. /// <param name="settingsHierarchy">The hierarchy of settings, starting with the leaf node and ending with the root</param>
  40. /// <returns>The overridden value, or the value from the root node if no overrides were applied</returns>
  41. public static T GetFinalValue<T>(Func<PathStraighteningSettings, T> valueGetter, Func<PathStraighteningSettings, bool> overriddenGetter, params PathStraighteningSettings[] settingsHierarchy)
  42. {
  43. // Loop through the hierarchy, starting at the leaf and going up to the root
  44. for (int i = 0; i < settingsHierarchy.Length; i++)
  45. {
  46. bool isRoot = i == settingsHierarchy.Length - 1;
  47. var settings = settingsHierarchy[i];
  48. // If we've reached the root, we should return its value
  49. if (isRoot)
  50. return valueGetter(settings);
  51. else
  52. {
  53. // If the current settings object has overridden the value, return it
  54. if (overriddenGetter(settings))
  55. return valueGetter(settings);
  56. }
  57. }
  58. return default;
  59. }
  60. /// <summary>
  61. /// Gets a new instance of <see cref="PathStraighteningSettings"/> with the final values from the hierarchy
  62. /// </summary>
  63. /// <param name="settingsHierarchy">The hierarchy of settings, starting with the leaf node and ending with the root</param>
  64. /// <returns>The overridden values, or the values from the root node if no overrides were applied</returns>
  65. public static PathStraighteningSettings GetFinalValues(params PathStraighteningSettings[] settingsHierarchy)
  66. {
  67. PathStraighteningSettings finalSettings = new PathStraighteningSettings
  68. {
  69. StraightenChance = GetFinalValue(s => s.StraightenChance, s => s.OverrideStraightenChance, settingsHierarchy),
  70. CanStraightenMainPath = GetFinalValue(s => s.CanStraightenMainPath, s => s.OverrideCanStraightenMainPath, settingsHierarchy),
  71. CanStraightenBranchPaths = GetFinalValue(s => s.CanStraightenBranchPaths, s => s.OverrideCanStraightenBranchPaths, settingsHierarchy)
  72. };
  73. return finalSettings;
  74. }
  75. }
  76. }