Exemplo n.º 1
0
        public static void Voronoi(int seed, CVec2 dimensions, ref Map map)
        {
            List <CVec2> sites = MakeVoronoiSites(seed, dimensions, 6);

            for (var itY = 0; itY < dimensions.y; ++itY)
            {
                for (var itX = 0; itX < dimensions.x; ++itX)
                {
                    var idx = map.Idx(itX, itY);
                    map.tiles[idx].Layers.Remove(LayerType.Voronoi);
                    float bestDistance = 9999999;
                    for (var itSite = 0; itSite < sites.Count; ++itSite)
                    {
                        CVec2 site         = sites[itSite];
                        float siteDistance = site.DistanceTo(map.tiles[idx].Location);
                        if (itSite == 0)
                        {
                            var layer = new VoronoiLayer {
                                VoronoiSite = site, VoronoiColor = GenerateColor(0)
                            };
                            map.tiles[idx].Layers.Add(LayerType.Voronoi, layer);
                            bestDistance = siteDistance;
                        }
                        else if (siteDistance < bestDistance)
                        {
                            var layer = map.tiles[idx].Layers[LayerType.Voronoi] as VoronoiLayer;
                            layer.VoronoiSite  = site;
                            layer.VoronoiColor = GenerateColor(itSite);
                            bestDistance       = siteDistance;
                            //map.tiles[idx].Layers[LayerType.Voronoi] = layer;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public string Get(int width, int height, int seed)
        {
            int worldWidth  = width > 0 ? width : 18;
            int worldHeight = height > 0 ? height : 15;
            int worldSeed   = seed > 0 ? seed : 0;
            int total       = worldWidth * worldHeight;

            _logger.LogInformation("generating" + total.ToString());
            Map ceData = new Map(worldWidth, worldHeight);

            WorldGenerator.Generate(worldSeed, ref ceData);

            var sb = new System.Text.StringBuilder();


            sb.Append('{', 1);

            sb.AppendLine();
            sb.Append("   \"width\": ");
            sb.Append(ceData.Width.ToString());
            sb.AppendLine(",");
            sb.Append("   \"height\": ");
            sb.Append(ceData.Height.ToString());
            sb.AppendLine(",");

            Func <Tile, string> tileCharFunc = (t) =>
            {
                TileType tileType = t.TileType;
                return(WorldLib.ConvertTileTypeToChar(tileType).ToString());
            };
            Func <Tile, string> tileElevationFunc = (t) =>
            {
                return(t.Elevation.ToString());
            };

            Func <Tile, string> tileColorFunc = (t) =>
            {
                if (t.Layers.ContainsKey(LayerType.Voronoi))
                {
                    VoronoiLayer layer = t.Layers[LayerType.Voronoi] as VoronoiLayer;
                    return(layer.VoronoiColor.ToWideNumberString());
                }
                else
                {
                    return("#000000");
                }
            };

            GetProperty("tileChar", tileCharFunc, false, true, ref ceData, ref sb);
            GetProperty("tileElevation", tileElevationFunc, true, false, ref ceData, ref sb);
            GetProperty("tileColor", tileColorFunc, true, false, ref ceData, ref sb);
            sb.AppendLine("   \"eof\": \"eof\"");
            sb.Append('}', 1);
            return(sb.ToString());
        }