public void Update() { if (drawGraph && m_Graph != null) { Debug.DrawLine(new Vector2(left, top), new Vector2(right, top), Color.blue); Debug.DrawLine(new Vector2(right, top), new Vector2(right, bottom), Color.blue); Debug.DrawLine(new Vector2(right, bottom), new Vector2(left, bottom), Color.blue); Debug.DrawLine(new Vector2(left, bottom), new Vector2(left, top), Color.blue); CityGraph.Edge[] graphEdges = m_Graph.GetEdges(); for (int i = 0; i < graphEdges.Length; i++) { Debug.DrawLine( graphEdges[i].pointA.position.ConvertToWorldPoint(m_PointSpacing), graphEdges [i].pointB.position.ConvertToWorldPoint(m_PointSpacing), Color.red); } } if (iterations > m_LastIterations) { int newIterations = iterations - m_LastIterations; m_LastIterations = iterations; m_Graph = BuildRRT(m_Graph, newIterations, increment); } }
private CityGraph AddBuildingsToCity(CityGraph currentCity, int lotsToPlace) { //Sort Edges by distance from the center in travel time. List <CityGraph.Edge> allEdges = new List <CityGraph.Edge>(currentCity.GetEdges()); allEdges.Sort(CompareEdges); //place lots on edges (left or right) //Lots are made up of at least 4 points. float totalLotsToPlace = lotsToPlace; for (int i = 0; i < allEdges.Count && lotsToPlace > 0; i++) { if (Random.value < (1 - populationDensity)) { continue; } if (allEdges [i].NeighbouringLotsCount < 2) { GridPoint point1 = allEdges [i].pointA.position; GridPoint point2 = allEdges [i].pointB.position; Vector2 point1Vec = point1.ConvertToWorldPoint(m_PointSpacing); Vector2 point2Vec = point2.ConvertToWorldPoint(m_PointSpacing); Vector2 edgeVector = point2Vec - point1Vec; Vector2 rhNormal = new Vector2(edgeVector.y, -edgeVector.x); Vector2 lhNormal = new Vector2(-edgeVector.y, edgeVector.x); CityGraph.Lot rhLot = new CityGraph.Lot(point1, point2, ConvertToGridPoint(point1Vec + rhNormal), ConvertToGridPoint(point2Vec + rhNormal)); CityGraph.Lot lhLot = new CityGraph.Lot(point1, point2, ConvertToGridPoint(point1Vec + lhNormal), ConvertToGridPoint(point2Vec + lhNormal)); bool coinFlip = Random.value > 0.5f; CityGraph.Lot lotToAdd = rhLot; if (coinFlip == true) { lotToAdd = lhLot; } bool success = currentCity.AddLot(allEdges [i], lotToAdd); if (!success) { lotToAdd = coinFlip ? rhLot : lhLot; success = currentCity.AddLot(allEdges [i], lotToAdd); } if (success) { lotsToPlace--; } } } return(currentCity); }
public List <Collider2D> DrawRoads(CityGraph graph, float pointSpacing) { ClearLastObjects(); List <Collider2D> newObstructions = new List <Collider2D> (); CityGraph.Edge[] graphEdges = graph.GetEdges(); for (int i = 0; i < graphEdges.Length; i++) { GameObject segment = Instantiate <GameObject> (roadSegment); m_Roads.Add(segment); LineRenderer segmentRederer = segment.GetComponent <LineRenderer> (); Vector3[] positions = new Vector3[2]; positions [0] = graphEdges [i].pointA.position.ConvertToWorldPoint(pointSpacing); positions [1] = graphEdges [i].pointB.position.ConvertToWorldPoint(pointSpacing); segmentRederer.SetPositions(positions); segmentRederer.widthMultiplier *= Mathf.Min(Vector3.Distance(positions [0], positions [1]), 2); } CityGraph.Lot[] lots = graph.GetLots(); for (int i = 0; i < lots.Length; i++) { GameObject building = Instantiate <GameObject> (buildingPrefab); m_Buildings.Add(building); Vector2 upVector = lots [i].corners [1].ConvertToWorldPoint(pointSpacing) - lots [i].corners [0].ConvertToWorldPoint(pointSpacing); Vector2 rightVector = lots [i].corners [2].ConvertToWorldPoint(pointSpacing) - lots [i].corners [1].ConvertToWorldPoint(pointSpacing); float width = Mathf.Min(maxBuildingWidth, rightVector.magnitude) * 0.9f; float depth = Mathf.Min(maxBuildingDepth, upVector.magnitude) * 0.9f; float height = (width + depth) / 2; building.transform.localScale = new Vector3(depth, width, height); float x = Vector2.Dot(rightVector, Vector2.right); float y = Vector2.Dot(rightVector, Vector2.up); float angle = Mathf.Rad2Deg * Mathf.Acos(x / Mathf.Max(rightVector.magnitude, 0.01f)); if (y < 0) { angle *= -1; } building.transform.localEulerAngles = new Vector3(0, 0, angle); Vector3 position = (Vector3)lots [i].GetCenter(pointSpacing); position.z = -height / 2; building.transform.localPosition = position; newObstructions.Add(building.GetComponent <Collider2D> ()); } return(newObstructions); }