// Returns a list of cells for possible next highway leg public List <Vector2> CreateHighwayLeg(Vector2 currentCoord, HighwayDirections currentDirection, List <Vector2> pendingHighway) { List <Vector2> highwayLeg = new List <Vector2>(); int numCells = 0; //GlobalLogger.Log(string.Format("Leg start point: {0}", currentCoord.ToString())); while (numCells < 20 && currentCoord.X > -1 && currentCoord.X < 120 && currentCoord.Y > -1 && currentCoord.Y < 160 && !Cells[(int)currentCoord.X, (int)currentCoord.Y].IsHighway && (numCells == 0 || !pendingHighway.Contains(currentCoord))) { highwayLeg.Add(currentCoord); numCells++; currentCoord = GetNextCell(currentCoord, currentDirection); } //GlobalLogger.Log(string.Format("Leg end point: {0}", currentCoord.ToString())); return(highwayLeg); }
public Vector2 GetNextCell(Vector2 start, HighwayDirections dir) { switch (dir) { case HighwayDirections.DOWN: return(new Vector2(start.X + 1, start.Y)); case HighwayDirections.LEFT: return(new Vector2(start.X, start.Y - 1)); case HighwayDirections.RIGHT: return(new Vector2(start.X, start.Y + 1)); case HighwayDirections.UP: return(new Vector2(start.X - 1, start.Y)); default: return(default(Vector2)); } }
// Returns true if entire highway is successfully created public bool CreateHighway(Vector2 startCoord, HighwayDirections startDirection) { Random random = new Random(System.Guid.NewGuid().GetHashCode()); List <Vector2> totalHighway = new List <Vector2>(); List <Vector2> highwayPart = null; while ((highwayPart = CreateHighwayLeg(startCoord, startDirection, totalHighway)).Count == 20 && !IsBoundaryCell(highwayPart.Last())) { // Add vector 2 to potential highway cell list totalHighway.AddRange(highwayPart); //GlobalLogger.Log(string.Format("Creating new highway leg: Current highway length: {0}", totalHighway.Count)); // Get new startCoord and startDirection double newDirProbability = random.NextDouble(); //GlobalLogger.Log(string.Format("Random number chosen for new highway direction: {0}", newDirProbability)); startCoord = totalHighway.Last(); switch (startDirection) { case HighwayDirections.DOWN: case HighwayDirections.UP: if (newDirProbability < 0.19) { //GlobalLogger.Log("Turning Left"); startDirection = HighwayDirections.LEFT; //startCoord = GetNextCell(startCoord, HighwayDirections.LEFT); } else if (newDirProbability < 0.39) { //GlobalLogger.Log("Turning Right"); startDirection = HighwayDirections.RIGHT; //startCoord = GetNextCell(startCoord, HighwayDirections.RIGHT); } else { //GlobalLogger.Log("Maintaining Direction"); //startCoord = GetNextCell(startCoord, startDirection); } break; case HighwayDirections.LEFT: case HighwayDirections.RIGHT: if (newDirProbability < 0.19) { //GlobalLogger.Log("Turning Up"); startDirection = HighwayDirections.UP; //startCoord = GetNextCell(startCoord, HighwayDirections.UP); } else if (newDirProbability < 0.39) { //GlobalLogger.Log("Turning Down"); startDirection = HighwayDirections.DOWN; //startCoord = GetNextCell(startCoord, HighwayDirections.DOWN); } else { //GlobalLogger.Log("Maintaining Direction"); //startCoord = GetNextCell(startCoord, startDirection); } break; } } // Add cells that 'hit' another boundary totalHighway.AddRange(highwayPart); if (totalHighway.Count >= 100 && IsBoundaryCell(totalHighway.Last())) { //GlobalLogger.Log("Highway leg creation successful"); // Iterate through all Vector2 in totalHighway and mark as highway foreach (Vector2 cell in totalHighway) { Cells[(int)cell.X, (int)cell.Y].IsHighway = true; } return(true); } else { //GlobalLogger.Log("Highway leg creation stopped"); return(false); } }