private static void DrawOverlay(TownGeometry geometry, StringBuilder sb) { foreach (var patch in geometry.Overlay) { sb.Append(patch.Shape.ToSvgPolygon("overlay", $" id=\"{patch.Id}\"")); } }
private void DrawRoads(TownGeometry geometry, StringBuilder sb) { Roads = new GameObject("Roads"); Roads.layer = LayerMask.NameToLayer("Map"); Roads.transform.parent = child; Roads.transform.localPosition = Vector3.zero; Cube cube; foreach (var road in geometry.Roads) { Geom.Vector2 last = new Geom.Vector2(0, 0); foreach (var current in road) { if (last.x != 0 && last.y != 0) { float MovedlastX = ScaleToWorldWithOffset(last.x, town.townOffset.x); float MovedlastY = ScaleToWorldWithOffset(last.y, town.townOffset.y); float MovedcurrentX = ScaleToWorldWithOffset(current.x, town.townOffset.x); float MovedcurrentY = ScaleToWorldWithOffset(current.y, town.townOffset.y); cube = new Cube("Road", GetLineVertices( MovedlastX, MovedcurrentX, MovedlastY, MovedcurrentY, 3 ), .2f, rendererOptions.RoadMaterial, Roads.transform, false, true); cube.Transform.localPosition = new Vector3(0, 1000f, 0); cube.GameObject.layer = LayerMask.NameToLayer("Map"); } last = current; } } }
private void DrawRoads(TownGeometry geometry, StringBuilder sb) { Roads = new GameObject("Roads"); Roads.transform.parent = child; Roads.transform.localPosition = Vector3.zero; Cube cube; foreach (var road in geometry.Roads) { Vector2 last = new Vector2(0, 0); foreach (var current in road) { if (last.x != 0 && last.y != 0) { cube = new Cube("Road", GetLineVertices( last.x, current.x, last.y, current.y, 2 ), 0.2f, rendererOptions.RoadMaterial, Roads.transform); cube.Transform.localPosition = Vector3.zero; } last = current; } } }
private void DrawWalls(TownGeometry geometry, StringBuilder sb) { WallsMesh = new GameObject("WallsMesh"); WallsMesh.transform.parent = child; Walls = new GameObject("Walls"); Walls.transform.parent = child; var replacedGates = new List <Geom.Vector2>(); foreach (var wall in geometry.Walls) { var start = wall.A; var end = wall.B; if (geometry.Gates.Contains(start)) { replacedGates.Add(start); start = start + Geom.Vector2.Scale(end - start, 0.3f); wall.A = start; geometry.Gates.Add(start); } if (geometry.Gates.Contains(end)) { replacedGates.Add(end); end = end - Geom.Vector2.Scale(end - start, 0.3f); wall.B = end; geometry.Gates.Add(end); } new Cube("Wall", GetLineVertices( start.x, end.x, start.y, end.y ), 0.1f, WallMaterial, Walls.transform); new Cube("WallMesh", GetLineVertices( start.x, end.x, start.y, end.y ), 4, WallMaterial, WallsMesh.transform, false); } foreach (var replacedGate in replacedGates.Distinct()) { geometry.Gates.Remove(replacedGate); } foreach (var tower in geometry.Towers) { new Cube("Tower", GetVertices(4, 4, tower.x - 2, tower.y - 2), 0.1f, TowerMaterial, Walls.transform); new Cube("TowerMesh", GetVertices(4, 4, tower.x - 2, tower.y - 2), 8, TowerMaterial, WallsMesh.transform, false); } foreach (var gate in geometry.Gates) { new Cube("Gate", GetVertices(4, 4, gate.x - 2, gate.y - 2), 0.1f, GateMaterial, Walls.transform); new Cube("GateMesh", GetVertices(4, 4, gate.x - 2, gate.y - 2), 6, GateMaterial, WallsMesh.transform, false); } }
private static void DrawRoads(TownGeometry geometry, StringBuilder sb) { foreach (var road in geometry.Roads) { var path = string.Join(" ", road.Select(v => v.ToString())); sb.Append($"<polyline class=\"road-outer\" points=\"{path}\" />"); sb.Append($"<polyline class=\"road-inner\" points=\"{path}\" />"); } }
private void DrawOverlay(TownGeometry geometry, StringBuilder sb) { List <Vector3> vertices = new List <Vector3>(); var overlays = new GameObject("Overlays"); overlays.transform.parent = child; foreach (var patch in geometry.Overlay) { foreach (var vertex in patch.Shape.Vertices) { vertices.Add(new Vector3(vertex.x, 0, vertex.y)); } new MeshUtils.Polygon("Patch", vertices, 0.05f, OverlayMaterial, overlays.transform, true); vertices.Clear(); } }
private static void DrawWalls(TownGeometry geometry, StringBuilder sb) { var replacedGates = new List <Vector2>(); foreach (var wall in geometry.Walls) { var start = wall.A; var end = wall.B; if (geometry.Gates.Contains(start)) { replacedGates.Add(start); start = start + Vector2.Scale(end - start, 0.3f); wall.A = start; geometry.Gates.Add(start); } if (geometry.Gates.Contains(end)) { replacedGates.Add(end); end = end - Vector2.Scale(end - start, 0.3f); wall.B = end; geometry.Gates.Add(end); } sb.Append($"<line x1=\"{start.x}\" y1=\"{start.y}\" x2=\"{end.x}\" y2=\"{end.y}\" class=\"wall\" />"); } foreach (var replacedGate in replacedGates.Distinct()) { geometry.Gates.Remove(replacedGate); } foreach (var tower in geometry.Towers) { sb.Append($"<rect width=\"8\" height=\"8\" x=\"{tower.x - 4}\" y=\"{tower.y - 4}\" class=\"tower\" />"); } foreach (var gate in geometry.Gates) { sb.Append($"<rect width=\"8\" height=\"8\" x=\"{gate.x - 4}\" y=\"{gate.y - 4}\" class=\"gate\" />"); } }
public TownGeometry GetTownGeometry(TownOptions options) { var geometry = new TownGeometry(); var buildingShapes = new List <Polygon> (); foreach (var patch in Patches.Where(p => p.Area != null)) { buildingShapes.AddRange(patch.Area.GetGeometry()); } var buildingPlacer = new BuildingPlacer(buildingShapes); var buildings = buildingPlacer.PopulateBuildings(); geometry.Buildings.AddRange(buildings); if (options.Walls) { geometry.Walls.AddRange(CityWall.GetEdges().Union(Castle.Wall.GetEdges()).Distinct()); geometry.Towers.AddRange(CityWall.Towers.Union(Castle.Wall.Towers)); geometry.Gates.AddRange(CityWall.Gates.Union(Castle.Wall.Gates)); } else { var castleWall = CityWall.GetEdges().Union(Castle.Wall.GetEdges()).Distinct().SelectMany(e => new [] { e.A, e.B }).Where(w => Castle.Patch.Shape.Vertices.Contains(w)).ToList(); var towers = CityWall.Towers.Union(Castle.Wall.Towers).Intersect(castleWall); var gates = CityWall.Gates.Union(Castle.Wall.Gates).Intersect(castleWall); geometry.Walls.AddRange(Edge.FromPointList(castleWall)); geometry.Towers.AddRange(towers); geometry.Gates.AddRange(gates); } geometry.Roads.AddRange(Roads); geometry.Roads.AddRange(Streets); geometry.Overlay.AddRange(Patches); geometry.Water.AddRange(Patches.Where(p => p.Water).Select(p => p.Shape)); geometry.WaterBorder = new Polygon(WaterBorder); return(geometry); }
private void DrawRoads(TownGeometry geometry, StringBuilder sb) { Roads = new GameObject("Roads"); Roads.transform.parent = child; foreach (var road in geometry.Roads) { Geom.Vector2 last = new Geom.Vector2(0, 0); foreach (var current in road) { if (last.x != 0 && last.y != 0) { new Cube("Road", GetLineVertices( last.x, current.x, last.y, current.y, 2 ), 0.2f, RoadMaterial, Roads.transform); } last = current; } } }
//private void DrawWallSplines(TownGeometry geometry, StringBuilder sb) //{ // var FenceSpline = new GameObject("FenceSpline"); // FenceSpline.transform.parent = child; // FenceSpline.transform.localPosition = Vector3.zero; // // we now - annoyingly - have a start and an end node to deal with from the outset... // // so if first Former.StartNode.transform.position = new Vector3(); // // if second Former.EndNode.transform.position = new Vector3(); // // if subsequent SplineNode node = Former.AddNodeImmediately(); node.transform.position = new Vector3(); // var replacedGates = new List<Geom.Vector2>(); // for (int i = 0; i < geometry.Walls.Count; i++) // { // // for some reason the first line is the circumference we don't want that? // if (i == 0) // { // continue; // } // Edge wall = (Edge)geometry.Walls[i]; // var splineFormer = GameObject.Instantiate(TownHolder.Instance.FenceSplineFormer, FenceSpline.transform); // splineFormer.name = String.Format("{0}_Wall{1}", town.name, i); // splineFormer.transform.localPosition = Vector3.zero; // var Former = splineFormer.GetComponent<SplineFormer>(); // var start = wall.A; // var end = wall.B; // if (wall.A == wall.B) // continue; // Former.SegmentsNumber = Math.Max(1, (int)Mathf.Sqrt(Geom.Vector2.DistanceSquare(wall.A, wall.B))); // // optionally offset the ends to fit the Gates // if (geometry.Gates.Contains(start)) // { // replacedGates.Add(start); // start = start + Geom.Vector2.Scale(end - start, 0.3f); // wall.A = start; // geometry.Gates.Add(start); // } // if (geometry.Gates.Contains(end)) // { // replacedGates.Add(end); // end = end - Geom.Vector2.Scale(end - start, 0.3f); // wall.B = end; // geometry.Gates.Add(end); // } // float MovedstartX = ScaleToWorldWithOffset(start.x, town.townOffset.x); // float MovedstartY = ScaleToWorldWithOffset(start.y, town.townOffset.y); // float MovedendX = ScaleToWorldWithOffset(end.x, town.townOffset.x); // float MovedendY = ScaleToWorldWithOffset(end.y, town.townOffset.y); // float startPlace, endPlace; // startPlace = GetTerrainPos(MovedstartX, MovedstartY).y; // endPlace = GetTerrainPos(MovedendX, MovedendY).y; // Former.StartNode.transform.position = new Vector3(MovedstartX, startPlace, MovedstartY); // Former.EndNode.transform.position = new Vector3(MovedendX, endPlace, MovedendY); // } // MakeFenceFromEdges(geometry, FenceSpline, replacedGates, town.CityWall.GetEdges(), "CityWall"); // MakeFenceFromEdges(geometry, FenceSpline, replacedGates, town.Market.Edges, "Market"); //} //private void MakeFenceFromEdges(TownGeometry geometry, GameObject FenceSpline, List<Geom.Vector2> replacedGates, IEnumerable<Edge> EdgeIEnumerable, string prettyName) //{ // int i = 0; // foreach (var (wall, splineFormer) in from wall in EdgeIEnumerable // let splineFormer = GameObject.Instantiate(TownHolder.Instance.FenceSplineFormer, FenceSpline.transform) // select (wall, splineFormer)) // { // splineFormer.name = String.Format("{0}_Wall{1}{2}", town.name, prettyName, i); // splineFormer.transform.localPosition = Vector3.zero; // var Former = splineFormer.GetComponent<SplineFormer>(); // var start = wall.A; // var end = wall.B; // if (wall.A == wall.B) // continue; // Former.SegmentsNumber = Math.Max(1, (int)Mathf.Sqrt(Geom.Vector2.DistanceSquare(wall.A, wall.B))); // // optionally offset the ends to fit the Gates // if (geometry.Gates.Contains(start)) // { // replacedGates.Add(start); // start = start + Geom.Vector2.Scale(end - start, 0.3f); // wall.A = start; // geometry.Gates.Add(start); // } // if (geometry.Gates.Contains(end)) // { // replacedGates.Add(end); // end = end - Geom.Vector2.Scale(end - start, 0.3f); // wall.B = end; // geometry.Gates.Add(end); // } // float MovedstartX = ScaleToWorldWithOffset(start.x, town.townOffset.x); // float MovedstartY = ScaleToWorldWithOffset(start.y, town.townOffset.y); // float MovedendX = ScaleToWorldWithOffset(end.x, town.townOffset.x); // float MovedendY = ScaleToWorldWithOffset(end.y, town.townOffset.y); // float startPlace, endPlace; // startPlace = GetTerrainPos(MovedstartX, MovedstartY).y; // endPlace = GetTerrainPos(MovedendX, MovedendY).y; // Former.StartNode.transform.position = new Vector3(MovedstartX, startPlace, MovedstartY); // Former.EndNode.transform.position = new Vector3(MovedendX, endPlace, MovedendY); // i = i + 1; // } //} private void DrawWalls(TownGeometry geometry, StringBuilder sb) { return; Cube cube; WallsMesh = new GameObject("WallsMesh"); WallsMesh.transform.parent = child; WallsMesh.transform.localPosition = Vector3.zero; Walls = new GameObject("Walls"); Walls.transform.parent = child; Walls.transform.localPosition = Vector3.zero; var replacedGates = new List <Geom.Vector2>(); foreach (var wall in geometry.Walls) { var start = wall.A; var end = wall.B; if (wall.A == wall.B) { continue; } if (geometry.Gates.Contains(start)) { replacedGates.Add(start); start = start + Geom.Vector2.Scale(end - start, 0.3f); wall.A = start; geometry.Gates.Add(start); } if (geometry.Gates.Contains(end)) { replacedGates.Add(end); end = end - Geom.Vector2.Scale(end - start, 0.3f); wall.B = end; geometry.Gates.Add(end); } float MovedstartX = ScaleToWorldWithOffset(start.x, town.townOffset.x); float MovedstartY = ScaleToWorldWithOffset(start.y, town.townOffset.y); float MovedendX = ScaleToWorldWithOffset(end.x, town.townOffset.x); float MovedendY = ScaleToWorldWithOffset(end.y, town.townOffset.y); float startPlace, endPlace; var thing = TownGlobalObjectService.MapMagicObjectRef.tiles.FindByWorldPosition(MovedstartX, MovedstartY); // var thing = TownGlobalObjectService.MapMagicObjectRef.tiles.FindByWorldPosition(start.x, start.y); // if (thing !=null) // sigh if (false) { Terrain ter = thing.ActiveTerrain; TerrainData terData = ter.terrainData; startPlace = terData.GetHeight((int)MovedstartX, (int)MovedstartY); } else { startPlace = GetTerrainPos(MovedstartX, MovedstartY).y; } // if (thing != null) // sigh if (false) { thing = TownGlobalObjectService.MapMagicObjectRef.tiles.FindByWorldPosition(MovedendX, MovedendY); Terrain ter = thing.ActiveTerrain; TerrainData terData = ter.terrainData; endPlace = terData.GetHeight((int)MovedendX, (int)MovedendY); } else { endPlace = GetTerrainPos(MovedendX, MovedendY).y; } //cube = new Cube("Wall", GetLineVertices( // ScaleToWorldWithOffset(start.x, town.townOffset.x), // ScaleToWorldWithOffset(end.x, town.townOffset.x), // ScaleToWorldWithOffset(start.y, town.townOffset.y), // ScaleToWorldWithOffset(end.y, town.townOffset.y) //), 0.1f, rendererOptions.WallMaterial, Walls.transform); //cube.Transform.localPosition = Vector3.zero; //cube = new Cube("WallMesh", GetLineVertices( // ScaleToWorldWithOffset(start.x, town.townOffset.x), // ScaleToWorldWithOffset(end.x, town.townOffset.x), // ScaleToWorldWithOffset(start.y, town.townOffset.y), // ScaleToWorldWithOffset(end.y, town.townOffset.y) //), 4, rendererOptions.WallMaterial, WallsMesh.transform, true); cube = new Cube("WallMesh", GetLineVertices( MovedstartX, MovedendX, MovedstartY, MovedendY ), 4, rendererOptions.WallMaterial, WallsMesh.transform, true); //Mesh mesh = cube.MeshFilter.mesh; //Vector3[] vertices = mesh.vertices; //UnityEngine.Vector2[] uvs = new UnityEngine.Vector2[vertices.Length]; //for (int i = 0; i < uvs.Length; i++) //{ // uvs[i] = new UnityEngine.Vector2(vertices[i].x, vertices[i].z); //} //mesh.RecalculateNormals(); //mesh.RecalculateTangents(); //mesh.uv = uvs; //cube.MeshFilter.mesh.uv = uvs; cube.Transform.localPosition = new Vector3(0, (startPlace), 0); } foreach (var replacedGate in replacedGates.Distinct()) { geometry.Gates.Remove(replacedGate); } if (options.Towers) { foreach (var tower in geometry.Towers) { cube = new Cube("Tower", TownMeshRendererUtils.GetVertices(4, 4, tower.x - 2, tower.y - 2), 0.1f, rendererOptions.TowerMaterial, Walls.transform); cube.Transform.localPosition = Vector3.zero; cube = new Cube("TowerMesh", TownMeshRendererUtils.GetVertices(4, 4, tower.x - 2, tower.y - 2), 8, rendererOptions.TowerMaterial, WallsMesh.transform, false); cube.Transform.localPosition = Vector3.zero; } foreach (var gate in geometry.Gates) { cube = new Cube("Gate", TownMeshRendererUtils.GetVertices(4, 4, gate.x - 2, gate.y - 2), 0.1f, rendererOptions.GateMaterial, Walls.transform); cube.Transform.localPosition = Vector3.zero; cube = new Cube("GateMesh", TownMeshRendererUtils.GetVertices(4, 4, gate.x - 2, gate.y - 2), 6, rendererOptions.GateMaterial, WallsMesh.transform, false); cube.Transform.localPosition = Vector3.zero; } } }
private void DrawOverlay(TownGeometry geometry, StringBuilder sb) { MeshUtils.Polygon poly; List <Vector3> vertices = new List <Vector3>(); var overlays = new GameObject("Overlays"); overlays.gameObject.layer = LayerMask.NameToLayer(CamMapLayer); overlays.transform.parent = child; overlays.transform.localPosition = new Vector3(0, 0, 0); // Vector3.zero; foreach (var patch in geometry.Overlay) { if (patch.Water) { continue; } if (patch.Area.GetType() == typeof(EmptyArea)) { continue; } if (patch.Area.GetType() == typeof(FarmArea)) { continue; // here exclude outlying areas. you could put this back. } var type = patch.Area.ToString(); float previousOffset = 900f; foreach (var vertex in patch.Shape.Vertices) { float MovedX = ScaleToWorldWithOffset(vertex.x, town.townOffset.x); float MovedY = ScaleToWorldWithOffset(vertex.y, town.townOffset.y); // MapMagic.Terrains.TerrainTile tileFound = TownGlobalObjectService.MapMagicObjectRef.tiles.FindByWorldPosition(MovedX, MovedY); try { var thing = TownGlobalObjectService.MapMagicObjectRef.tiles.FindByWorldPosition(vertex.x, vertex.y); Terrain ter = thing.ActiveTerrain; TerrainData terData = ter.terrainData; previousOffset = terData.GetHeight((int)MovedX, (int)MovedY) + 900; vertices.Add(new Vector3( MovedX, previousOffset, MovedY)); } catch // (Exception ex) { if (patch.IsCityCenter) { previousOffset = 0f; } // Debug.Log(ex.Message); vertices.Add(new Vector3( MovedX, previousOffset, MovedY)); } } float offset = 3f; Material mat; if (patch.HasCastle) { // offset = 3.4f; mat = rendererOptions.CastleGroundMaterial; } else if (patch.IsCityCenter) { // offset = 3.2f; mat = rendererOptions.CityCenterGround; } else if (patch.WithinWalls) { if (type == "Town.RichArea") { mat = rendererOptions.RichArea; } else if (type == "Town.PoorArea") { mat = rendererOptions.PoorArea; } else { mat = rendererOptions.WithinWallsGroundMaterial; } } else { if (!options.Farm && type == "Town.EmptyArea") { mat = rendererOptions.HiddenCity; } else if (options.Farm && type == "Town.FarmArea") { // mat = rendererOptions.FarmArea; mat = rendererOptions.HiddenCity; } else { mat = rendererOptions.OuterCityGroundMaterial; } } poly = new MeshUtils.Polygon(patch.Area.GetType().ToString(), vertices, offset, mat, overlays.transform, false); poly.GameObject.layer = LayerMask.NameToLayer(CamMapLayer); poly.Transform.localPosition = Vector3.zero; if (patch.IsCityCenter) { poly.GameObject.isStatic = false; poly.GameObject.AddComponent <LerpToGround>(); } vertices.Clear(); } }
public string DrawTown() { var bounds = _town.GetCityWallsBounds().Expand(100); //var sb = new StringBuilder(@"<?xml version=""1.0"" standalone=""yes""?>"); //sb.Append($"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1500\" height=\"1500\" viewBox=\"{bounds.ToSvgViewport()}\">"); //sb.Append(@"<defs><style type=""text/css""><![CDATA["); //sb.Append(".building { stroke-width: 0.1; stroke: #777; fill: #DDD; }"); //sb.Append(".building-description { visibility: hidden; font-size: 4; font-family: Verdana; fill: #666; }"); //sb.Append(".wall { stroke-width: 5; stroke: black; fill: none; }"); //sb.Append(".road-outer { stroke-width: 3.6; stroke: black; fill: none; }"); //sb.Append(".road-inner { stroke-width: 3; stroke: white; fill: none; }"); //sb.Append(".tower { fill: black; }"); //sb.Append(".water { fill: #7799FF; fill-opacity: 0.6; stroke: none; }"); //sb.Append(".overlay { fill: #FFFF00; fill-opacity: 0.2; stroke: black; stroke-width: 0.5; }"); //sb.Append("]]></style></defs>"); //sb.Append(@"<rect width=""1500"" height=""1500"" x=""0"" y=""0"" fill=""#FCFCFA"" />"); geometry = _town.GetTownGeometry(_options); //foreach (var water in geometry.Water) //{ // sb.Append(water.ToSvgPolygon("water")); //} //var id = 0; foreach (var building in geometry.Buildings) { // Instantiate<GameObject>(Prefab, new Vector3(building.Shape.Center.x, 0, building.Shape.Center.y), Quaternion.identity); // var hover = $"onmouseover =\"document.getElementById('building{id}').style.visibility = 'visible'\" onmouseout=\"document.getElementById('building{id}').style.visibility = 'hidden'\""; // sb.Append(building.Shape.ToSvgPolygon("building", hover)); // id++; } //DrawRoads(geometry, sb); //DrawWalls(geometry, sb); //id = 0; //foreach (var building in geometry.Buildings) //{ // sb.Append("<text z-index=\"9\" id=\"building" + id + "\" class=\"building-description\" x=\"" + building.Shape.Vertices[0].x + "\" y=\"" + building.Shape.Vertices[0].y + // "\">" + building.Description + "</text>"); // id++; //} //if (_options.Overlay) //{ // DrawOverlay(geometry, sb); //} ////sb.Append(geometry.WaterBorder.ToSvgPolygon("road-outer")); //sb.Append($"<text x=\"{bounds.X + 20}\" y=\"{bounds.Y + 30}\">" + _options.Seed + "</text>"); //sb.Append("</svg>"); //return sb.ToString(); return(""); }
private void DrawOverlay(TownGeometry geometry, StringBuilder sb) { MeshUtils.Polygon poly; List <Vector3> vertices = new List <Vector3> (); var overlays = new GameObject("Overlays"); overlays.transform.parent = child; overlays.transform.localPosition = Vector3.zero; foreach (var patch in geometry.Overlay) { if (patch.Water) { continue; } var type = patch.Area.ToString(); foreach (var vertex in patch.Shape.Vertices) { vertices.Add(new Vector3(vertex.x, -3f, vertex.y)); } float offset = 3f; Material mat; if (patch.HasCastle) { offset = 3.4f; mat = rendererOptions.CastleGroundMaterial; } else if (patch.IsCityCenter) { offset = 3.2f; mat = rendererOptions.CityCenterGround; } else if (patch.WithinWalls) { if (type == "Town.RichArea") { mat = rendererOptions.RichArea; } else if (type == "Town.PoorArea") { mat = rendererOptions.PoorArea; } else { mat = rendererOptions.WithinWallsGroundMaterial; } } else { if (type == "Town.FarmArea") { mat = rendererOptions.FarmArea; } else { mat = rendererOptions.OuterCityGroundMaterial; } } poly = new MeshUtils.Polygon("Patch", vertices, offset, mat, overlays.transform, true); poly.Transform.localPosition = Vector3.zero; vertices.Clear(); } }