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 void connectJunctions(Vector2 junctionIndex, ref List <Junction> junctions, ref JunctionIndexer indexer, ref List <AreaSet> areaSet) { areaGenerator.checkNeighbor(junctions[junctions.Count - 1].areas[0], junctionIndex, ref junctions, ref indexer, ref areaSet); areaGenerator.checkNeighbor(junctions[junctions.Count - 1].areas[1], junctionIndex, ref junctions, ref indexer, ref areaSet); areaGenerator.checkNeighbor(junctions[junctions.Count - 1].areas[2], junctionIndex, ref junctions, ref indexer, ref areaSet); areaGenerator.checkNeighbor(junctions[junctions.Count - 1].areas[3], junctionIndex, ref junctions, ref indexer, ref areaSet); }
//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 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 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; }
private bool checkByDirection(Vector2 junctionIndex, int direction, Area area, int targetPosition, ref List <Junction> junctions, ref JunctionIndexer indexer, ref List <AreaSet> areaSet) { bool isMerged = false; int neighborIndex = indexer.getIndex((directions[direction] + junctionIndex)); if (indexer.getIndex((directions[direction] + junctionIndex)) >= 0) { int index = junctions[neighborIndex].areas[targetPosition].areaSetIndex; if (index >= 0) { areaSet[index].addArea(area); area.setAreaSetIndex(index); isMerged = true; } else { Debug.Log("ERROR: World - connectJunctions - Negative Index error"); } } return(isMerged); }
public void checkNeighbor(Area area, Vector2 junctionIndex, ref List <Junction> junctions, ref JunctionIndexer indexer, ref List <AreaSet> areaSet) { if (area.areaPosition == AreaPosition.NE) { if (!checkByDirection(junctionIndex, (int)AbsDirection.N, area, (int)AreaPosition.SE, ref junctions, ref indexer, ref areaSet) && !checkByDirection(junctionIndex, (int)AbsDirection.E, area, (int)AreaPosition.NW, ref junctions, ref indexer, ref areaSet)) { createNewAreaSet(area, ref areaSet); } } else if (area.areaPosition == AreaPosition.NW) { if (!checkByDirection(junctionIndex, (int)AbsDirection.N, area, (int)AreaPosition.SW, ref junctions, ref indexer, ref areaSet) && !checkByDirection(junctionIndex, (int)AbsDirection.W, area, (int)AreaPosition.NE, ref junctions, ref indexer, ref areaSet)) { createNewAreaSet(area, ref areaSet); } } else if (area.areaPosition == AreaPosition.SE) { if (!checkByDirection(junctionIndex, (int)AbsDirection.S, area, (int)AreaPosition.NE, ref junctions, ref indexer, ref areaSet) && !checkByDirection(junctionIndex, (int)AbsDirection.E, area, (int)AreaPosition.SW, ref junctions, ref indexer, ref areaSet)) { createNewAreaSet(area, ref areaSet); } } else if (area.areaPosition == AreaPosition.SW) { if (!checkByDirection(junctionIndex, (int)AbsDirection.S, area, (int)AreaPosition.NW, ref junctions, ref indexer, ref areaSet) && !checkByDirection(junctionIndex, (int)AbsDirection.W, area, (int)AreaPosition.SE, ref junctions, ref indexer, ref areaSet)) { createNewAreaSet(area, ref areaSet); } } }
public void connectByRoad(Junction source, AbsDirection connectDirection, GameObject roadParent, ref List <Junction> roadEnds, ref List <Road> roads, ref List <Junction> junctions, ref JunctionIndexer indexer, ref JunctionGenerator junctionGenerator) { int coDirection = (int)connectDirection; int opDirection = ((int)connectDirection + 2) % 4; if (indexer.getIndex(source.coordIndex + directions[coDirection]) >= 0) { Junction target = junctions[indexer.getIndex(source.coordIndex + directions[coDirection])]; //Debug.Log(source.ToString() + "\t" + target.ToString() + "\r" + source.connectRoadIdx[coDirection] + "\t" + target.connectRoadIdx[opDirection]); if (source.connectRoadIdx[coDirection] < 0 && target.connectRoadIdx[opDirection] < 0) { createRoads(source, target, ref roads, roadParent); } else if (source.connectRoadIdx[coDirection] < 0) { source.connectRoadIdx[coDirection] = target.connectRoadIdx[opDirection]; } else if (target.connectRoadIdx[opDirection] < 0) { target.connectRoadIdx[opDirection] = source.connectRoadIdx[coDirection]; } } else { junctionGenerator.createNullJunction(source.coordIndex + directions[coDirection], source, (AbsDirection)coDirection, ref roadEnds); createRoads(source, roadEnds[roadEnds.Count - 1], ref roads, roadParent); } }