TilePlacementData.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using DunGen.Graph;
  2. using System;
  3. using UnityEngine;
  4. namespace DunGen
  5. {
  6. /// <summary>
  7. /// A container for all of the information about a tile's posoitioning in the generated dungeon
  8. /// </summary>
  9. [Serializable]
  10. public sealed class TilePlacementData
  11. {
  12. /// <summary>
  13. /// Gets the depth of this tile in the dungeon along the main path
  14. /// </summary>
  15. public int PathDepth
  16. {
  17. get { return pathDepth; }
  18. internal set { pathDepth = value; }
  19. }
  20. /// <summary>
  21. /// Gets the normalized depth (0.0-1.0) of this tile in the dungeon along the main path
  22. /// </summary>
  23. public float NormalizedPathDepth
  24. {
  25. get { return normalizedPathDepth; }
  26. internal set { normalizedPathDepth = value; }
  27. }
  28. /// <summary>
  29. /// Gets the depth of this tile in the dungeon along the branch it's on
  30. /// </summary>
  31. public int BranchDepth
  32. {
  33. get { return branchDepth; }
  34. internal set { branchDepth = value; }
  35. }
  36. /// <summary>
  37. /// Gets the normalized depth (0.0-1.0) of this tile in the dungeon along the branch it's on
  38. /// </summary>
  39. public float NormalizedBranchDepth
  40. {
  41. get { return normalizedBranchDepth; }
  42. internal set { normalizedBranchDepth = value; }
  43. }
  44. /// <summary>
  45. /// An ID representing which branch the tile belongs to. All tiles on the same branch will share an ID.
  46. /// Will be -1 for tiles not on a branch
  47. /// </summary>
  48. public int BranchId
  49. {
  50. get { return branchId; }
  51. internal set { branchId = value; }
  52. }
  53. /// <summary>
  54. /// Whether or not this tile lies on the dungeon's main path
  55. /// </summary>
  56. public bool IsOnMainPath
  57. {
  58. get { return isOnMainPath; }
  59. internal set { isOnMainPath = value; }
  60. }
  61. /// <summary>
  62. /// The boundaries of this tile
  63. /// </summary>
  64. public Bounds Bounds
  65. {
  66. get => worldBounds;
  67. private set => worldBounds = value;
  68. }
  69. /// <summary>
  70. /// The local boundaries of this tile
  71. /// </summary>
  72. public Bounds LocalBounds
  73. {
  74. get { return localBounds; }
  75. internal set
  76. {
  77. localBounds = value;
  78. RecalculateTransform();
  79. }
  80. }
  81. public TilePlacementParameters PlacementParameters
  82. {
  83. get { return placementParameters; }
  84. internal set { placementParameters = value; }
  85. }
  86. public GraphNode GraphNode => placementParameters.Node;
  87. public GraphLine GraphLine => placementParameters.Line;
  88. public DungeonArchetype Archetype => placementParameters.Archetype;
  89. public TileSet TileSet
  90. {
  91. get { return tileSet; }
  92. internal set { tileSet = value; }
  93. }
  94. public Vector3 Position
  95. {
  96. get { return position; }
  97. set
  98. {
  99. position = value;
  100. RecalculateTransform();
  101. }
  102. }
  103. public Quaternion Rotation
  104. {
  105. get { return rotation; }
  106. set
  107. {
  108. rotation = value;
  109. RecalculateTransform();
  110. }
  111. }
  112. public Matrix4x4 Transform { get; private set; }
  113. /// <summary>
  114. /// Gets the depth of this tile. Returns the branch depth if on a branch path, otherwise, returns the main path depth
  115. /// </summary>
  116. public int Depth { get { return (isOnMainPath) ? pathDepth : branchDepth; } }
  117. /// <summary>
  118. /// Gets the normalized depth (0-1) of this tile. Returns the branch depth if on a branch path, otherwise, returns the main path depth
  119. /// </summary>
  120. public float NormalizedDepth { get { return (isOnMainPath) ? normalizedPathDepth : normalizedBranchDepth; } }
  121. /// <summary>
  122. /// Data about how this tile was injected, or null if it was not placed using tile injection
  123. /// </summary>
  124. public InjectedTile InjectionData { get; set; }
  125. [SerializeField]
  126. private int pathDepth;
  127. [SerializeField]
  128. private float normalizedPathDepth;
  129. [SerializeField]
  130. private int branchDepth;
  131. [SerializeField]
  132. private float normalizedBranchDepth;
  133. [SerializeField]
  134. private int branchId = -1;
  135. [SerializeField]
  136. private bool isOnMainPath;
  137. [SerializeField]
  138. private Bounds localBounds;
  139. [SerializeField]
  140. private Bounds worldBounds;
  141. [SerializeField]
  142. private TilePlacementParameters placementParameters;
  143. [SerializeField]
  144. private TileSet tileSet;
  145. [SerializeField]
  146. private Vector3 position = Vector3.zero;
  147. [SerializeField]
  148. private Quaternion rotation = Quaternion.identity;
  149. public TilePlacementData()
  150. {
  151. RecalculateTransform();
  152. }
  153. public TilePlacementData(TilePlacementData copy)
  154. {
  155. PathDepth = copy.PathDepth;
  156. NormalizedPathDepth = copy.NormalizedPathDepth;
  157. BranchDepth = copy.BranchDepth;
  158. NormalizedBranchDepth = copy.NormalizedDepth;
  159. BranchId = copy.BranchId;
  160. IsOnMainPath = copy.IsOnMainPath;
  161. LocalBounds = copy.LocalBounds;
  162. Transform = copy.Transform;
  163. PlacementParameters = copy.PlacementParameters;
  164. TileSet = copy.TileSet;
  165. InjectionData = copy.InjectionData;
  166. position = copy.position;
  167. rotation = copy.rotation;
  168. RecalculateTransform();
  169. }
  170. public void SetPositionAndRotation(Vector3 position, Quaternion rotation)
  171. {
  172. this.position = position;
  173. this.rotation = rotation;
  174. RecalculateTransform();
  175. }
  176. private void RecalculateTransform()
  177. {
  178. Transform = Matrix4x4.TRS(position, rotation, Vector3.one);
  179. Vector3 min = Transform.MultiplyPoint(localBounds.min);
  180. Vector3 max = Transform.MultiplyPoint(localBounds.max);
  181. Vector3 size = max - min;
  182. Vector3 center = min + size / 2f;
  183. size.x = Mathf.Abs(size.x);
  184. size.y = Mathf.Abs(size.y);
  185. size.z = Mathf.Abs(size.z);
  186. Bounds = new Bounds(center, size);
  187. }
  188. }
  189. }