//builds a street from the center of a large unit to the edge in the given direction private void buildMidCurve(TULarge lu, Vector2 outsideConPoint, Dir dir, TransportUnit powMid, PSide side) //powmid is the mid unit that contains the large's conpoint { int gix = 0, giy = 0; //goal index x and y of the goal mid unit float outSlope = GridMath.findSlope(lu.conPoint, outsideConPoint); //find the average slope from this conPoint to the outsideConPoint Debug.DrawLine(UnitConverter.getWP(new SurfacePos(PSide.TOP, lu.conPoint.x, lu.conPoint.y), 10000, sideLength), UnitConverter.getWP(new SurfacePos(PSide.TOP, outsideConPoint.x, outsideConPoint.y), 10000, sideLength), Color.blue, Mathf.Infinity); //determine the goal mid unit if (dir == Dir.RIGHT) { //find the index of the goal mid unit float goalPosX = (lu.indexI + 1) * sideLengthLarge; //the x value of the very right side of the large unit in mid units float goalPosY = GridMath.findY(goalPosX, lu.conPoint, outSlope); //the y value on the line between teh two conpoints GridMath.findMidIndexfromPoint(new Vector2(goalPosX - 0.5f, goalPosY), midTUWidth, out gix, out giy); TransportUnit goalMid = buildMid(gix, giy, side); goalMid.conRight = true; goalMid.RightLev = 1; //Debug.DrawLine(UnitConverter.getWP(new SurfacePos(PSide.TOP, lu.conPoint.x, lu.conPoint.y), 10000, 1024), // UnitConverter.getWP(new SurfacePos(PSide.TOP, goalPosX, goalPosY), 10000, 1024), Color.blue, Mathf.Infinity); } else if (dir == Dir.LEFT) { //find the index of the goal mid unit float goalPosX = (lu.indexI) * sideLengthLarge; //the x value of the very right side of the large unit in mid units float goalPosY = GridMath.findY(goalPosX, lu.conPoint, outSlope); //the y value on the line between teh two conpoints GridMath.findMidIndexfromPoint(new Vector2(goalPosX + 0.5f, goalPosY), midTUWidth, out gix, out giy); //buildMid(gix, giy, side).conLeft=true; } else if (dir == Dir.UP) { //find the index of the goal mid unit float goalPosY = (lu.indexJ + 1) * sideLengthLarge; //the x value of the very right side of the large unit in mid units float goalPosX = GridMath.findX(goalPosY, lu.conPoint, outSlope); //the y value on the line between teh two conpoints GridMath.findMidIndexfromPoint(new Vector2(goalPosX, goalPosY - 0.5f), midTUWidth, out gix, out giy); TransportUnit goalMid = buildMid(gix, giy, side); goalMid.conUp = true; goalMid.UpLev = 1; } else if (dir == Dir.DOWN) { //find the index of the goal mid unit float goalPosY = (lu.indexJ) * sideLengthLarge; //the x value of the very right side of the large unit in mid units float goalPosX = GridMath.findX(goalPosY, lu.conPoint, outSlope); //the y value on the line between teh two conpoints GridMath.findMidIndexfromPoint(new Vector2(goalPosX, goalPosY + 0.5f), midTUWidth, out gix, out giy); } //the x and y index of the mid unit last created in the loop(starts as if pows were last created) //int lastix = powIndexX, lastiy = powIndexY; TransportUnit lastMid = powMid; while (true) { //the difference in indexes between the current and goal mid units int xdif = gix - lastMid.indexI; int ydif = giy - lastMid.indexJ; //the direction to move in this loop iteration(1=x 2=y) int movedir; //if they are both not on the goal index, pick a random one to change if (xdif != 0 && ydif != 0) { movedir = lu.rng.NextDouble() > 0.5 ? 1:2; } else if (xdif != 0) { movedir = 1; } else if (ydif != 0) { movedir = 2; } else //if they are both zero we are done maybe? { break; //? } //the index of the mid unit to be created int curix = lastMid.indexI, curiy = lastMid.indexJ; //if moving in the x direction if (movedir == 1) { if (xdif > 0) { curix++; } else { curix--; } } else //movedir==2 { if (ydif > 0) { curiy++; } else { curiy--; } } //create or retrieve the new mid unit TransportUnit curMid = buildMid(curix, curiy, side); //set its conpoint if it has not already been set curMid.setConPoint(new Vector2((curix + (float)lu.rng.NextDouble()) * midTUWidth, (curiy + (float)lu.rng.NextDouble()) * midTUWidth)); //MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, curMid.conPoint.x, curMid.conPoint.y), 10000, 1024), 5); connectUnits(curMid, lastMid, 1); lastMid = curMid; } }
//builds a level 2 street of length from startMid within curLarge transport unit private void buildLev2(TULarge curLarge, TransportUnit startMid, int length, PSide side) { //the max and min indexes that the street can be built to(stay within the large unit) //move out of this function so it is not recalculated every time int maxI = (curLarge.indexI + 1) * largeTUWidth - 1; int minI = (curLarge.indexI) * largeTUWidth; int maxJ = (curLarge.indexJ + 1) * largeTUWidth - 1; int minJ = (curLarge.indexJ) * largeTUWidth; //the last mid unit to build away from TransportUnit lastMid = startMid; Dir lastDir; for (int i = 0; i < length; i++) { //the direction to build //NOTE: modify later to not build back from the same direction Dir dir = (Dir)(curLarge.rng.Next(1, 5)); //the index of the new mid unit to modify int curix = lastMid.indexI; int curiy = lastMid.indexJ; if (dir == Dir.RIGHT) { curix++; if (curix > maxI) { lastMid.conRight = true; lastMid.RightLev = 2; break; } } if (dir == Dir.LEFT) { curix--; if (curix < minI) { break; } } if (dir == Dir.UP) { curiy++; if (curiy > maxJ) { lastMid.conUp = true; lastMid.UpLev = 2; break; } } if (dir == Dir.DOWN) { curiy--; if (curiy < minJ) { break; } } //build (or just retrieve) the mid unit at the new indexes TransportUnit curMid = buildMid(curix, curiy, side); //set its conpoint if it has not already been set curMid.setConPoint(new Vector2((curix + (float)curLarge.rng.NextDouble()) * midTUWidth, (curiy + (float)curLarge.rng.NextDouble()) * midTUWidth)); //MyDebug.placeMarker(UnitConverter.getWP(new SurfacePos(PSide.TOP, curMid.conPoint.x, curMid.conPoint.y), 10000, 1024), 10); //connect on level 2 connectUnits(curMid, lastMid, 2); lastMid = curMid; } }