private Vector2 getNextCenter(Junction junction, JunctionSize newSize, int hrzWeight, int vtcWeight) { int prvWidth = junction.size.numOfVtcLane + junction.size.lenOfHrzLane; int prvHeight = junction.size.numOfHrzLane + junction.size.lenOfVtcLane; return(new Vector2(junction.center.x + hrzWeight * (prvWidth + newSize.numOfVtcLane + newSize.lenOfHrzLane), junction.center.y + vtcWeight * (prvHeight + newSize.numOfHrzLane + newSize.lenOfVtcLane))); }
public void initialize(Vector2 givenCoordIndex, Vector2 givenCenter, JunctionSize givenSize, ref List <Block> sidewalks) { initialize(givenCoordIndex, givenCenter); isNullJunction = false; size = givenSize; buildJunction(ref sidewalks); }
public void createJunction(Vector2 junctionCenter, JunctionSize junctionSize, Vector2 junctionIndex, ref List <Junction> junctions, ref JunctionIndexer indexer, ref List <AreaSet> areaSet, ref List <Block> sidewalks) { junctions.Add(Object.Instantiate((Resources.Load("Prefab/DummyObjects/DummyJunction") as GameObject).GetComponent <Junction>(), Vector3.zero, Quaternion.identity) as SCPAR.SIM.PVATestbed.Junction); junctions[junctions.Count - 1].initialize(junctionIndex, junctionCenter, junctionSize, ref sidewalks); junctions[junctions.Count - 1].transform.parent = GameObject.Find("World").transform; junctions[junctions.Count - 1].transform.name = "Junction" + (junctions.Count - 1).ToString(); indexer.setIndex((int)junctionIndex.x, (int)junctionIndex.y, junctions.Count - 1); connectJunctions(junctionIndex, ref junctions, ref indexer, ref areaSet); }
public JunctionSize setRandomParam(JunctionSize givenSize) { JunctionSize size = new JunctionSize(); if (givenSize.numOfHrzLane == 0) { //size.numOfHrzLane = (int)Mathf.Round(Random.Range(1.0f, 3.0f)); size.numOfHrzLane = 2; } else { size.numOfHrzLane = givenSize.numOfHrzLane; } if (givenSize.numOfVtcLane == 0) { //size.numOfVtcLane = (int)Mathf.Round(Random.Range(1.0f, 3.0f)); size.numOfVtcLane = 2; } else { size.numOfVtcLane = givenSize.numOfVtcLane; } if (givenSize.lenOfHrzLane == 0) { size.lenOfHrzLane = 14;//(int)Mathf.Round(Random.Range(size.numOfHrzLane * 2 + 4.0f, 15.0f)) * 2; } else { size.lenOfHrzLane = givenSize.lenOfHrzLane; } if (givenSize.lenOfVtcLane == 0) { size.lenOfVtcLane = 14; //(int)Mathf.Round(Random.Range(size.numOfVtcLane * 2 + 4.0f, 15.0f)) * 2; } else { size.lenOfVtcLane = givenSize.lenOfVtcLane; } return(size); }
public void createMap(int loopNum) { // generate junctions Vector2 center = new Vector2(0, 0); Vector2 junctionIndex = Vector2.zero; JunctionSize size = new JunctionSize(); int dirIndex = 0; indexer = new JunctionIndexer(loopNum); roads = new List <Road>(); sidewalks = new List <Block>(); roadGenerator = new RoadGenerator(); junctionGenerator = new JunctionGenerator(); // generate junction size = junctionGenerator.setRandomParam(size); junctionGenerator.createJunction(center, size, junctionIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks); for (int i = 1; i < loopNum; i++) { junctionGenerator.setAndCreateJunction(i, ref size, ref center, ref junctionIndex, ref dirIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks); junctionGenerator.setAndCreateJunction(i, ref size, ref center, ref junctionIndex, ref dirIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks); if (i == loopNum - 1) { junctionGenerator.setAndCreateJunction(i, ref size, ref center, ref junctionIndex, ref dirIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks); } } // link junctions using road linkJunctions(GameObject.Find("Road")); // generate area if (buildingGenerator == null) { buildingGenerator = UnityEngine.Object.Instantiate((Resources.Load("Prefab/DummyObjects/BuildingGenerator") as GameObject).GetComponent <BuildingGenerator>(), transform); } buildingGenerator.generate(areaSet); isReady = true; }
//private JunctionSize setNextSize(Junction junction, int hrzWeight, int vtcWeight) private JunctionSize getNextSize(Vector2 newJunctionIndex, ref List <Junction> junctions, ref JunctionIndexer indexer) { int hrzWeight = 0; int vtcWeight = 0; JunctionSize size = new JunctionSize(); if (indexer.getIndex((int)newJunctionIndex.x + 1, (int)newJunctionIndex.y) >= 0) { //Debug.Log(indexer.getIndex((int)newJunctionIndex.x + 1, (int)newJunctionIndex.y)); size.lenOfVtcLane = junctions[indexer.getIndex((int)newJunctionIndex.x + 1, (int)newJunctionIndex.y)].size.lenOfVtcLane; size.numOfHrzLane = junctions[indexer.getIndex((int)newJunctionIndex.x + 1, (int)newJunctionIndex.y)].size.numOfHrzLane; hrzWeight = 1; } if (indexer.getIndex((int)newJunctionIndex.x - 1, (int)newJunctionIndex.y) >= 0) { //Debug.Log(indexer.getIndex((int)newJunctionIndex.x - 1, (int)newJunctionIndex.y)); size.lenOfVtcLane = junctions[indexer.getIndex((int)newJunctionIndex.x - 1, (int)newJunctionIndex.y)].size.lenOfVtcLane; size.numOfHrzLane = junctions[indexer.getIndex((int)newJunctionIndex.x - 1, (int)newJunctionIndex.y)].size.numOfHrzLane; hrzWeight = -1; } if (indexer.getIndex((int)newJunctionIndex.x, (int)newJunctionIndex.y + 1) >= 0) { //Debug.Log(indexer.getIndex((int)newJunctionIndex.x, (int)newJunctionIndex.y + 1)); size.lenOfHrzLane = junctions[indexer.getIndex((int)newJunctionIndex.x, (int)newJunctionIndex.y + 1)].size.lenOfHrzLane; size.numOfVtcLane = junctions[indexer.getIndex((int)newJunctionIndex.x, (int)newJunctionIndex.y + 1)].size.numOfVtcLane; vtcWeight = 1; } if (indexer.getIndex((int)newJunctionIndex.x, (int)newJunctionIndex.y - 1) >= 0) { //Debug.Log(indexer.getIndex((int)newJunctionIndex.x, (int)newJunctionIndex.y - 1)); size.lenOfHrzLane = junctions[indexer.getIndex((int)newJunctionIndex.x, (int)newJunctionIndex.y - 1)].size.lenOfHrzLane; size.numOfVtcLane = junctions[indexer.getIndex((int)newJunctionIndex.x, (int)newJunctionIndex.y - 1)].size.numOfVtcLane; vtcWeight = -1; } int hW = Mathf.Abs(hrzWeight); int vW = Mathf.Abs(vtcWeight); return(new JunctionSize(hW * size.numOfHrzLane, vW * size.numOfVtcLane, vW * size.lenOfHrzLane, hW * size.lenOfVtcLane)); }
public void setAndCreateJunction(int repetition, ref JunctionSize size, ref Vector2 center, ref Vector2 junctionIndex, ref int dirIndex, ref List <Junction> junctions, ref JunctionIndexer indexer, ref List <AreaSet> areaSet, ref List <Block> sidewalks) { Vector2 direction = directions[dirIndex]; for (int k = 0; k < repetition; k++) { Vector2 newJunctionIndex = junctionIndex + direction; if (junctions.Count > 0) { size = getNextSize(newJunctionIndex, ref junctions, ref indexer);//junctions[junctions.Count - 1], (int)direction.x, (int)direction.y); } size = setRandomParam(size); if (junctions.Count > 0) { center = getNextCenter(junctions[junctions.Count - 1], size, (int)direction.x, (int)direction.y); } junctionIndex = newJunctionIndex; createJunction(center, size, junctionIndex, ref junctions, ref indexer, ref areaSet, ref sidewalks); } dirIndex = (dirIndex + 1) % directions.Count; }
public void initialize(Junction givenSource, Junction givenTarget) { lanes = new List <Lane>(); lines = new List <Line>(); source = givenSource; target = givenTarget; direction = Util.vec22AbsDirection(givenTarget.coordIndex - givenSource.coordIndex); this.isHorizontal = (direction == AbsDirection.N || direction == AbsDirection.S) ? false : true; if (givenSource.isNullJunction || givenTarget.isNullJunction) { Vector2 center = givenSource.isNullJunction ? givenTarget.center : givenSource.center; JunctionSize size = givenSource.isNullJunction ? givenTarget.size : givenSource.size; center = source.center; if (isHorizontal) { int startI = source.coordIndex.x < target.coordIndex.x ? -size.numOfHrzLane : 0; length = size.lenOfHrzLane * SimParameter.unitBlockSize; numOfLanes = size.numOfHrzLane; for (int i = 0; i < size.numOfHrzLane; i++) { if (direction == AbsDirection.E) { createLane(center, size.numOfVtcLane, (size.lenOfHrzLane + size.numOfVtcLane), startI + i, i, direction, isHorizontal); } else if (direction == AbsDirection.W) { createLane(center, -(size.lenOfHrzLane + size.numOfVtcLane), -size.numOfVtcLane, startI + i, i, direction, isHorizontal); } if (startI + i == 0) { createRoadLine(center, -(size.lenOfHrzLane + size.numOfVtcLane), -size.numOfVtcLane, startI + i, isHorizontal); } if (i == 0) { continue; } if (direction == AbsDirection.E) { createRoadLine(center, size.numOfVtcLane, (size.lenOfHrzLane + size.numOfVtcLane), startI + i, isHorizontal); } else if (direction == AbsDirection.W) { createRoadLine(center, -(size.lenOfHrzLane + size.numOfVtcLane), -size.numOfVtcLane, startI + i, isHorizontal); } } } else { int startI = source.coordIndex.y > target.coordIndex.y ? -size.numOfVtcLane : 0; length = size.lenOfVtcLane * SimParameter.unitBlockSize; numOfLanes = size.numOfVtcLane; for (int i = 0; i < size.numOfVtcLane; i++) { if (direction == AbsDirection.N) { createLane(center, size.numOfHrzLane, (size.lenOfVtcLane + size.numOfHrzLane), startI + i, i, direction, isHorizontal); } else if (direction == AbsDirection.S) { createLane(center, -(size.lenOfVtcLane + size.numOfHrzLane), -size.numOfHrzLane, startI + i, i, direction, isHorizontal); } if (startI + i == 0) { createRoadLine(center, size.numOfHrzLane, (size.lenOfVtcLane + size.numOfHrzLane), startI + i, isHorizontal); } if (i == 0) { continue; } if (direction == AbsDirection.N) { createRoadLine(center, size.numOfHrzLane, (size.lenOfVtcLane + size.numOfHrzLane), startI + i, isHorizontal); } else if (direction == AbsDirection.S) { createRoadLine(center, -(size.lenOfVtcLane + size.numOfHrzLane), -size.numOfHrzLane, startI + i, isHorizontal); } } } } else { if (isHorizontal) { if (source.size.numOfHrzLane != target.size.numOfHrzLane) { Debug.Log("ERROR: Road - initialize - numOfHrzLane is not matched."); } int numOfHrzLane = source.size.numOfHrzLane; int startI = direction == AbsDirection.E ? -numOfHrzLane : 0; length = (source.size.lenOfHrzLane + target.size.lenOfHrzLane) * SimParameter.unitBlockSize; numOfLanes = numOfHrzLane; for (int i = 0; i < numOfHrzLane; i++) { if (source.center.x < target.center.x) { createLane(source.center, source.size.numOfVtcLane, source.size.numOfVtcLane + (source.size.lenOfHrzLane + target.size.lenOfHrzLane), startI + i, i, direction, isHorizontal); } else { createLane(target.center, target.size.numOfVtcLane, target.size.numOfVtcLane + (target.size.lenOfHrzLane + source.size.lenOfHrzLane), startI + i, i, direction, isHorizontal); } if (startI + i == 0) { createRoadLine(target.center, target.size.numOfVtcLane, target.size.numOfVtcLane + (target.size.lenOfHrzLane + source.size.lenOfHrzLane), startI + i, isHorizontal); } if (i == 0) { continue; } if (source.center.x < target.center.x) { createRoadLine(source.center, source.size.numOfVtcLane, source.size.numOfVtcLane + (source.size.lenOfHrzLane + target.size.lenOfHrzLane), startI + i, isHorizontal); } else { createRoadLine(target.center, target.size.numOfVtcLane, target.size.numOfVtcLane + (target.size.lenOfHrzLane + source.size.lenOfHrzLane), startI + i, isHorizontal); } } } else { if (source.size.numOfVtcLane != target.size.numOfVtcLane) { Debug.Log("ERROR: Road - initialize - numOfVtcLane is not matched."); } int numOfVtcLane = source.size.numOfVtcLane; int startI = direction == AbsDirection.S ? -numOfVtcLane : 0; length = (source.size.lenOfVtcLane + target.size.lenOfVtcLane) * SimParameter.unitBlockSize; numOfLanes = numOfVtcLane; for (int i = 0; i < numOfVtcLane; i++) { if (source.center.y < target.center.y) { createLane(source.center, source.size.numOfHrzLane, source.size.numOfHrzLane + (source.size.lenOfVtcLane + target.size.lenOfVtcLane), startI + i, i, direction, isHorizontal); } else { createLane(target.center, target.size.numOfHrzLane, target.size.numOfHrzLane + (target.size.lenOfVtcLane + source.size.lenOfVtcLane), startI + i, i, direction, isHorizontal); } if (startI + i == 0) { createRoadLine(source.center, source.size.numOfHrzLane, source.size.numOfHrzLane + (source.size.lenOfVtcLane + target.size.lenOfVtcLane), startI + i, isHorizontal); } if (i == 0) { continue; } if (source.center.y < target.center.y) { createRoadLine(source.center, source.size.numOfHrzLane, source.size.numOfHrzLane + (source.size.lenOfVtcLane + target.size.lenOfVtcLane), startI + i, isHorizontal); } else { createRoadLine(target.center, target.size.numOfHrzLane, target.size.numOfHrzLane + (target.size.lenOfVtcLane + source.size.lenOfVtcLane), startI + i, isHorizontal); } } } } if (direction == AbsDirection.E || direction == AbsDirection.S) { lanes.Reverse(); } }