Advertisement
Ajes

Untitled

May 7th, 2015
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. [SerializeField, Range(30, 250)]
  2.     private static float size = 20;
  3.  
  4.     [SerializeField]
  5.     private static string seed = "Empty";
  6.  
  7.     [SerializeField, Range(0, 0.35f)]
  8.     private static float complexity = 0.1f;
  9.  
  10.     public static DestructablePolygonHandler GenerateRandomPolygon(/*float minX, float maxX, float minY, float maxY*/Vector2 desiredSize, float cellsPrUnit = 0.8f) {
  11.         /*  Heatmap<bool> grid = new Heatmap<bool>(
  12.               new Rect(
  13.                   Random.Range(minX, maxX),
  14.                   Random.Range(minX, maxX),
  15.                   Random.Range(minY, maxY),
  16.                   Random.Range(minY, maxY)
  17.               ),
  18.               cellsPrUnit,
  19.               () => false,
  20.               null,
  21.               null
  22.           );*/
  23.  
  24.         /* for (int x = 0; x < grid.LengthX; x++) {
  25.              for (int y = 0; y < grid.LengthY; y++) {
  26.                  grid[x, y] = true;
  27.              }
  28.          }*/
  29.         Heatmap<bool> grid;
  30.         while (true) {
  31.  
  32.  
  33.             var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  34.             var random = new System.Random();
  35.             seed = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());
  36.  
  37.  
  38.             Vector2 startPos = FindIsland();
  39.             List<Vector2> island = GetIsland(startPos).ToList();
  40.  
  41.             Rect rect = getBounds(island);
  42.  
  43.             for (int i = 0; i < island.Count; i++) {
  44.                 Vector2 vector2 = island[i];
  45.                 vector2.x += -1 * rect.x;
  46.                 vector2.y += -1 * rect.y;
  47.                 island[i] = vector2;
  48.             }
  49.             rect.x = 0;
  50.             rect.y = 0;
  51.  
  52.  
  53.             float scaleX = desiredSize.x / rect.width;
  54.             float scaleY = desiredSize.y / rect.height;
  55.             for (int i = 0; i < island.Count; i++) {
  56.                 Vector2 vector2 = island[i];
  57.                 island[i] = new Vector2(vector2.x * scaleX, vector2.y * scaleY);
  58.             }
  59.             rect.width = desiredSize.x;
  60.             rect.height = desiredSize.y;
  61.  
  62.  
  63.             grid = new Heatmap<bool>(new Rect(rect.x, rect.y, rect.width + 1, rect.height + 1),
  64.                   cellsPrUnit, () => false, null, null);
  65.             foreach (Vector2 vector2 in island) {
  66.                 grid[Mathf.FloorToInt(vector2.x), Mathf.FloorToInt(vector2.y)] = true;
  67.             }
  68.  
  69.             if (island.Count > 20) {
  70.                 break;
  71.             }
  72.  
  73.         }
  74.  
  75.  
  76.  
  77.  
  78.         return new DestructablePolygonHandler(grid);
  79.     }
  80.  
  81.  
  82.  
  83.     private static Rect getBounds(List<Vector2> island) {
  84.  
  85.         float minX = float.MaxValue;
  86.         float maxX = float.MinValue;
  87.         float minY = float.MaxValue;
  88.         float maxY = float.MinValue;
  89.  
  90.         foreach (Vector2 vector2 in island) {
  91.  
  92.             if (vector2.x < minX) {
  93.                 minX = vector2.x;
  94.             }
  95.             if (vector2.x > maxX) {
  96.                 maxX = vector2.x;
  97.             }
  98.  
  99.             if (vector2.y < minY) {
  100.                 minY = vector2.y;
  101.             }
  102.             if (vector2.y > maxY) {
  103.                 maxY = vector2.y;
  104.             }
  105.  
  106.         }
  107.         /*Debug.Log(minX);
  108.         Debug.Log(maxX);
  109.         Debug.Log(minY);
  110.         Debug.Log(maxY);*/
  111.  
  112.         Rect rect = new Rect(minX, minY, Mathf.Abs(minX) + Mathf.Abs(maxX), Mathf.Abs(minY) + Mathf.Abs(maxY));
  113.         return rect;
  114.  
  115.     }
  116.  
  117.     private static bool runOnce = false;
  118.  
  119.     private static Vector2 FindIsland() {
  120.         Vector2 point = new Vector2(0, 0);
  121.  
  122.         while (IsOnIsland(point) == false) {
  123.             point.x++;
  124.             point.y++;
  125.         }
  126.  
  127.         return point;
  128.     }
  129.  
  130.     private static bool IsOnIsland(Vector2 point) {
  131.         return Noise.Generate(40 + point.x / size, (seed.GetHashCode() / 3.33f) % 30, 40 + point.y / size) > (0.35f - complexity);
  132.     }
  133.  
  134.     private static HashSet<Vector2> GetIsland(Vector2 startPos) {
  135.         HashSet<Vector2> visited = new HashSet<Vector2>();
  136.         HashSet<Vector2> unvisited = new HashSet<Vector2>();
  137.         unvisited.Add(startPos);
  138.  
  139.         do {
  140.             Vector2 currentPoint = unvisited.First();
  141.             unvisited.Remove(currentPoint);
  142.             visited.Add(currentPoint);
  143.  
  144.             // Left
  145.             Vector2 searchDirection = new Vector2(currentPoint.x - 1, currentPoint.y);
  146.             if (!visited.Contains(searchDirection) && IsOnIsland(searchDirection)) unvisited.Add(searchDirection);
  147.             // Up
  148.             searchDirection = new Vector2(currentPoint.x, currentPoint.y + 1);
  149.             if (!visited.Contains(searchDirection) && IsOnIsland(searchDirection)) unvisited.Add(searchDirection);
  150.             // Right
  151.             searchDirection = new Vector2(currentPoint.x + 1, currentPoint.y);
  152.             if (!visited.Contains(searchDirection) && IsOnIsland(searchDirection)) unvisited.Add(searchDirection);
  153.             // Down
  154.             searchDirection = new Vector2(currentPoint.x, currentPoint.y - 1);
  155.             if (!visited.Contains(searchDirection) && IsOnIsland(searchDirection)) unvisited.Add(searchDirection);
  156.         } while (unvisited.Count != 0);
  157.  
  158.         return visited;
  159.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement