naprawiono aretefakty lokacji
This commit is contained in:
parent
367f433639
commit
b445fcb05c
@ -2,6 +2,8 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Assets.Common;
|
using Assets.Common;
|
||||||
|
using Assets.Voronoi;
|
||||||
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
namespace Assets.AnnotationPass
|
namespace Assets.AnnotationPass
|
||||||
{
|
{
|
||||||
@ -27,16 +29,17 @@ namespace Assets.AnnotationPass
|
|||||||
|
|
||||||
private void Process(int vertex, Location location)
|
private void Process(int vertex, Location location)
|
||||||
{
|
{
|
||||||
location.AddSite(_sites.Vertices[vertex]);
|
SetLocation(_sites.Vertices[vertex], location);
|
||||||
|
|
||||||
_sites.Vertices[vertex].Metadata.SetProperty(SiteLocationProperty, location);
|
|
||||||
|
|
||||||
foreach (var neighbour in _sites.Neighbours(vertex))
|
foreach (var neighbour in _sites.Neighbours(vertex))
|
||||||
{
|
{
|
||||||
var site = _sites[neighbour];
|
var site = _sites[neighbour];
|
||||||
|
|
||||||
if (!site.Metadata.HasProperty(SiteLocationProperty))
|
if (!site.Metadata.HasProperty(SiteLocationProperty))
|
||||||
_queue.Add((neighbour, location));
|
{
|
||||||
|
if (location == _locations[0] || !site.Tags.Contains(CommonTags.Edge))
|
||||||
|
_queue.Add((neighbour, location));
|
||||||
|
}
|
||||||
else if (location != site.Metadata.GetProperty<Location>(SiteLocationProperty))
|
else if (location != site.Metadata.GetProperty<Location>(SiteLocationProperty))
|
||||||
_locations.AddEdge(
|
_locations.AddEdge(
|
||||||
_locations.Vertices.IndexOf(location),
|
_locations.Vertices.IndexOf(location),
|
||||||
@ -45,6 +48,13 @@ namespace Assets.AnnotationPass
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SetLocation(MapSite site, Location location)
|
||||||
|
{
|
||||||
|
location.AddSite(site);
|
||||||
|
|
||||||
|
site.Metadata.SetProperty(SiteLocationProperty, location);
|
||||||
|
}
|
||||||
|
|
||||||
private (int, Location) Dequeue()
|
private (int, Location) Dequeue()
|
||||||
{
|
{
|
||||||
while (_queue.Count > 0)
|
while (_queue.Count > 0)
|
||||||
@ -113,21 +123,29 @@ namespace Assets.AnnotationPass
|
|||||||
_points = map.Boundaries.Vertices;
|
_points = map.Boundaries.Vertices;
|
||||||
|
|
||||||
map.Metadata.SetProperty(MapLocationsProperty, _locations);
|
map.Metadata.SetProperty(MapLocationsProperty, _locations);
|
||||||
|
|
||||||
|
var ocean = _locations[0];
|
||||||
|
var possible = new Queue<int>(map.Sites
|
||||||
|
.Vertices
|
||||||
|
.Select((site, i) => (Vertex: i, Site: site))
|
||||||
|
.Where(tuple => !tuple.Site.Tags.Contains(CommonTags.Edge))
|
||||||
|
.Select(tuple => tuple.Vertex)
|
||||||
|
.OrderBy(tuple => _random.Next()));
|
||||||
|
|
||||||
var initial =
|
var initial =
|
||||||
from location in _locations.Vertices.Skip(1)
|
from location in _locations.Vertices.Skip(1)
|
||||||
select (_random.Next(map.Sites.Vertices.Count), location);
|
select (possible.Dequeue(), location);
|
||||||
|
|
||||||
initial = initial.Concat(
|
initial = initial.Concat(
|
||||||
map.Sites.Vertices
|
map.Sites.Vertices
|
||||||
.Select((s, i) => (Site: s, Vertex: i))
|
.Select((s, i) => (Site: s, Vertex: i))
|
||||||
.Where(t => t.Site.IsOuter)
|
.Where(t => t.Site.Tags.Contains(CommonTags.Outer))
|
||||||
.Select(t => (t.Vertex, _locations[0]))
|
.Select(t => (t.Vertex, ocean))
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach (var (vertex, location) in initial)
|
foreach (var (vertex, location) in initial)
|
||||||
Process(vertex, location);
|
Process(vertex, location);
|
||||||
|
|
||||||
while (_queue.Count != 0)
|
while (_queue.Count != 0)
|
||||||
Step();
|
Step();
|
||||||
}
|
}
|
||||||
|
11
Assets/Scripts/Common/CommonTags.cs
Normal file
11
Assets/Scripts/Common/CommonTags.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
namespace Assets.Common
|
||||||
|
{
|
||||||
|
public static class CommonTags
|
||||||
|
{
|
||||||
|
public const string Outer = "Outer";
|
||||||
|
public const string Empty = "Empty";
|
||||||
|
public const string Land = "Land";
|
||||||
|
public const string Water = "Water";
|
||||||
|
public const string Edge = "Edge";
|
||||||
|
}
|
||||||
|
}
|
3
Assets/Scripts/Common/CommonTags.cs.meta
Normal file
3
Assets/Scripts/Common/CommonTags.cs.meta
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1019713c428346a2a9c5e6f57b360d15
|
||||||
|
timeCreated: 1573936282
|
@ -18,8 +18,7 @@ namespace Assets.Common
|
|||||||
|
|
||||||
public int Index { get; internal set; }
|
public int Index { get; internal set; }
|
||||||
public Point Center { get; set; }
|
public Point Center { get; set; }
|
||||||
public bool IsOuter { get; set; } = false;
|
public ISet<string> Tags { get; set; } = new HashSet<string> { CommonTags.Empty };
|
||||||
public ISet<string> Tags { get; set; } = new HashSet<string> { "Empty" };
|
|
||||||
public IEnumerable<int> Boundary { get; set; }
|
public IEnumerable<int> Boundary { get; set; }
|
||||||
public IEnumerable<(int, int)> Edges => Boundary.RotateRight().Zip(Boundary, (a, b) => (a, b));
|
public IEnumerable<(int, int)> Edges => Boundary.RotateRight().Zip(Boundary, (a, b) => (a, b));
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ namespace Assets.GridGenerator
|
|||||||
public class VoronoiGridGenerator : IGridGenerator
|
public class VoronoiGridGenerator : IGridGenerator
|
||||||
{
|
{
|
||||||
public const string VoronoiSiteProperty = "VoronoiSite";
|
public const string VoronoiSiteProperty = "VoronoiSite";
|
||||||
|
|
||||||
private readonly int _seed;
|
private readonly int _seed;
|
||||||
private readonly IPointDistribution _sampler;
|
private readonly IPointDistribution _sampler;
|
||||||
|
|
||||||
@ -27,23 +27,49 @@ namespace Assets.GridGenerator
|
|||||||
var generator = new VoronoiGenerator(points);
|
var generator = new VoronoiGenerator(points);
|
||||||
|
|
||||||
generator.Generate();
|
generator.Generate();
|
||||||
|
|
||||||
|
bool IsOutside(Point point) => !(point.x > 0 && point.x < width && point.y > 0 && point.y < height);
|
||||||
|
|
||||||
result.Boundaries = (Graph<Point>)generator.Voronoi.Clone();
|
result.Boundaries = generator.Voronoi.Clone() as Graph<Point>;
|
||||||
result.Sites = generator.Delaunay.Morph(s =>
|
result.Sites = generator.Delaunay.Morph(s =>
|
||||||
{
|
{
|
||||||
var site = new MapSite(s.Point, ClockwiseVertices(generator.Voronoi, s.Vertices));
|
var site = new MapSite(s.Point, ClockwiseVertices(generator.Voronoi, s.Vertices));
|
||||||
|
|
||||||
site.Metadata.SetProperty(VoronoiSiteProperty, s);
|
site.Metadata.SetProperty(VoronoiSiteProperty, s);
|
||||||
|
|
||||||
|
if (site.Boundary.Select(n => result.Boundaries[n]).Any(IsOutside))
|
||||||
|
site.Tags.Add(CommonTags.Edge);
|
||||||
|
|
||||||
return site;
|
return site;
|
||||||
});
|
});
|
||||||
|
|
||||||
foreach (var site in result.Sites.Vertices.Take(4))
|
foreach (var site in result.Sites.Vertices.Take(4))
|
||||||
site.IsOuter = true;
|
site.Tags.Add(CommonTags.Outer);
|
||||||
|
|
||||||
|
FixTooLongEdges(result, width, height);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void FixTooLongEdges(Map map, double width, double height)
|
||||||
|
{
|
||||||
|
List<(int, int)> remove = new List<(int, int)>();
|
||||||
|
|
||||||
|
bool IsValid(Point point) => point.x > 0 && point.x < width && point.y > 0 && point.y < height;
|
||||||
|
|
||||||
|
foreach (var (a, b) in map.Boundaries.Edges)
|
||||||
|
{
|
||||||
|
var pointA = map.Boundaries[a];
|
||||||
|
var pointB = map.Boundaries[b];
|
||||||
|
|
||||||
|
if (!IsValid(pointA) || !IsValid(pointB))
|
||||||
|
remove.Add((a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var (a, b) in remove)
|
||||||
|
map.Boundaries.DeleteEdge(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
private List<Point> GeneratePoints(double width, double height)
|
private List<Point> GeneratePoints(double width, double height)
|
||||||
{
|
{
|
||||||
var points = new List<Point>
|
var points = new List<Point>
|
||||||
|
Loading…
Reference in New Issue
Block a user