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));
        }
        public bool HasOutlineOverride()
        {
            var tilemapsRoot    = RoomTemplateUtilsGrid2D.GetTilemapsRoot(gameObject);
            var outlineOverride = tilemapsRoot.transform.Find(GeneratorConstantsGrid2D.OutlineOverrideLayerName);

            return(outlineOverride != null);
        }
 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());
 }
        public void RemoveOutlineOverride()
        {
            if (!HasOutlineOverride())
            {
                return;
            }

            var tilemapsRoot    = RoomTemplateUtilsGrid2D.GetTilemapsRoot(gameObject);
            var outlineOverride = tilemapsRoot.transform.Find(GeneratorConstantsGrid2D.OutlineOverrideLayerName).gameObject;

            PostProcessUtilsGrid2D.Destroy(outlineOverride);
        }
        /// <summary>
        /// Sets a given material to all shared tilemap layers.
        /// </summary>
        /// <param name="level"></param>
        /// <param name="tilemapMaterial"></param>
        public static void SetTilemapsMaterial(GeneratedLevel level, Material tilemapMaterial)
        {
            if (tilemapMaterial == null)
            {
                return;
            }

            var tilemapsRoot = RoomTemplateUtilsGrid2D.GetTilemapsRoot(level.RootGameObject);

            foreach (var tilemapRenderer in tilemapsRoot.GetComponentsInChildren <TilemapRenderer>())
            {
                tilemapRenderer.material = tilemapMaterial;
            }
        }
Example #6
0
        /// <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>
        /// Initializes shared tilemaps of a given level.
        /// </summary>
        /// <param name="level">Generated level.</param>
        /// <param name="mode">Tilemap layers mode.</param>
        /// <param name="defaultTilemapLayersHandler">Default tilemap layers handler. Used for the Default mode.</param>
        /// <param name="customTilemapLayersHandler">Custom tilemap layers handler. Used for the Custom mode.</param>
        /// <param name="example">Example game object for tilemaps structure. Used for the FromExample mode.</param>
        public static void InitializeSharedTilemaps(GeneratedLevel level, TilemapLayersStructureModeGrid2D mode, ITilemapLayersHandlerGrid2D defaultTilemapLayersHandler, ITilemapLayersHandlerGrid2D customTilemapLayersHandler, GameObject example)
        {
            GameObject tilemapsRoot;

            if (mode == TilemapLayersStructureModeGrid2D.FromExample)
            {
                if (example == null)
                {
                    throw new ConfigurationException($"When {nameof(PostProcessingConfigGrid2D.TilemapLayersStructure)} is set to {nameof(TilemapLayersStructureModeGrid2D.FromExample)}, {nameof(PostProcessingConfigGrid2D.TilemapLayersExample)} must not be null. Please set the field in the Dungeon Generator component.");
                }

                var tilemapsSource     = example;
                var tilemapsSourceRoot = RoomTemplateUtilsGrid2D.GetTilemapsRoot(tilemapsSource);

                if (tilemapsSourceRoot == tilemapsSource)
                {
                    throw new ConfigurationException($"Given {nameof(PostProcessingConfigGrid2D.TilemapLayersExample)} is not valid as it does not contain a game object called {GeneratorConstantsGrid2D.TilemapsRootName} that holds individual tilemap layers.");
                }

                tilemapsRoot      = Object.Instantiate(tilemapsSourceRoot, level.RootGameObject.transform);
                tilemapsRoot.name = GeneratorConstantsGrid2D.TilemapsRootName;

                foreach (var tilemap in tilemapsRoot.GetComponentsInChildren <Tilemap>())
                {
                    tilemap.ClearAllTiles();
                }
            }
            else
            {
                // Initialize GameObject that will hold tilemaps
                tilemapsRoot = new GameObject(GeneratorConstantsGrid2D.TilemapsRootName);
                tilemapsRoot.transform.parent = level.RootGameObject.transform;

                if (mode == TilemapLayersStructureModeGrid2D.Default)
                {
                    defaultTilemapLayersHandler.InitializeTilemaps(tilemapsRoot);
                }
                else if (mode == TilemapLayersStructureModeGrid2D.Custom)
                {
                    if (customTilemapLayersHandler == null)
                    {
                        throw new ConfigurationException($"When {nameof(PostProcessingConfigGrid2D.TilemapLayersStructure)} is set to {nameof(TilemapLayersStructureModeGrid2D.Custom)}, {nameof(PostProcessingConfigGrid2D.TilemapLayersHandler)} must not be null. Please set the field in the Dungeon Generator component.");
                    }

                    customTilemapLayersHandler.InitializeTilemaps(tilemapsRoot);
                }
            }
        }
        public void AddOutlineOverride()
        {
            if (HasOutlineOverride())
            {
                return;
            }

            var tilemapsRoot    = RoomTemplateUtilsGrid2D.GetTilemapsRoot(gameObject);
            var outlineOverride = new GameObject(GeneratorConstantsGrid2D.OutlineOverrideLayerName);

            outlineOverride.transform.parent = tilemapsRoot.transform;
            outlineOverride.AddComponent <Tilemap>();
            outlineOverride.AddComponent <TilemapRenderer>();
            outlineOverride.AddComponent <OutlineOverrideGrid2D>();
            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 = 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 tiles that are not null in given tilemaps.
 /// </summary>
 /// <param name="tilemaps"></param>
 /// <returns></returns>
 public static HashSet <Vector2Int> GetUsedTiles(IEnumerable <Tilemap> tilemaps)
 {
     return(RoomTemplateUtilsGrid2D.GetUsedTiles(tilemaps));
 }
 /// <summary>
 /// Gets tilemaps that should be used when computing room template outline.
 /// </summary>
 /// <param name="tilemaps"></param>
 /// <returns></returns>
 public static List <Tilemap> GetTilemapsForOutline(ICollection <Tilemap> tilemaps)
 {
     return(RoomTemplateUtilsGrid2D.GetTilemapsForOutline(tilemaps));
 }
 /// <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 the GameObject that is the parent to all the tilemaps.
 /// </summary>
 /// <remarks>
 /// If there is no child named GeneratorConstants.TilemapsRootName, the room template GameObject
 /// itself is returned to provide backwards compatibility.
 /// </remarks>
 /// <param name="roomTemplate">GameObject representing the room template.</param>
 /// <returns></returns>
 public static GameObject GetTilemapsRoot(GameObject roomTemplate)
 {
     return(RoomTemplateUtilsGrid2D.GetTilemapsRoot(roomTemplate));
 }
Example #14
0
        /// <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(RoomTemplateUtilsGrid2D.GetTilemapsForOutline(tilemaps));

            return(GetPolygonFromTiles(usedTiles));
        }
 /// <summary>
 /// Gets all the shared tilemaps.
 /// </summary>
 /// <returns></returns>
 public List <Tilemap> GetSharedTilemaps()
 {
     return(RoomTemplateUtilsGrid2D.GetTilemaps(RootGameObject));
 }