/// <summary>
        /// Renders the specified grid on the specified tilemap, with optional slow mode (only works at runtime)
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="tilemap"></param>
        /// <param name="tile"></param>
        /// <param name="slowRender"></param>
        /// <param name="slowRenderDuration"></param>
        /// <param name="slowRenderTweenType"></param>
        /// <param name="slowRenderSupport"></param>
        public static void RenderGrid(int[,] grid, MMTilemapGeneratorLayer layer, bool slowRender = false, float slowRenderDuration = 1f,
                                      MMTweenType slowRenderTweenType = null, MonoBehaviour slowRenderSupport = null)
        {
            if (layer.FusionMode == MMTilemapGeneratorLayer.FusionModes.Normal)
            {
                ClearTilemap(layer.TargetTilemap);
            }
            TileBase tile = layer.Tile;

            if (layer.FusionMode == MMTilemapGeneratorLayer.FusionModes.Combine)
            {
                grid = MMGridGenerator.InvertGrid(grid);
                tile = null;
            }
            if (layer.FusionMode == MMTilemapGeneratorLayer.FusionModes.Subtract)
            {
                grid = MMGridGenerator.InvertGrid(grid);
            }

            if (!slowRender || !Application.isPlaying)
            {
                DrawGrid(grid, layer.TargetTilemap, tile, 0, TotalFilledBlocks(grid));
            }
            else
            {
                slowRenderSupport.StartCoroutine(SlowRenderGrid(grid, layer.TargetTilemap, tile, slowRenderDuration, slowRenderTweenType, 60));
            }

            if (!Application.isPlaying && slowRender)
            {
                Debug.LogWarning("Rendering maps in SlowRender mode is only supported at runtime.");
            }
        }
Exemple #2
0
        /// <summary>
        /// Generates a layer grid, and renders it
        /// </summary>
        /// <param name="layer"></param>
        protected virtual void GenerateLayer(MMTilemapGeneratorLayer layer)
        {
            if (!layer.Active)
            {
                return;
            }

            if (layer.TargetTilemap == null)
            {
                Debug.LogError("Tilemap Generator : you need to specify a Target Tilemap to paint on.");
            }
            if (layer.Tile == null)
            {
                Debug.LogError("Tilemap Generator : you need to specify a Tile to paint with.");
            }
            if (layer.GridWidth == 0)
            {
                Debug.LogError("Tilemap Generator : grid width can't be 0.");
            }
            if (layer.GridHeight == 0)
            {
                Debug.LogError("Tilemap Generator : grid height can't be 0.");
            }

            float seedFloat       = 0f;
            float layerSeedFloat  = 0f;
            float globalSeedFloat = 0f;


            UnityEngine.Random.InitState(GlobalSeed);
            int width  = layer.OverrideGridSize ? layer.GridWidth : UnityEngine.Random.Range(GridWidth.x, GridWidth.y);
            int height = layer.OverrideGridSize ? layer.GridHeight : UnityEngine.Random.Range(GridHeight.x, GridHeight.y);

            globalSeedFloat = UnityEngine.Random.value;

            // random outside of the global seed
            if (layer.DoNotUseGlobalSeed)
            {
                Random.InitState((int)System.DateTime.Now.Ticks);
                if (layer.RandomizeSeed)
                {
                    layer.Seed = Mathf.Abs(Random.Range(int.MinValue, int.MaxValue));
                }
                UnityEngine.Random.InitState(layer.Seed);
                layerSeedFloat = UnityEngine.Random.value;
            }

            int seed = layer.DoNotUseGlobalSeed ? layer.Seed : GlobalSeed;

            seedFloat = layer.DoNotUseGlobalSeed ? layerSeedFloat : globalSeedFloat;

            switch (layer.GenerateMethod)
            {
            case GenerateMethods.Full:
                _grid      = MMGridGeneratorFull.Generate(width, height, layer.FullGenerationFilled);
                layer.Grid = _grid;
                break;

            case GenerateMethods.Perlin:
                _grid      = MMGridGeneratorPerlinNoise.Generate(width, height, seedFloat);
                layer.Grid = _grid;
                break;

            case GenerateMethods.PerlinGround:
                _grid      = MMGridGeneratorPerlinNoiseGround.Generate(width, height, seedFloat);
                layer.Grid = _grid;
                break;

            case GenerateMethods.Random:
                _grid      = MMGridGeneratorRandom.Generate(width, height, seed, layer.RandomFillPercentage);
                layer.Grid = _grid;
                break;

            case GenerateMethods.RandomWalk:
                _grid      = MMGridGeneratorRandomWalk.Generate(width, height, seed, layer.RandomWalkPercent, layer.RandomWalkStartingPoint, layer.RandomWalkMaxIterations);
                layer.Grid = _grid;
                break;

            case GenerateMethods.RandomWalkAvoider:

                int[,] obstacleGrid = MMGridGenerator.TilemapToGrid(layer.RandomWalkAvoiderObstaclesTilemap, width, height);
                _grid      = MMGridGeneratorRandomWalkAvoider.Generate(width, height, seed, layer.RandomWalkAvoiderPercent, layer.RandomWalkAvoiderStartingPoint, obstacleGrid, layer.RandomWalkAvoiderObstaclesDistance, layer.RandomWalkAvoiderMaxIterations);
                layer.Grid = _grid;
                break;

            case GenerateMethods.RandomWalkGround:
                _grid = MMGridGeneratorRandomWalkGround.Generate(width, height, seed,
                                                                 layer.RandomWalkGroundMinHeightDifference, layer.RandomWalkGroundMaxHeightDifference,
                                                                 layer.RandomWalkGroundMinFlatDistance, layer.RandomWalkGroundMaxFlatDistance, layer.RandomWalkGroundMaxHeight);
                layer.Grid = _grid;
                break;

            case GenerateMethods.Path:
                _grid = MMGridGeneratorPath.Generate(width, height, seed, layer.PathDirection, layer.PathStartPosition, layer.PathMinWidth,
                                                     layer.PathMaxWidth, layer.PathDirectionChangeDistance, layer.PathWidthChangePercentage,
                                                     layer.PathDirectionChangePercentage);
                layer.Grid = _grid;
                break;

            case GenerateMethods.Copy:
                layer.TargetTilemap.ClearAllTiles();
                DelayedCopy(layer);
                break;
            }

            if (layer.Smooth)
            {
                _grid = MMGridGenerator.SmoothenGrid(_grid);
            }
            if (layer.InvertGrid)
            {
                _grid = MMGridGenerator.InvertGrid(_grid);
            }

            _grid = MMGridGenerator.BindGrid(_grid, layer.BoundsTop, layer.BoundsBottom, layer.BoundsLeft, layer.BoundsRight);
            _grid = MMGridGenerator.ApplySafeSpots(_grid, layer.SafeSpots);

            RenderGrid(layer);
        }
 /// <summary>
 /// Returns true if an obstacle is found at the specified coordinates, false otherwise
 /// </summary>
 /// <param name="obstacles"></param>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <returns></returns>
 private static bool ObstacleAt(int[,] obstacles, int x, int y)
 {
     return (MMGridGenerator.GetValueAtGridCoordinate(obstacles, x, y, 1) == 1);
 }