private List <DistrictCell> GenerateDistrictCells(VoronoiDiagram voronoi) { //Create a district cell from the voronoi cells var districtCells = new List <DistrictCell>(); foreach (var cell in voronoi.GetCellsInBounds()) { //ignore cells that are not valid if (cell.Edges.Count < 2) { continue; } districtCells.Add(DistrictCell.FromCell(cell, _citySettings.DistrictSettings[0].Type)); } //tag random cells based on the settings for each district foreach (var setting in _citySettings.DistrictSettings) { for (int i = 0; i < setting.Frequency; ++i) { //Get a random start cell from the voronoi var startCell = districtCells.GetRandomValue(); //size is a ratio of the width and length of the plane var size = setting.Size * ((voronoi.Bounds.Right + voronoi.Bounds.Bottom) / 8); //tag cell districtCells.TagCells(startCell, size, setting.Type); } } return(districtCells); }
/// <summary> /// Build a road inside a district cell /// </summary> public List<Road> BuildRoad(DistrictCell cell, bool generateInnerRoads, int subdivisions) { //Edges are the bounds of the road and will be part of the road as well var edges = cell.Edges.ToList(); //find the longest line in the cell as a start line var longest = FindLongestLineInCell(edges); //a cell edge can be shared so first remove the line from one of the shared cells //all edges of the cell are part of the road var roads = new List<Road>(); foreach (var edge in cell.Edges) { //add the original side edge var road = Road.FromLine(edge); roads.Add(road); } var innerRoads = new List<Road>(); if (generateInnerRoads && subdivisions > 0) { //Create another subdivision roads = GenerateRoad(roads, innerRoads, Road.FromLine(longest.Key), Road.FromLine(longest.Value), subdivisions - 1); } //set up a reference to the parent cell foreach (var road in roads) { road.ParentCell = cell; } return roads; }
public static DistrictCell FromCell(Cell c, string type) { var dc = new DistrictCell(type) { Edges = c.Edges, SitePoint = c.SitePoint, Points = c.Points }; return(dc); }
public static void TagCells(this List<DistrictCell> cells, DistrictCell startCell, double radius,string tag) { //go over all cells foreach (var cell in cells) { //get cell center var cellCenter = cell.SitePoint; //Calculate distance between current cell and the start cell var distance = MathHelpers.DistanceBetweenPoints(startCell.SitePoint, cellCenter); //if it is within range add the cell if (distance < radius) { cell.DistrictType = tag; } } }
public static void TagCells(this List <DistrictCell> cells, DistrictCell startCell, double radius, string tag) { //go over all cells foreach (var cell in cells) { //get cell center var cellCenter = cell.SitePoint; //Calculate distance between current cell and the start cell var distance = MathHelpers.DistanceBetweenPoints(startCell.SitePoint, cellCenter); //if it is within range add the cell if (distance < radius) { cell.DistrictType = tag; } } }
/// <summary> /// Build a road inside a district cell /// </summary> public List <Road> BuildRoad(DistrictCell cell, bool generateInnerRoads, int subdivisions) { //Edges are the bounds of the road and will be part of the road as well var edges = cell.Edges.ToList(); //find the longest line in the cell as a start line var longest = FindLongestLineInCell(edges); //a cell edge can be shared so first remove the line from one of the shared cells //all edges of the cell are part of the road var roads = new List <Road>(); foreach (var edge in cell.Edges) { //add the original side edge var road = Road.FromLine(edge); roads.Add(road); } var innerRoads = new List <Road>(); if (generateInnerRoads && subdivisions > 0) { //Create another subdivision roads = GenerateRoad(roads, innerRoads, Road.FromLine(longest.Key), Road.FromLine(longest.Value), subdivisions - 1); } //set up a reference to the parent cell foreach (var road in roads) { road.ParentCell = cell; } return(roads); }
public static DistrictCell FromCell(Cell c, string type) { var dc = new DistrictCell(type) { Edges = c.Edges, SitePoint = c.SitePoint, Points = c.Points }; return dc; }
/// <summary> /// Generate some additional gameobjects to the inside of a cell /// </summary> private void GenerateObjectsInCell(DistrictCell cell) { //using an inset create a smaller cell from the cell //this is to avoid spawning objects on buildings or roads var insetCell = cell.Inset(_terrainSettings.RoadWidth/2); var amount = _terrainSettings.AdditionalProps; //Generate the spawn points inside the cell var points = insetCell.GenerateRandomPoints(Random.Range(amount - 5, amount + 5)); foreach (var p in points) { //create a 3D vector from the 2D point var position = p.ToVector3(); //randomize what will spawn var r = Random.value; //spawn a tree if (r < 0.75) { SpawnTree(position); } else if(r < 0.85f) //Spawn a prop { SpawnProp(position); } else { //nothing } } }