Example #1
0
        public void FindRooms()
        {
            var position = Vector3Int.zero;

            var bounds = tilemap.cellBounds;

            searchedTiles = new Hashtable();

            for (int y = bounds.yMin; y < bounds.yMax; y++)

            {
                position.y = y;
                for (int x = bounds.xMin; x < bounds.xMax; x++)
                {
                    position.x = x;

                    if (!searchedTiles.ContainsKey(position))
                    {
                        var currentTileType = TilemapHelper.GetTileTypeFromPosition(position, tilemap);
                        if (currentTileType == TileType.ROOM)
                        {
                            var room = FindRoomArea(position);
                            if (room.tiles.Any())
                            {
                                rooms.Add(room);
                            }
                        }
                    }
                }
            }

            Debug.Log($"Number of room areas for this tilemap: {rooms.Count}");
        }
Example #2
0
        public RoomArea FindRoomArea(Vector3Int startPosition)
        {
            Queue <Vector3Int> tilesToCheck = new Queue <Vector3Int>();

            tilesToCheck.Enqueue(startPosition);

            RoomArea room = new RoomArea();

            while (tilesToCheck.Any())
            {
                var position = tilesToCheck.Dequeue();

                if (!searchedTiles.ContainsKey(position))
                {
                    searchedTiles.Add(position, position);

                    var tileType = TilemapHelper.GetTileTypeFromPosition(position, tilemap);

                    if (tileType == TileType.ROOM)
                    {
                        // Add current tile to room
                        room.tiles.Add(new TileObject(position, tileType));

                        tilesToCheck = AddNeighborsInQueue(tilesToCheck, position);
                    }
                }
            }

            return(room);
        }
Example #3
0
        public Queue <Vector3Int> AddNeighborsInQueue(Queue <Vector3Int> tilesToCheck, Vector3Int position)
        {
            // Add up position
            var upPosition = new Vector3Int(position.x, position.y + 1, position.z);
            var upTileType = TilemapHelper.GetTileTypeFromPosition(upPosition, tilemap);

            if (!searchedTiles.ContainsKey(upPosition))
            {
                tilesToCheck.Enqueue(upPosition);
            }

            // Add right position
            var rightPosition = new Vector3Int(position.x + 1, position.y, position.z);
            var rightTileType = TilemapHelper.GetTileTypeFromPosition(rightPosition, tilemap);

            if (!searchedTiles.ContainsKey(rightPosition))
            {
                tilesToCheck.Enqueue(rightPosition);
            }

            // Add down position
            var downPosition = new Vector3Int(position.x, position.y - 1, position.z);
            var downTileType = TilemapHelper.GetTileTypeFromPosition(downPosition, tilemap);

            if (!searchedTiles.ContainsKey(downPosition))
            {
                tilesToCheck.Enqueue(downPosition);
            }

            // Add left position
            var leftPosition = new Vector3Int(position.x - 1, position.y, position.z);
            var leftTileType = TilemapHelper.GetTileTypeFromPosition(leftPosition, tilemap);

            if (!searchedTiles.ContainsKey(leftPosition))
            {
                tilesToCheck.Enqueue(leftPosition);
            }

            if (upTileType == TileType.ROOM || rightTileType == TileType.ROOM)
            {
                // Add up and right position
                var uprightPosition = new Vector3Int(position.x + 1, position.y + 1, position.z);
                if (!searchedTiles.ContainsKey(uprightPosition))
                {
                    tilesToCheck.Enqueue(uprightPosition);
                }
            }

            if (downTileType == TileType.ROOM || rightTileType == TileType.ROOM)
            {
                // Add position down and right
                var downrightPosition = new Vector3Int(position.x + 1, position.y - 1, position.z);
                if (!searchedTiles.ContainsKey(downrightPosition))
                {
                    tilesToCheck.Enqueue(downrightPosition);
                }
            }

            if (downTileType == TileType.ROOM || leftTileType == TileType.ROOM)
            {
                // Add position down and left
                var downleftPosition = new Vector3Int(position.x - 1, position.y - 1, position.z);
                if (!searchedTiles.ContainsKey(downleftPosition))
                {
                    tilesToCheck.Enqueue(downleftPosition);
                }
            }

            if (upTileType == TileType.ROOM || leftTileType == TileType.ROOM)
            {
                // Add position up and left
                var upleftPosition = new Vector3Int(position.x - 1, position.y + 1, position.z);
                if (!searchedTiles.ContainsKey(upleftPosition))
                {
                    tilesToCheck.Enqueue(upleftPosition);
                }
            }

            return(tilesToCheck);
        }