DungeonArchetype.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace DunGen
  5. {
  6. /// <summary>
  7. /// A description of the layout of a dungeon
  8. /// </summary>
  9. [Serializable]
  10. [CreateAssetMenu(fileName = "New Archetype", menuName = "DunGen/Dungeon Archetype", order = 700)]
  11. public sealed class DungeonArchetype : ScriptableObject, ISerializationCallbackReceiver
  12. {
  13. #region Legacy Properties
  14. [Obsolete("StraightenChance is deprecated. Use StraighteningSettings instead")]
  15. public float StraightenChance = 0.0f;
  16. #endregion
  17. public static int CurrentFileVersion = 1;
  18. /// <summary>
  19. /// A collection of tile sets from which rooms will be selected to fill the dungeon
  20. /// </summary>
  21. public List<TileSet> TileSets = new List<TileSet>();
  22. /// <summary>
  23. /// A collection of tile sets that can be used at the start of branch paths
  24. /// </summary>
  25. public List<TileSet> BranchStartTileSets = new List<TileSet>();
  26. /// <summary>
  27. /// Defines how the TileSets and BranchStartTileSets are used when placing rooms at the beginning of a branch
  28. /// </summary>
  29. public BranchCapType BranchStartType = BranchCapType.InsteadOf;
  30. /// <summary>
  31. /// A collection of tile sets that can be used at the end of branch paths
  32. /// </summary>
  33. public List<TileSet> BranchCapTileSets = new List<TileSet>();
  34. /// <summary>
  35. /// Defines how the TileSets and BranchEndTileSets are used when placing rooms at the end of a branch
  36. /// </summary>
  37. public BranchCapType BranchCapType = BranchCapType.AsWellAs;
  38. /// <summary>
  39. /// The maximum depth (in tiles) that any branch in the dungeon can be
  40. /// </summary>
  41. public IntRange BranchingDepth = new IntRange(2, 4);
  42. /// <summary>
  43. /// The maximum number of branches each room can have
  44. /// </summary>
  45. public IntRange BranchCount = new IntRange(0, 2);
  46. /// <summary>
  47. /// The chance that this archetype will produce a straight section for the main path
  48. /// </summary>
  49. public PathStraighteningSettings StraighteningSettings = new PathStraighteningSettings();
  50. /// <summary>
  51. /// Should DunGen attempt to prevent this archetype from appearing more than once throughout the dungeon layout?
  52. /// </summary>
  53. public bool Unique = false;
  54. [SerializeField]
  55. private int fileVersion = 0;
  56. public bool GetHasValidBranchStartTiles()
  57. {
  58. if (BranchStartTileSets.Count == 0)
  59. return false;
  60. foreach (var tileSet in BranchStartTileSets)
  61. if (tileSet.TileWeights.Weights.Count > 0)
  62. return true;
  63. return false;
  64. }
  65. public bool GetHasValidBranchCapTiles()
  66. {
  67. if (BranchCapTileSets.Count == 0)
  68. return false;
  69. foreach (var tileSet in BranchCapTileSets)
  70. if (tileSet.TileWeights.Weights.Count > 0)
  71. return true;
  72. return false;
  73. }
  74. #region ISerializationCallbackReceiver Implementation
  75. public void OnBeforeSerialize()
  76. {
  77. }
  78. public void OnAfterDeserialize()
  79. {
  80. if (this == null)
  81. return;
  82. bool isDirty = false;
  83. // Upgrade to StraighteningSettings
  84. if (fileVersion < 1)
  85. {
  86. if (StraighteningSettings == null)
  87. StraighteningSettings = new PathStraighteningSettings();
  88. #pragma warning disable 0618
  89. if (StraightenChance > 0.0f)
  90. {
  91. StraighteningSettings.StraightenChance = Mathf.Clamp01(StraightenChance);
  92. StraighteningSettings.OverrideStraightenChance = true;
  93. }
  94. #pragma warning restore 0618
  95. fileVersion = 1;
  96. isDirty = true;
  97. }
  98. #if UNITY_EDITOR
  99. // Schedule to mark dirty & save
  100. if (isDirty)
  101. {
  102. UnityEditor.EditorApplication.delayCall += () =>
  103. {
  104. UnityEditor.EditorUtility.SetDirty(this);
  105. UnityEditor.AssetDatabase.SaveAssetIfDirty(this);
  106. };
  107. }
  108. #endif
  109. }
  110. #endregion
  111. }
  112. public enum BranchCapType : byte
  113. {
  114. InsteadOf,
  115. AsWellAs,
  116. }
  117. }