public override MapPath generate(PathGenerationRequirements pgp) { this.pgp = pgp; mapPath = new MapPath(pgp.middle); //Get build dir Vector2 buildDir = pgp.startPos - pgp.endPos; if (pgp.forceRectangularPaths) { if (Mathf.Abs(buildDir.x) > Mathf.Abs(buildDir.y)) { //Make horizontal buildDir.y = 0; } else { //Make vertical buildDir.x = 0; } } //Add initial point Vector2 buildPos = generatePathPos(mapPath.Start, buildDir); mapPath.addToStart(buildPos, true); //Add middle points Vector2 prevBuildDir = buildDir; for (int i = 0; i < segmentCount - 3; i++) { int safetyEject = 100; do { buildDir = generateNewDirection(prevBuildDir); buildPos = generatePathPos(mapPath.Start, buildDir); safetyEject--; if (safetyEject == 0) { Debug.Log($"Safety eject! buildPos: {buildPos}"); break; } }while (!validPosition(buildPos)); prevBuildDir = buildDir; mapPath.addToStart(buildPos, true); } //Add second to last point mapPath.addToStart(new Vector2(pgp.startPos.x, mapPath.Start.y), true); //Add last point mapPath.addToStart(pgp.startPos, true); //Return return(mapPath); }
public override MapPath generate(PathGenerationRequirements pgp) { //Set generation bounds Bounds bounds = pgp.bounds; bounds.min = new Vector2( bounds.min.x, pgp.startPos.y + bufferY ); bounds.max = new Vector2( bounds.max.x, pgp.middle.y - bufferY ); //Generate points to "collect" List <Vector2> pointList = new List <Vector2>(); for (int i = 0; i < pointCount; i++) { int safetyEject = 100; Vector2 point; do { point = new Vector2( Random.Range(bounds.min.x, bounds.max.x), Random.Range(bounds.min.y, bounds.max.y) ); safetyEject--; if (safetyEject == 0) { Debug.Log($"Safety eject! point: {point}"); break; } }while (pointList.Any( p => Mathf.Abs(p.x - point.x) < minPointDiff || Mathf.Abs(p.y - point.y) < minPointDiff ) || avoidPointList.Any( p => Mathf.Abs(p.x - point.x) < minPointDiff || Mathf.Abs(p.y - point.y) < minPointDiff ) ); //Point is going to cause overlapping parallel path segments, so add it //(or safety eject was used, in which case add it anyway) pointList.Add(point); } //Add start position to points to collect pointList.Add(pgp.startPos); //"Collect" the points with the path MapPath mapPath = new MapPath(pgp.middle); foreach (Vector2 point in pointList) { //Move up/down to point mapPath.addToStart( new Vector2( mapPath.Start.x, point.y ), true ); //Move left/right to point mapPath.addToStart( new Vector2( point.x, mapPath.Start.y ), true ); //ok, point collected, move on to next point } //Ok, all points collected, return path return(mapPath); }