Ejemplo n.º 1
0
        /// <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);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void CopyTilesToSharedTilemaps(GeneratedLevel level, RoomInstance roomInstance)
        {
            var destinationTilemaps = level.GetSharedTilemaps();
            var sourceTilemaps      = RoomTemplateUtils.GetTilemaps(roomInstance.RoomTemplateInstance);

            CopyTiles(sourceTilemaps, destinationTilemaps, roomInstance.Position);
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
 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());
 }
Ejemplo n.º 5
0
        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);
                }
            }
        }
Ejemplo n.º 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 <IRoomTemplateOutlineHandler>();

            if (outlineHandler != null)
            {
                var polygon2d = outlineHandler.GetRoomTemplateOutline();
                return(polygon2d?.GetGridPolygon());
            }

            var tilemaps = RoomTemplateUtils.GetTilemaps(roomTemplate);
            var outline  = RoomTemplateUtils.GetTilemapsForOutline(tilemaps);

            return(GetPolygonFromTilemaps(outline));
        }
Ejemplo n.º 7
0
        /// <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>
 /// Gets all the shared tilemaps.
 /// </summary>
 /// <returns></returns>
 public List <Tilemap> GetSharedTilemaps()
 {
     return(RoomTemplateUtils.GetTilemaps(RootGameObject));
 }