inz-00/Assets/Scripts/VoronoiGridGenerator.cs

59 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using Assets.Common;
using Assets.PointDistribution;
using Assets.Voronoi;
namespace Assets
{
public class VoronoiGridGenerator : IGridGenerator
{
public const string VoronoiSiteProperty = "VoronoiSite";
private readonly int _seed;
private readonly IPointDistribution _sampler;
public VoronoiGridGenerator(int seed, IPointDistribution sampler)
{
_seed = seed;
_sampler = sampler;
}
public Map CreateGrid(double width, double height)
{
Map result = new Map(_seed);
var points = GeneratePoints(width, height);
var generator = new VoronoiGenerator(points);
generator.Generate();
result.Boundaries = (Graph<Point>)generator.Voronoi.Clone();
result.Sites = generator.Delaunay.Morph(s =>
{
var site = new MapSite(s.Point, new List<Point>());
site.Metadata.SetProperty(VoronoiSiteProperty, s);
return site;
});
return result;
}
private List<Point> GeneratePoints(double width, double height)
{
var points = new List<Point>
{
new Point(-width / 8, height / 2 + 0.1f),
new Point(width * 1.125f, height / 2 - 0.1f),
new Point(width / 2 - 0.1f, -height / 8),
new Point(width / 2 + 0.1f, height * 1.125f)
};
points.AddRange(_sampler.Generate(width, height));
return points;
}
}
}