private bool CreateMap(int segmentWidth, int segmentHeight) { segments = new MapSegment[segmentWidth, segmentHeight]; map.Initialize(segmentWidth * MapSegment.SegmentCellWidthHeight, segmentHeight * MapSegment.SegmentCellWidthHeight); do { startXsegment = Game.Random.Next(0, segmentWidth - 1); startYsegment = Game.Random.Next(0, segmentHeight - 1); }while (segments[startXsegment, startYsegment] != null); segmentsVisited.Clear(); MapSegment segment = GetConstrainedSegment(segments, new Point(startXsegment, startYsegment)); if (segment == null || segment.ID == 0) { return(false); } segment.x = startXsegment; segment.y = startYsegment; segments[startXsegment, startYsegment] = segment; segmentsVisited.Add(segment); while (segmentsVisited.Count > 0) { MapSegment visitedSegment = segmentsVisited[segmentsVisited.Count - 1]; pointsToBeVisited.Clear(); for (int p = 0; p < neighbors.Length; p++) { Point pointToVisit = new Point(visitedSegment.x + neighbors[p].X, visitedSegment.y + neighbors[p].Y); if (pointToVisit.X >= 0 && pointToVisit.X < segmentWidth && pointToVisit.Y >= 0 && pointToVisit.Y < segmentHeight) { if (segments[pointToVisit.X, pointToVisit.Y] == null) { if (visitedSegment.EdgeIsOpen(directions[p])) { pointsToBeVisited.Add(pointToVisit); } } } } if (pointsToBeVisited.Count > 0) { Point point; int index = Game.Random.Next(0, pointsToBeVisited.Count - 1); point = pointsToBeVisited[index]; segment = GetConstrainedSegment(segments, point); if (segment == null) { return(false); } segment.x = point.X; segment.y = point.Y; segments[point.X, point.Y] = segment; segmentsVisited.Add(segment); //Debug.Log("Created Segment " + segment.ID + " at " + point.X + ", " + point.Y); } else { segmentsVisited.Remove(visitedSegment); } } emptySegments = 0; smallSegments = 0; for (int y = 0; y < segmentHeight; y++) { for (int x = 0; x < segmentWidth; x++) { if (segments[x, y] == null) // || segments[x, y].ID == 0) { segment = new MapSegment(0); segment.x = x; segment.y = y; segments[x, y] = segment; } if (segments[x, y].ID == 0) { emptySegments++; } if (MapSegment.smallSegments.Contains(segments[x, y].ID)) { smallSegments++; } map.Copy(MapSegmentTile.GetSegmentMap(segments[x, y].ID), x * MapSegment.SegmentCellWidthHeight, y * MapSegment.SegmentCellWidthHeight); } } map.SegmentWidth = segmentWidth; map.SegmentHeight = segmentHeight; map.segments = segments; return(true); }