예제 #1
0
        public override void Apply(TilemapStructure tilemap)
        {
            int targetTileId  = (int)TargetTile;
            int replaceTileId = (int)ReplacedBy;

            for (int i = 0; i < Repetitions; i++)
            {
                for (int x = 0; x < tilemap.Width; x++)
                {
                    for (int y = 0; y < tilemap.Height; y++)
                    {
                        // Check if the current tile is our target tile
                        var tile = tilemap.GetTile(x, y);
                        if (tile == targetTileId)
                        {
                            // Retrieve all 8 neighbors of our current tile
                            var neighbors = tilemap.GetNeighbors(x, y);

                            // Count all the neighbors that are of type target tile
                            int targetTilesCount = neighbors.Count(a => a.Value == targetTileId);

                            // If the min alive count is not reached, we replace the tile
                            if (targetTilesCount < MinAlive)
                            {
                                if (ReplaceByDominantTile)
                                {
                                    // Group tiles on tiletype, then order them in descending order based on group size
                                    // Select the group's key which is the tiletype because thats what we grouped on
                                    // And select the first one (first group's key), because that's the dominant tile type
                                    var dominantTile = neighbors
                                                       .GroupBy(a => a.Value)
                                                       .OrderByDescending(a => a.Count())
                                                       .Select(a => a.Key)
                                                       .First();

                                    tilemap.SetTile(x, y, dominantTile);
                                }
                                else
                                {
                                    tilemap.SetTile(x, y, replaceTileId);
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 모래 타일, 물 타일, 풀 타일 뭐 등등 그 중에 풀 타일을 골라
        ///  10x10 그리드에  각 셀 하나하나 그 타일의 8개의 이웃타일에게 얼마나 많이 같은 타입이 있는지 세고
        ///  그 숫자를 통해 타일들을 같은 타입으로 두냐 아니냐를 선택한다.
        /// </summary>
        /// <param name="tilemap"></param>


        public override void Apply(TilemapStructure tilemap)
        {
            int targetTileId  = (int)TargetTile;
            int replaceTileId = (int)ReplaceBy;

            for (int i = 0; i < Reptitions; i++)
            {
                for (int x = 0; x < tilemap.Width; x++)
                {
                    for (int y = 0; y < tilemap.Height; y++)
                    {
                        var tile = tilemap.GetTileEnumNumber(x, y);
                        if (tile == targetTileId)
                        {
                            var neighbors = tilemap.GetNeighbors(x, y);

                            int targetTilesCount = neighbors.Count(a => a.Value == targetTileId);


                            // 만약 minAlive 숫자가 도달하지 못하면 타일을 교체한다
                            if (targetTilesCount < minAlive)
                            {
                                if (ReplaceByDominantTile)
                                {
                                    int dominantTile = neighbors
                                                       .GroupBy(a => a.Value)
                                                       .OrderByDescending(a => a.Count())
                                                       .Select(a => a.Key)
                                                       .First();
                                    tilemap.SetTileEnumNumber(x, y, dominantTile);
                                }
                                else
                                {
                                    tilemap.SetTileEnumNumber(x, y, replaceTileId);
                                }
                            }
                        }
                    }
                }
            }
        }