public static void CopyTilesToSharedTilemaps(GeneratedLevel level, RoomInstance roomInstance) { var destinationTilemaps = level.GetSharedTilemaps(); var sourceTilemaps = RoomTemplateUtils.GetTilemaps(roomInstance.RoomTemplateInstance); CopyTiles(sourceTilemaps, destinationTilemaps, roomInstance.Position); }
public bool HasOutlineOverride() { var tilemapsRoot = RoomTemplateUtils.GetTilemapsRoot(gameObject); var outlineOverride = tilemapsRoot.transform.Find(GeneratorConstants.OutlineOverrideLayerName); return(outlineOverride != null); }
/// <summary> /// Disables colliders of individual room template tilemaps in the generated level. /// The goal is to try to keep triggers functioning. /// </summary> /// <param name="level"></param> public static void DisableRoomTemplatesColliders(GeneratedLevel level) { // Iterate through all the rooms foreach (var roomInstance in level.GetRoomInstances()) { var roomTemplateInstance = roomInstance.RoomTemplateInstance; var tilemaps = RoomTemplateUtils.GetTilemaps(roomTemplateInstance); foreach (var tilemap in tilemaps) { // Iterate through all the colliders foreach (var collider in tilemap.GetComponents <Collider2D>()) { // If the collider is not used by composite collider and it is not a trigger, destroy it if (!collider.usedByComposite && !collider.isTrigger) { Destroy(collider); } else if (collider.usedByComposite) { // If the collider is used by composite but that composite does not exist or is not a trigger, destroy it if (collider.composite == null || !collider.composite.isTrigger) { Destroy(collider); } } } } } }
public static void CopyTiles(List <Tilemap> sourceTilemaps, List <Tilemap> destinationTilemaps, Vector3Int offset) { sourceTilemaps = RoomTemplateUtils.GetTilemapsForCopying(sourceTilemaps); DeleteNonNullTiles(sourceTilemaps, destinationTilemaps, offset); foreach (var sourceTilemap in sourceTilemaps) { var destinationTilemap = destinationTilemaps.FirstOrDefault(x => x.name == sourceTilemap.name); if (destinationTilemap == null) { continue; } foreach (var tilemapPosition in sourceTilemap.cellBounds.allPositionsWithin) { var tile = sourceTilemap.GetTile(tilemapPosition); if (tile != null) { destinationTilemap.SetTile(tilemapPosition + offset, tile); destinationTilemap.SetTransformMatrix(tilemapPosition + offset, sourceTilemap.GetTransformMatrix(tilemapPosition)); } } } }
public Polygon2D GetRoomTemplateOutline() { var tilemaps = RoomTemplateUtils.GetTilemaps(gameObject); var outlineTilemaps = RoomTemplateUtils.GetTilemapsForOutline(tilemaps); var usedTiles = RoomTemplateUtils.GetUsedTiles(outlineTilemaps); if (usedTiles.Count == 0) { return(null); } var minX = usedTiles.Min(x => x.x); var maxX = usedTiles.Max(x => x.x); var minY = usedTiles.Min(x => x.y); var maxY = usedTiles.Max(x => x.y) + PaddingTop; var polygonPoints = new List <Vector2Int>() { new Vector2Int(minX, minY), new Vector2Int(minX, maxY), new Vector2Int(maxX, maxY), new Vector2Int(maxX, minY), }; return(new Polygon2D(polygonPoints)); }
private static List <Tilemap> GetTilemaps(GameObject gameObject, Predicate <IgnoreTilemap> excludePredicate) { return(RoomTemplateUtils .GetTilemaps(gameObject) .Where(tilemap => { var ignoreTilemap = tilemap.GetComponent <IgnoreTilemap>(); return ignoreTilemap == null || !excludePredicate(ignoreTilemap); }) .ToList()); }
public void RemoveOutlineOverride() { if (!HasOutlineOverride()) { return; } var tilemapsRoot = RoomTemplateUtils.GetTilemapsRoot(gameObject); var outlineOverride = tilemapsRoot.transform.Find(GeneratorConstants.OutlineOverrideLayerName).gameObject; PostProcessUtils.Destroy(outlineOverride); }
public static void InitializeSharedTilemaps(GeneratedLevel level, TilemapLayersStructureMode mode, ITilemapLayersHandler defaultTilemapLayersHandler, ITilemapLayersHandler customTilemapLayersHandler, GameObject example, Material tilemapMaterial) { GameObject tilemapsRoot; if (/*mode == TilemapLayersStructureMode.Automatic || */ mode == TilemapLayersStructureMode.FromExample) { if (mode == TilemapLayersStructureMode.FromExample && example == null) { throw new ConfigurationException($"When {nameof(PostProcessConfig.TilemapLayersStructure)} is set to {nameof(TilemapLayersStructureMode.FromExample)}, {nameof(PostProcessConfig.TilemapLayersExample)} must not be null. Please set the field in the Dungeon Generator component."); } //var tilemapsSource = mode == TilemapLayersStructureMode.Automatic // ? level.GetRoomInstances().First().RoomTemplateInstance // : example; var tilemapsSource = example; var tilemapsSourceRoot = RoomTemplateUtils.GetTilemapsRoot(tilemapsSource); if (mode == TilemapLayersStructureMode.FromExample && tilemapsSourceRoot == tilemapsSource) { throw new ConfigurationException($"Given {nameof(PostProcessConfig.TilemapLayersExample)} is not valid as it does not contain a game object called {GeneratorConstants.TilemapsRootName} that holds individual tilemap layers."); } tilemapsRoot = Object.Instantiate(tilemapsSourceRoot, level.RootGameObject.transform); tilemapsRoot.name = GeneratorConstants.TilemapsRootName; foreach (var tilemap in tilemapsRoot.GetComponentsInChildren <Tilemap>()) { tilemap.ClearAllTiles(); } } else { // Initialize GameObject that will hold tilemaps tilemapsRoot = new GameObject(GeneratorConstants.TilemapsRootName); tilemapsRoot.transform.parent = level.RootGameObject.transform; if (mode == TilemapLayersStructureMode.Default) { defaultTilemapLayersHandler.InitializeTilemaps(tilemapsRoot); } else if (mode == TilemapLayersStructureMode.Custom) { if (customTilemapLayersHandler == null) { throw new ConfigurationException($"When {nameof(PostProcessConfig.TilemapLayersStructure)} is set to {nameof(TilemapLayersStructureMode.Custom)}, {nameof(PostProcessConfig.TilemapLayersHandler)} must not be null. Please set the field in the Dungeon Generator component."); } customTilemapLayersHandler.InitializeTilemaps(tilemapsRoot); } } }
public static void DisableRoomTemplatesRenderers(GeneratedLevel level) { foreach (var roomInstance in level.GetRoomInstances()) { var roomTemplateInstance = roomInstance.RoomTemplateInstance; var tilemaps = RoomTemplateUtils.GetTilemaps(roomTemplateInstance); foreach (var tilemap in tilemaps) { var tilemapRenderer = tilemap.GetComponent <TilemapRenderer>(); Destroy(tilemapRenderer); } } }
public static void SetTilemapsMaterial(GeneratedLevel level, Material tilemapMaterial) { if (tilemapMaterial == null) { return; } var tilemapsRoot = RoomTemplateUtils.GetTilemapsRoot(level.RootGameObject); foreach (var tilemapRenderer in tilemapsRoot.GetComponentsInChildren <TilemapRenderer>()) { tilemapRenderer.material = tilemapMaterial; } }
/// <summary> /// Computes a polygon from points on given tilemaps. /// </summary> /// <param name="roomTemplate"></param> /// <returns></returns> public static PolygonGrid2D GetPolygonFromRoomTemplate(GameObject roomTemplate) { var outlineHandler = roomTemplate.GetComponent <IRoomTemplateOutlineHandler>(); if (outlineHandler != null) { var polygon2d = outlineHandler.GetRoomTemplateOutline(); return(polygon2d?.GetGridPolygon()); } var tilemaps = RoomTemplateUtils.GetTilemaps(roomTemplate); var outline = RoomTemplateUtils.GetTilemapsForOutline(tilemaps); return(GetPolygonFromTilemaps(outline)); }
public void AddOutlineOverride() { if (HasOutlineOverride()) { return; } var tilemapsRoot = RoomTemplateUtils.GetTilemapsRoot(gameObject); var outlineOverride = new GameObject(GeneratorConstants.OutlineOverrideLayerName); outlineOverride.transform.parent = tilemapsRoot.transform; outlineOverride.AddComponent <Tilemap>(); outlineOverride.AddComponent <TilemapRenderer>(); outlineOverride.AddComponent <OutlineOverride>(); outlineOverride.GetComponent <TilemapRenderer>().sortingOrder = 1000; }
/// <summary> /// Copies tiles from a given room template to given destination tilemaps. /// </summary> /// <remarks> /// One important aspect of this method is how to handle already existing tiles in destination tilemaps. /// /// When deleteNonNullTiles is true, it computes all non-null positions across all layers in the room template. /// After that, it deletes all tiles on these positions in destination tilemaps. /// /// When deleteTilesInsideOutline is true, it computes all tiles inside the outline of the room template and /// deletes them from the destination tilemaps. /// So even if there is a hole inside the room template, the position is still removed. /// /// deleteNonNullTiles and deleteTilesInsideOutline can be combined together. /// </remarks> /// <param name="roomInstance">Room instance to be copied to the destination tilemaps.</param> /// <param name="destinationTilemaps">List of destination tilemaps.</param> /// <param name="deleteNonNullTiles">Whether to delete non-null tiles from destination tilemaps.</param> /// <param name="deleteTilesInsideOutline">Whether to delete all tiles insides the outline from destination tilemaps.</param> public static void CopyTiles(RoomInstance roomInstance, List <Tilemap> destinationTilemaps, bool deleteNonNullTiles, bool deleteTilesInsideOutline) { var sourceTilemaps = RoomTemplateUtils.GetTilemaps(roomInstance.RoomTemplateInstance); sourceTilemaps = RoomTemplateUtils.GetTilemapsForCopying(sourceTilemaps); var tilesToRemove = new List <Vector3Int>(); if (deleteNonNullTiles) { var tiles = GetNonNullTiles(sourceTilemaps); tilesToRemove.AddRange(tiles.Select(x => x + roomInstance.Position)); } if (deleteTilesInsideOutline) { var tiles = GetTilesInsideOutline(roomInstance, false); tilesToRemove.AddRange(tiles); } RemoveTiles(destinationTilemaps, tilesToRemove); foreach (var sourceTilemap in sourceTilemaps) { var destinationTilemap = destinationTilemaps.FirstOrDefault(x => x.name == sourceTilemap.name); if (destinationTilemap == null) { continue; } foreach (var tilemapPosition in sourceTilemap.cellBounds.allPositionsWithin) { var tile = sourceTilemap.GetTile(tilemapPosition); if (tile != null) { destinationTilemap.SetTile(tilemapPosition + roomInstance.Position, tile); destinationTilemap.SetTransformMatrix(tilemapPosition + roomInstance.Position, sourceTilemap.GetTransformMatrix(tilemapPosition)); } } } }
/// <summary> /// Computes a polygon from points on given tilemaps. /// </summary> /// <param name="tilemaps"></param> /// <returns></returns> public static PolygonGrid2D GetPolygonFromTilemaps(ICollection <Tilemap> tilemaps) { var usedTiles = GetUsedTiles(RoomTemplateUtils.GetTilemapsForOutline(tilemaps)); return(GetPolygonFromTiles(usedTiles)); }
/// <summary> /// Gets all the shared tilemaps. /// </summary> /// <returns></returns> public List <Tilemap> GetSharedTilemaps() { return(RoomTemplateUtils.GetTilemaps(RootGameObject)); }