using DunGen.Graph; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace DunGen { public static class DungeonUtil { /// /// Adds a Door component to the selected doorPrefab if one doesn't already exist /// /// The dungeon that this door belongs to /// The door prefab on which to apply the component /// The doorway that this door belongs to public static void AddAndSetupDoorComponent(Dungeon dungeon, GameObject doorPrefab, Doorway doorway) { var door = doorPrefab.GetComponent(); if (door == null) door = doorPrefab.AddComponent(); door.Dungeon = dungeon; door.DoorwayA = doorway; door.DoorwayB = doorway.ConnectedDoorway; door.TileA = doorway.Tile; door.TileB = doorway.ConnectedDoorway.Tile; dungeon.AddAdditionalDoor(door); } public static bool HasAnyViableEntries(this List weights) { if (weights == null || weights.Count == 0) return false; foreach (var entry in weights) if (entry.GameObject != null && entry.Weight > 0f) return true; return false; } public static GameObject GetRandom(this List weights, RandomStream randomStream) { float totalWeight = 0f; foreach (var entry in weights) if (entry.GameObject != null) totalWeight += entry.Weight; float randomNumber = (float)(randomStream.NextDouble() * totalWeight); foreach (var entry in weights) { if (entry == null || entry.GameObject == null) continue; if (randomNumber < entry.Weight) return entry.GameObject; randomNumber -= entry.Weight; } return null; } } }