Exemple #1
0
        /// <summary>
        /// Returns a random position on the ground floor of the grid
        /// </summary>
        /// <param name="targetTilemap"></param>
        /// <param name="grid"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="startingHeight"></param>
        /// <param name="xMin"></param>
        /// <param name="xMax"></param>
        /// <param name="shouldBeFilled"></param>
        /// <param name="maxIterations"></param>
        /// <returns></returns>
        public static Vector2 GetRandomPositionOnGround(Tilemap targetTilemap, Grid grid, int width, int height, int startingHeight, int xMin, int xMax, bool shouldBeFilled = true, int maxIterations = 1000)
        {
            int        iterationsCount  = 0;
            Vector3Int randomCoordinate = Vector3Int.zero;

            while (iterationsCount < maxIterations)
            {
                randomCoordinate.x = UnityEngine.Random.Range(xMin, xMax);
                randomCoordinate.y = startingHeight;
                randomCoordinate  += MMTilemapGridRenderer.ComputeOffset(width - 1, height - 1);

                int counter = height;

                while (counter > 0)
                {
                    bool hasTile = targetTilemap.HasTile(randomCoordinate);
                    if (hasTile == shouldBeFilled)
                    {
                        randomCoordinate.y++;
                        return(targetTilemap.CellToWorld(randomCoordinate) + (grid.cellSize / 2));
                    }

                    randomCoordinate.y--;
                    counter--;
                }

                iterationsCount++;
            }

            return(Vector2.zero);
        }
Exemple #2
0
        /// <summary>
        /// Converts a tilemap's contents into a grid
        /// </summary>
        /// <param name="tilemap"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static int[,] TilemapToGrid(Tilemap tilemap, int width, int height)
        {
            int[,] grid = new int[width, height];
            Vector3Int currentPosition = Vector3Int.zero;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    currentPosition.x = i;
                    currentPosition.y = j;
                    currentPosition  += MMTilemapGridRenderer.ComputeOffset(width - 1, height - 1);

                    grid[i, j] = (tilemap.GetTile(currentPosition) == null) ? 0 : 1;
                }
            }
            return(grid);
        }
Exemple #3
0
        /// <summary>
        /// Returns a random world position on the specified tilemap/grid combo, filled or not based on the shouldBeFilled flag
        /// </summary>
        /// <param name="targetTilemap"></param>
        /// <param name="grid"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="shouldBeFilled"></param>
        /// <param name="maxIterations"></param>
        /// <returns></returns>
        public static Vector2 GetRandomPosition(Tilemap targetTilemap, Grid grid, int width, int height, bool shouldBeFilled = true, int maxIterations = 1000)
        {
            int        iterationsCount  = 0;
            Vector3Int randomCoordinate = Vector3Int.zero;

            while (iterationsCount < maxIterations)
            {
                randomCoordinate.x = UnityEngine.Random.Range(0, width);
                randomCoordinate.y = UnityEngine.Random.Range(0, height);
                randomCoordinate  += MMTilemapGridRenderer.ComputeOffset(width - 1, height - 1);

                bool hasTile = targetTilemap.HasTile(randomCoordinate);
                if (hasTile == shouldBeFilled)
                {
                    return(targetTilemap.CellToWorld(randomCoordinate) + (grid.cellSize / 2));
                }

                iterationsCount++;
            }

            return(Vector2.zero);
        }
        /// <summary>
        /// Converts a tilemap's contents into a grid
        /// </summary>
        /// <param name="tilemap"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static int[,] TilemapToGrid(Tilemap tilemap, int width, int height)
        {
            if (tilemap == null)
            {
                Debug.LogError("[MMGridGenerator] You're trying to convert a tilemap into a grid but didn't specify what tilemap to convert.");
                return(null);
            }

            int[,] grid = new int[width, height];
            Vector3Int currentPosition = Vector3Int.zero;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    currentPosition.x = i;
                    currentPosition.y = j;
                    currentPosition  += MMTilemapGridRenderer.ComputeOffset(width - 1, height - 1);

                    grid[i, j] = (tilemap.GetTile(currentPosition) == null) ? 0 : 1;
                }
            }
            return(grid);
        }
Exemple #5
0
 /// <summary>
 /// Renders the grid with the selected modes to the specified target tilemap
 /// </summary>
 /// <param name="layer"></param>
 protected virtual void RenderGrid(MMTilemapGeneratorLayer layer)
 {
     MMTilemapGridRenderer.RenderGrid(_grid, layer, SlowRender, SlowRenderDuration, SlowRenderTweenType, this);
 }