TilePlacementResult.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace DunGen
  6. {
  7. public abstract class TilePlacementResult
  8. {
  9. public abstract string DisplayName { get; }
  10. public static string ProduceReport(IEnumerable<TilePlacementResult> results, int maxRetryAttempts)
  11. {
  12. var report = new StringBuilder();
  13. report.AppendLine($"=== Failed to generate dungeon {maxRetryAttempts} times ===");
  14. report.AppendLine($"This could indicate an issue with the way your tiles are set up.");
  15. report.AppendLine($"Here is a list of all the reasons tile placement failed while trying to generate the dungeon:\n");
  16. var groupedResults = results
  17. .GroupBy(r => r.GetType())
  18. .OrderByDescending(g => g.Count());
  19. foreach (var group in groupedResults)
  20. report.AppendLine($"\t- {group.First().DisplayName} (x{group.Count()})");
  21. return report.ToString();
  22. }
  23. }
  24. public abstract class TileTemplatePlacementResult : TilePlacementResult
  25. {
  26. /// <summary>
  27. /// The template used to create a the new tile.
  28. /// </summary>
  29. public GameObject TileTemplatePrefab { get; private set; }
  30. public TileTemplatePlacementResult(TileProxy tileTemplate)
  31. {
  32. if (tileTemplate != null)
  33. TileTemplatePrefab = tileTemplate.Prefab;
  34. }
  35. }
  36. /// <summary>
  37. /// The tile placement failed because the tile was outside of the bounding box specified in the dungeon generator settings.
  38. /// </summary>
  39. public sealed class OutOfBoundsPlacementResult : TileTemplatePlacementResult
  40. {
  41. public override string DisplayName => "Out-of-Bounds";
  42. public OutOfBoundsPlacementResult(TileProxy tileTemplate)
  43. : base(tileTemplate)
  44. {
  45. }
  46. }
  47. /// <summary>
  48. /// The tile placement failed because the tile was colliding with another tile.
  49. /// </summary>
  50. public sealed class TileIsCollidingPlacementResult : TileTemplatePlacementResult
  51. {
  52. public override string DisplayName => "Collision";
  53. public TileIsCollidingPlacementResult(TileProxy tileTemplate)
  54. : base(tileTemplate)
  55. {
  56. }
  57. }
  58. /// <summary>
  59. /// The tile placement failed because there was no valid tile template provided.
  60. /// </summary>
  61. public sealed class NullTemplatePlacementResult : TilePlacementResult
  62. {
  63. public override string DisplayName => "No Template";
  64. }
  65. /// <summary>
  66. /// The tile placement was successful and a new tile was created.
  67. /// </summary>
  68. public sealed class SuccessPlacementResult : TilePlacementResult
  69. {
  70. override public string DisplayName => "Success";
  71. }
  72. /// <summary>
  73. /// The tile placement failed because there was no valid doorway pairs were found.
  74. /// </summary>
  75. public sealed class NoMatchingDoorwayPlacementResult : TilePlacementResult
  76. {
  77. public override string DisplayName => "No Doorway Pairs";
  78. /// <summary>
  79. /// The tile that the placement was attempted from.
  80. /// </summary>
  81. public GameObject FromTilePrefab { get; private set; }
  82. public NoMatchingDoorwayPlacementResult(TileProxy fromTile)
  83. {
  84. if (fromTile != null)
  85. FromTilePrefab = fromTile.Prefab;
  86. }
  87. }
  88. }