public TilemapTileChange(
     TilemapBase parent,
     Vector2Int position,
     UnityEngine.Tilemaps.TileBase newTile
     )
     : base(parent)
 {
     _newTile  = newTile ?? throw new ArgumentNullException(nameof(newTile));
     _position = position;
 }
        public Result SetTile(Vector2Int position, UnityEngine.Tilemaps.TileBase tile)
        {
            if (_tileLookup.TryGetValue(position, out var testTile) == false || testTile != tile)
            {
                _tileLookup[position] = tile;
                SetTileForSubTilemap(position, tile);
            }

            return(new Result(true, $"A tile \"{tile.name}\" was placed at {position}."));
        }
Example #3
0
        // Sets a movement penalty for this node. Draws the tile if debug mode is selected.
        public void SetMovementPenalty(int val, UnityEngine.Tilemaps.TileBase tile = null)
        {
            movementpenalty = val;

            // Debug - draws the nodes in debug mode;
            if (pf.DebugDrawNavGrid)
            {
                if (movementpenalty == 0)
                {
                    pf.SelectedNavigationTileMap.SetTile(pf.NavToTile(index), null);
                }
                else
                {
                    pf.SelectedNavigationTileMap.SetTile(pf.NavToTile(index), tile);
                }
            }
        }
        private void SetTileForSubTilemap(Vector2Int position, UnityEngine.Tilemaps.TileBase tile)
        {
            SetSubtilemapTile();

            void SetSubtilemapTile()
            {
                SubTilemapWrapper tilemap  = GetTileMapForPosition(position);
                Vector2Int        adjusted = PositionToSubTilemapPosition(position);
                Vector2Int        topLeft  =
                    new Vector2Int(
                        adjusted.x * _subTilemapSize.x,
                        adjusted.y * _subTilemapSize.y
                        );

                tilemap.SetTile((position - topLeft), tile);
            }
        }
Example #5
0
        public void Create(TilemapWrapper tilemap, UnityEngine.Tilemaps.TileBase[] tiles)
        {
            int width  = 120;
            int height = 60;

            var back = new UnityEngine.Tilemaps.TileBase[height, width];
            var fore = new UnityEngine.Tilemaps.TileBase[height, width];

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    fore[y, x] = back[y, x] = tiles[Random.Range(0, tiles.Length)];
                }
            }

            {
                int[] counts = new int[tiles.Length];
                for (int i = 0; i < 3; ++i)
                {
                    for (int y = 0; y < height; ++y)
                    {
                        for (int x = 0; x < width; ++x)
                        {
                            int highestCount      = -1;
                            int highestCountIndex = -1;
                            for (int tilesIndex = 0; tilesIndex < tiles.Length; ++tilesIndex)
                            {
                                counts[tilesIndex] = 0;
                            }
                            for (int yscan = -1; yscan <= 1; ++yscan)
                            {
                                int ycheck = y + yscan;
                                if (ycheck < 0)
                                {
                                    continue;
                                }
                                if (ycheck >= height)
                                {
                                    break;
                                }
                                for (int xscan = -1; xscan <= 1; ++xscan)
                                {
                                    int xcheck = x + xscan;
                                    if (xcheck < 0 ||
                                        (xcheck == 0 && ycheck == 0))
                                    {
                                        continue;
                                    }
                                    if (xcheck >= width)
                                    {
                                        break;
                                    }

                                    for (int tilesIndex = 0; tilesIndex < tiles.Length; ++tilesIndex)
                                    {
                                        if (tiles[tilesIndex] == fore[ycheck, xcheck])
                                        {
                                            counts[tilesIndex] += 1;
                                        }
                                    }
                                }
                            }

                            for (int tilesIndex = 0; tilesIndex < tiles.Length; ++tilesIndex)
                            {
                                if (counts[tilesIndex] > highestCount)
                                {
                                    highestCount      = counts[tilesIndex];
                                    highestCountIndex = tilesIndex;
                                }
                            }

                            back[y, x] = tiles[highestCountIndex];
                        }
                    }

                    { var temp = back; back = fore; fore = temp; }
                }
            }

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    tilemap.SetTile(new Vector2Int(x, y), fore[y, x]);
                }
            }
        }