DungeonUtil.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using DunGen.Graph;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace DunGen
  6. {
  7. public static class DungeonUtil
  8. {
  9. /// <summary>
  10. /// Adds a Door component to the selected doorPrefab if one doesn't already exist
  11. /// </summary>
  12. /// <param name="dungeon">The dungeon that this door belongs to</param>
  13. /// <param name="doorPrefab">The door prefab on which to apply the component</param>
  14. /// <param name="doorway">The doorway that this door belongs to</param>
  15. public static void AddAndSetupDoorComponent(Dungeon dungeon, GameObject doorPrefab, Doorway doorway)
  16. {
  17. var door = doorPrefab.GetComponent<Door>();
  18. if (door == null)
  19. door = doorPrefab.AddComponent<Door>();
  20. door.Dungeon = dungeon;
  21. door.DoorwayA = doorway;
  22. door.DoorwayB = doorway.ConnectedDoorway;
  23. door.TileA = doorway.Tile;
  24. door.TileB = doorway.ConnectedDoorway.Tile;
  25. dungeon.AddAdditionalDoor(door);
  26. }
  27. public static bool HasAnyViableEntries(this List<GameObjectWeight> weights)
  28. {
  29. if (weights == null || weights.Count == 0)
  30. return false;
  31. foreach (var entry in weights)
  32. if (entry.GameObject != null && entry.Weight > 0f)
  33. return true;
  34. return false;
  35. }
  36. public static GameObject GetRandom(this List<GameObjectWeight> weights, RandomStream randomStream)
  37. {
  38. float totalWeight = 0f;
  39. foreach (var entry in weights)
  40. if (entry.GameObject != null)
  41. totalWeight += entry.Weight;
  42. float randomNumber = (float)(randomStream.NextDouble() * totalWeight);
  43. foreach (var entry in weights)
  44. {
  45. if (entry == null || entry.GameObject == null)
  46. continue;
  47. if (randomNumber < entry.Weight)
  48. return entry.GameObject;
  49. randomNumber -= entry.Weight;
  50. }
  51. return null;
  52. }
  53. }
  54. }