public Polygon2D GetRoomTemplateOutline() { var tilemaps = RoomTemplateUtilsGrid2D.GetTilemaps(gameObject); var outlineTilemaps = RoomTemplateUtilsGrid2D.GetTilemapsForOutline(tilemaps); var usedTiles = RoomTemplateUtilsGrid2D.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)); }
internal static List <Tilemap> GetTilemaps(GameObject gameObject, Predicate <IgnoreTilemap> excludePredicate) { return(RoomTemplateUtilsGrid2D .GetTilemaps(gameObject) .Where(tilemap => { var ignoreTilemap = tilemap.GetComponent <IgnoreTilemap>(); return ignoreTilemap == null || !excludePredicate(ignoreTilemap); }) .ToList()); }
/// <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 <IRoomTemplateOutlineHandlerGrid2D>(); if (outlineHandler != null) { var polygon2d = outlineHandler.GetRoomTemplateOutline(); return(polygon2d?.GetGridPolygon()); } var tilemaps = RoomTemplateUtilsGrid2D.GetTilemaps(roomTemplate); var outline = RoomTemplateUtilsGrid2D.GetTilemapsForOutline(tilemaps); return(GetPolygonFromTilemaps(outline)); }
/// <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 = RoomTemplateUtilsGrid2D.GetTilemaps(roomInstance.RoomTemplateInstance); sourceTilemaps = RoomTemplateUtilsGrid2D.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> /// Gets all the tilemap layers from the room template GameObject. /// </summary> /// <remarks> /// Only tilemaps that are direct children of the tilemap root GameObject are returned. /// That means that if there is a tilemap that is not a direct child of the tilemaps root /// (e.g. it is part of a prefab that should be instantiated alongside the room template), /// it is not returned here and is not used to compute room template outlines. /// </remarks> /// <param name="roomTemplate">GameObject representing the room template.</param> /// <param name="includeInactive">Whether inactive tilemaps should be returned.</param> /// <returns></returns> public static List <Tilemap> GetTilemaps(GameObject roomTemplate, bool includeInactive = true) { return(RoomTemplateUtilsGrid2D.GetTilemaps(roomTemplate, includeInactive)); }
/// <summary> /// Gets all the shared tilemaps. /// </summary> /// <returns></returns> public List <Tilemap> GetSharedTilemaps() { return(RoomTemplateUtilsGrid2D.GetTilemaps(RootGameObject)); }