DungeonAttachmentSettings.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. namespace DunGen
  4. {
  5. public class DungeonAttachmentSettings
  6. {
  7. /// <summary>
  8. /// The doorway to attach the dungeon to. If set, the new dungeon must be attached to this doorway
  9. /// </summary>
  10. public Doorway AttachmentDoorway { get; private set; }
  11. /// <summary>
  12. /// The tile to attach the dungeon to. If set, the new dungeon will attach to this tile, but the doorway
  13. /// will be chosen randomly
  14. /// </summary>
  15. public Tile AttachmentTile { get; private set; }
  16. public TileProxy TileProxy { get; private set; }
  17. public DungeonAttachmentSettings(Doorway attachmentDoorway)
  18. {
  19. Assert.IsNotNull(attachmentDoorway, "attachmentDoorway cannot be null");
  20. AttachmentDoorway = attachmentDoorway;
  21. }
  22. public DungeonAttachmentSettings(Tile attachmentTile)
  23. {
  24. Assert.IsNotNull(attachmentTile, "attachmentTile cannot be null");
  25. AttachmentTile = attachmentTile;
  26. }
  27. public TileProxy GenerateAttachmentProxy(Vector3 upVector, RandomStream randomStream)
  28. {
  29. if (AttachmentTile != null)
  30. {
  31. // This tile wasn't placed by DunGen so we'll need to do
  32. // some extra setup to ensure we have all the data we'll need later
  33. if (AttachmentTile.Prefab == null)
  34. PrepareManuallyPlacedTile(AttachmentTile, upVector, randomStream);
  35. TileProxy = new TileProxy(AttachmentTile.Prefab,
  36. (doorway, index) => AttachmentTile.UnusedDoorways.Contains(AttachmentTile.AllDoorways[index])); // Ensure chosen doorway is unused
  37. TileProxy.Placement.Position = AttachmentTile.transform.localPosition;
  38. TileProxy.Placement.Rotation = AttachmentTile.transform.localRotation;
  39. }
  40. else if (AttachmentDoorway != null)
  41. {
  42. var attachmentTile = AttachmentDoorway.Tile;
  43. if(attachmentTile == null)
  44. {
  45. attachmentTile = AttachmentDoorway.GetComponentInParent<Tile>();
  46. if(attachmentTile == null)
  47. {
  48. Debug.LogError($"Cannot attach to a doorway that doesn't belong to a Tile. Ensure the Doorway is parented to a GameObject with a Tile component");
  49. return null;
  50. }
  51. }
  52. if(attachmentTile.Prefab == null)
  53. PrepareManuallyPlacedTile(attachmentTile, upVector, randomStream);
  54. if (AttachmentDoorway.Tile.UsedDoorways.Contains(AttachmentDoorway))
  55. Debug.LogError($"Cannot attach dungeon to doorway '{AttachmentDoorway.name}' as it is already in use");
  56. TileProxy = new TileProxy(AttachmentDoorway.Tile.Prefab,
  57. (doorway, index) => index == attachmentTile.AllDoorways.IndexOf(AttachmentDoorway));
  58. TileProxy.Placement.Position = AttachmentDoorway.Tile.transform.localPosition;
  59. TileProxy.Placement.Rotation = AttachmentDoorway.Tile.transform.localRotation;
  60. }
  61. return TileProxy;
  62. }
  63. private void PrepareManuallyPlacedTile(Tile tileToPrepare, Vector3 upVector, RandomStream randomStream)
  64. {
  65. tileToPrepare.Prefab = tileToPrepare.gameObject;
  66. foreach (var doorway in tileToPrepare.GetComponentsInChildren<Doorway>())
  67. {
  68. doorway.Tile = tileToPrepare;
  69. tileToPrepare.AllDoorways.Add(doorway);
  70. tileToPrepare.UnusedDoorways.Add(doorway);
  71. doorway.ProcessDoorwayObjects(false, randomStream);
  72. }
  73. Bounds bounds;
  74. if (tileToPrepare.OverrideAutomaticTileBounds)
  75. bounds = tileToPrepare.TileBoundsOverride;
  76. else
  77. bounds = UnityUtil.CalculateProxyBounds(tileToPrepare.gameObject, upVector);
  78. tileToPrepare.Placement.LocalBounds = UnityUtil.CondenseBounds(bounds, tileToPrepare.AllDoorways);
  79. }
  80. public Tile GetAttachmentTile()
  81. {
  82. Tile attachmentTile = null;
  83. if (AttachmentTile != null)
  84. attachmentTile = AttachmentTile;
  85. else if (AttachmentDoorway != null)
  86. attachmentTile = AttachmentDoorway.Tile;
  87. return attachmentTile;
  88. }
  89. }
  90. }