diff --git a/Assets/Editor/ForestRendererUI.cs b/Assets/Editor/ForestRendererUI.cs deleted file mode 100644 index c3bb15b..0000000 --- a/Assets/Editor/ForestRendererUI.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Assets; -using UnityEditor; -using UnityEngine; - -namespace Editor -{ - [CustomEditor(typeof (ForestGenerator))] - public class ForestGeneratorUI : UnityEditor.Editor - { - public override void OnInspectorGUI() - { - var target = this.target as ForestGenerator; - - DrawDefaultInspector(); - - if (GUILayout.Button("Generate")) - { -// target.Generate(); - } - } - } -} \ No newline at end of file diff --git a/Assets/Editor/ForestRendererUI.cs.meta b/Assets/Editor/ForestRendererUI.cs.meta deleted file mode 100644 index d9ac9cf..0000000 --- a/Assets/Editor/ForestRendererUI.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 0d169cf324704c908e59fcebe537a011 -timeCreated: 1567618094 \ No newline at end of file diff --git a/Assets/Scripts/AnnotationPass/LandmassPass.cs b/Assets/Scripts/AnnotationPass/LandmassPass.cs index 751dd7a..432a15e 100644 --- a/Assets/Scripts/AnnotationPass/LandmassPass.cs +++ b/Assets/Scripts/AnnotationPass/LandmassPass.cs @@ -35,12 +35,12 @@ namespace Assets.AnnotationPass { var site = _sites[neighbour]; - if (!site.Metadata.HasProperty(SiteLocationProperty)) + if (!site.HasProperty(SiteLocationProperty)) { if (location == _locations[0] || !site.Tags.Contains(CommonTags.Edge)) _queue.Add((neighbour, location)); } - else if (location != site.Metadata.GetProperty(SiteLocationProperty)) + else if (location != site.GetProperty(SiteLocationProperty)) _locations.AddEdge( _locations.Vertices.IndexOf(location), _locations.Vertices.IndexOf(site.Metadata.GetProperty(SiteLocationProperty)) @@ -55,7 +55,8 @@ namespace Assets.AnnotationPass if (location != _locations[0]) site.Tags.Add(CommonTags.Land); - site.Metadata.SetProperty(SiteLocationProperty, location); + site.SetProperty(SiteLocationProperty, location); + site.SetProperty(CommonSiteProperties.Height, (double)location.Type.height); } private (int, Location) Dequeue() @@ -67,7 +68,7 @@ namespace Assets.AnnotationPass _queue.RemoveAt(index); - if (!_sites[pair.Item1].Metadata.HasProperty(SiteLocationProperty)) return pair; + if (!_sites[pair.Item1].HasProperty(SiteLocationProperty)) return pair; } throw new Exception("No more elements."); @@ -125,7 +126,7 @@ namespace Assets.AnnotationPass _sites = map.Sites; _points = map.Boundaries.Vertices; - map.Metadata.SetProperty(MapLocationsProperty, _locations); + map.SetProperty(MapLocationsProperty, _locations); var ocean = _locations[0]; var possible = new Queue(map.Sites diff --git a/Assets/Scripts/Common/CommonSiteProperties.cs b/Assets/Scripts/Common/CommonSiteProperties.cs new file mode 100644 index 0000000..966a11d --- /dev/null +++ b/Assets/Scripts/Common/CommonSiteProperties.cs @@ -0,0 +1,7 @@ +namespace Assets.Common +{ + public class CommonSiteProperties + { + public const string Height = "Common.Height"; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Common/CommonSiteProperties.cs.meta b/Assets/Scripts/Common/CommonSiteProperties.cs.meta new file mode 100644 index 0000000..f22004c --- /dev/null +++ b/Assets/Scripts/Common/CommonSiteProperties.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6890627434244b08a4602c46789ad43b +timeCreated: 1574019493 \ No newline at end of file diff --git a/Assets/Scripts/ForestGenerator.cs b/Assets/Scripts/ForestGenerator.cs deleted file mode 100644 index 3475e5f..0000000 --- a/Assets/Scripts/ForestGenerator.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Assets.Common; -using Assets.PointDistribution; -using Assets.Voronoi; -using UnityEngine; -using Random = System.Random; - -//namespace Assets -//{ -// [RequireComponent(typeof(MeshFilter))] -// [RequireComponent(typeof(MeshRenderer))] -// [RequireComponent(typeof(LandmassGenerator))] - public class ForestGenerator : MonoBehaviour - { -// public GameObject[] trees; -// [Range(1, 15)] -// public float radius = 3.0f; -// public GameObject forest; -// -// private Random _random = new Random(); -// private List _sites; -// -// private LandmassGenerator Generator => GetComponent(); -// -// public void Generate() -// { -// Cleanup(); -// -// Generator.EnsureGenerated(); -// -// FindForestSites(); -// PlaceTrees(); -// -// _random = new Random(Generator.seed); -// } -// -// private void PlaceTrees() -// { -// foreach (Site site in _sites) -// { -// var sampler = new PoissonDiskSampler(radius) { Generator = _random }; -// -// Bounding bounding = -// BoundingTools.GetBounding(site.Vertices.Select(i => Generator.BoundariesGraph.Vertices[i])); -// -// var offset = Vector3.up * site.Location.Type.height; -// var points = sampler.Generate((float)bounding.Width, (float)bounding.Height); -// -// foreach (var point in points.Select(point => point + bounding.Min).Where(point => IsPointInSite(site, point))) -// PlaceRandomTree(point.ToVector3() + offset); -// } -// } -// -// public void Cleanup() -// { -// var destroy = new List(); -// -// foreach (var child in forest.transform) -// if (child is Transform t) -// destroy.Add(t.gameObject); -// -// foreach (var @object in destroy) -// DestroyImmediate(@object); -// } -// -// private void FindForestSites() -// { -// _sites = new List(); -// -// foreach (var location in Generator.LocationGraph.Vertices.Skip(1)) -// { -// var count = _random.Next(10); -// -// for (int i = 0; i < count; i++) -// _sites.Add(location.Sites[_random.Next(location.Sites.Count)]); -// } -// } -// -// private void PlaceRandomTree(Vector3 pos) -// { -// GameObject tree = Instantiate(trees[_random.Next(trees.Length)], forest.transform); -// -// tree.transform.position = pos - Vector3.down * 0.2f; -// } -// -// private bool IsPointInSite(Site site, Point point) -// { -// var polygon = site.Vertices.Select(i => Generator.BoundariesGraph[i]).ToArray(); -// -// return PointUtils.IsPointInside(point, polygon); -// } - } -//} \ No newline at end of file diff --git a/Assets/Scripts/ForestGenerator.cs.meta b/Assets/Scripts/ForestGenerator.cs.meta deleted file mode 100644 index e7f129c..0000000 --- a/Assets/Scripts/ForestGenerator.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 36d65e29139a4864b1c3a6beb0db0cf1 -timeCreated: 1567614336 \ No newline at end of file diff --git a/Assets/Scripts/MapRenderer.cs b/Assets/Scripts/MapRenderer.cs index 7578d81..654b953 100644 --- a/Assets/Scripts/MapRenderer.cs +++ b/Assets/Scripts/MapRenderer.cs @@ -17,11 +17,18 @@ namespace Assets private bool _rerender = false; public GameObject citiesGameObject; + public GameObject forestGameObject; + public GameObject[] trees; private IList Renderers => new List { new LandmassRenderer(), new CityRenderer(citiesGameObject), + new ForestRender + { + ForestObject = forestGameObject, + Trees = trees, + } }; public void GenerateRandom() diff --git a/Assets/Scripts/RenderPass/ForestRender.cs b/Assets/Scripts/RenderPass/ForestRender.cs new file mode 100644 index 0000000..a83eb55 --- /dev/null +++ b/Assets/Scripts/RenderPass/ForestRender.cs @@ -0,0 +1,45 @@ +using System.Collections; +using System.Collections.Generic; +using Assets.AnnotationPass; +using Assets.Common; +using UnityEngine; +using Random = System.Random; +using Tree = Assets.Common.Tree; + +namespace Assets.RenderPass +{ + public class ForestRender : IRenderer + { + public ICollection Trees { get; set; } + public GameObject ForestObject { get; set; } + + public void Render(Map map, Component component) + { + var rng = new Random(map.Seed); + var trees = map.GetProperty>(ForestPass.TreesProperty); + + var toRemove = new List(); + foreach (Transform child in ForestObject.transform) + toRemove.Add(child.gameObject); + + foreach (var o in toRemove) + Object.DestroyImmediate(o); + + foreach (var tree in trees) + { + var origin = tree.Placement.ToVector3() + (float)tree.Site.GetProperty(CommonSiteProperties.Height) * Vector3.up; + Object.Instantiate(GetRandomTree(rng), origin, Quaternion.identity, ForestObject.transform); + } + } + + private GameObject GetRandomTree(Random generator) + { + return Trees.RandomElement(generator); + } + + public void DrawGizmos(Map map, Component component) + { + + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/RenderPass/ForestRender.cs.meta b/Assets/Scripts/RenderPass/ForestRender.cs.meta new file mode 100644 index 0000000..20fd113 --- /dev/null +++ b/Assets/Scripts/RenderPass/ForestRender.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f7a96fe45ad140fcb69b84c7d27e5ca9 +timeCreated: 1574019277 \ No newline at end of file