Generator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using UnityEngine;
  2. using System.Text;
  3. using System;
  4. using DunGen.Analysis;
  5. namespace DunGen.Demo
  6. {
  7. public class Generator : MonoBehaviour
  8. {
  9. public RuntimeDungeon DungeonGenerator;
  10. public Action<StringBuilder> GetAdditionalText;
  11. private StringBuilder infoText = new StringBuilder();
  12. private bool showStats = true;
  13. private float keypressDelay = 0.1f;
  14. private float timeSinceLastPress;
  15. private bool allowHold;
  16. private bool isKeyDown;
  17. private void Start()
  18. {
  19. if(DungeonGenerator == null)
  20. DungeonGenerator = GetComponentInChildren<RuntimeDungeon>();
  21. DungeonGenerator.Generator.OnGenerationStatusChanged += OnGenerationStatusChanged;
  22. GenerateRandom();
  23. }
  24. private void OnGenerationStatusChanged(DungeonGenerator generator, GenerationStatus status)
  25. {
  26. const int textPadding = 20;
  27. void AddEntry(StringBuilder stringBuilder, string title, string entry)
  28. {
  29. string spacing = new string(' ', textPadding - title.Length);
  30. stringBuilder.Append($"\n\t{title}:{spacing}\t{entry}");
  31. }
  32. infoText.Length = 0;
  33. if (status != GenerationStatus.Complete)
  34. {
  35. if (status == GenerationStatus.Failed)
  36. infoText.Append("Generation Failed");
  37. else if (status == GenerationStatus.NotStarted)
  38. {
  39. }
  40. else
  41. infoText.Append($"Generating ({status})...");
  42. return;
  43. }
  44. infoText.AppendLine("Seed: " + generator.ChosenSeed);
  45. infoText.AppendLine();
  46. infoText.Append("## TIME TAKEN ##");
  47. foreach (var step in GenerationAnalysis.MeasurableSteps)
  48. {
  49. float generationTime = generator.GenerationStats.GetGenerationStepTime(step);
  50. AddEntry(infoText, step.ToString(), $"{generationTime:0.00} ms ({generationTime / generator.GenerationStats.TotalTime:P0})");
  51. }
  52. infoText.Append("\n\t-------------------------------------------------------");
  53. AddEntry(infoText, "Total", $"{generator.GenerationStats.TotalTime:0.00} ms");
  54. infoText.AppendLine();
  55. infoText.AppendLine();
  56. infoText.AppendLine("## ROOM COUNT ##");
  57. infoText.Append($"\n\tMain Path: {generator.GenerationStats.MainPathRoomCount}");
  58. infoText.Append($"\n\tBranch Paths: {generator.GenerationStats.BranchPathRoomCount}");
  59. infoText.Append("\n\t-------------------");
  60. infoText.Append($"\n\tTotal: {generator.GenerationStats.TotalRoomCount}");
  61. infoText.AppendLine();
  62. infoText.Append($"\n\tRetry Count: {generator.GenerationStats.TotalRetries}");
  63. infoText.AppendLine();
  64. infoText.AppendLine();
  65. infoText.AppendLine("Press 'F1' to toggle this information");
  66. infoText.AppendLine("Press 'R' to generate a new layout");
  67. if(GetAdditionalText != null)
  68. GetAdditionalText(infoText);
  69. }
  70. public void GenerateRandom()
  71. {
  72. DungeonGenerator.Generate();
  73. }
  74. private void Update()
  75. {
  76. timeSinceLastPress += Time.deltaTime;
  77. if (Input.GetKeyDown(KeyCode.R))
  78. {
  79. timeSinceLastPress = 0;
  80. isKeyDown = true;
  81. GenerateRandom();
  82. }
  83. if (Input.GetKeyUp(KeyCode.R))
  84. {
  85. isKeyDown = false;
  86. allowHold = false;
  87. }
  88. if (!allowHold && isKeyDown && timeSinceLastPress >= keypressDelay)
  89. {
  90. allowHold = true;
  91. timeSinceLastPress = 0;
  92. }
  93. if (allowHold && Input.GetKey(KeyCode.R))
  94. {
  95. if (timeSinceLastPress >= keypressDelay)
  96. {
  97. GenerateRandom();
  98. timeSinceLastPress = 0;
  99. }
  100. }
  101. if (Input.GetKeyDown(KeyCode.F1))
  102. showStats = !showStats;
  103. }
  104. private void OnGUI()
  105. {
  106. if (showStats)
  107. GUILayout.Label(infoText.ToString());
  108. }
  109. }
  110. }