johnhattan

2D Hex grid generator for Unity

Oct 9th, 2019
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. public static class GridGenerator
  2. { // handy gizmo that makes 2D arrays of positions
  3.     public static Vector2Int DiamondHexVectorArrayPointMinimumSize(int sideLength) => new Vector2Int(sideLength, sideLength + (sideLength - 1)); // returns the minimum size rectangular array if you want to make a pointy-top diamond
  4.  
  5.     public static Vector3?[,] DiamondHexVectorArrayPoint(Vector3?[,] arr, int lengthSide)
  6.     { // convert a points-on-top hex array into a vertically-oriented diamond
  7.         int CenterRow = (lengthSide + (lengthSide - 1)) / 2;
  8.  
  9.         for (var RowDelta = 0; RowDelta <= CenterRow; ++RowDelta)
  10.         { // make sure the array that got passed to me is actually large enough
  11.             bool RowNegativeExists = (CenterRow - RowDelta < arr.GetLength(1));
  12.             bool RowPositiveExists = (CenterRow + RowDelta < arr.GetLength(1));
  13.  
  14.             int NumToRemoveLeft = (lengthSide % 2 != 0) ? (int)Math.Floor(RowDelta / 2.0f) : (int)Math.Ceiling(RowDelta / 2.0f);
  15.  
  16.             for (var i = 0; i < NumToRemoveLeft; ++i)
  17.             { // remove from the left
  18.                 if (RowNegativeExists)
  19.                     arr[i, CenterRow - RowDelta] = null;
  20.                 if (RowPositiveExists)
  21.                     arr[i, CenterRow + RowDelta] = null;
  22.             }
  23.  
  24.             int RowWidth = lengthSide - RowDelta; // the number of hexes in this row
  25.             for (var i = NumToRemoveLeft + RowWidth; i < arr.GetLength(0); ++i)
  26.             { // remove from the right
  27.                 if (RowNegativeExists)
  28.                     arr[i, CenterRow - RowDelta] = null;
  29.                 if (RowPositiveExists)
  30.                     arr[i, CenterRow + RowDelta] = null;
  31.             }
  32.         }
  33.  
  34.         // finally, wipe out anything that might be outside the diamond entirely
  35.         for (var LeftoverY = lengthSide + (lengthSide - 1); LeftoverY < arr.GetLength(1); ++LeftoverY)
  36.             for (var i = 0; i < arr.GetLength(1); ++i)
  37.                 arr[i, LeftoverY] = null;
  38.  
  39.         return arr;
  40.     }
  41.  
  42.     public static Vector3?[,] CenterVectorArray(Vector3?[,] arr, Vector2? origin = null)
  43.     { // figure out the width and height of the positions and adjust them so the whole thing is centered
  44.         float xMin = int.MaxValue, xMax = int.MinValue, yMin = int.MaxValue, yMax = int.MinValue;
  45.  
  46.         for (var i = 0; i < arr.GetLength(0); ++i)
  47.             for (var j = 0; j < arr.GetLength(1); ++j)
  48.             {
  49.                 Vector3? Pos = arr[i, j];
  50.                 if (Pos.HasValue)
  51.                 {
  52.                     if (Pos.Value.x < xMin)
  53.                         xMin = Pos.Value.x;
  54.                     if (Pos.Value.x > xMax)
  55.                         xMax = Pos.Value.x;
  56.                     if (Pos.Value.y < yMin)
  57.                         yMin = Pos.Value.y;
  58.                     if (Pos.Value.y > yMax)
  59.                         yMax = Pos.Value.y;
  60.                 }
  61.             }
  62.  
  63.         Vector3 Offset = new Vector3(xMin + ((xMax - xMin) * 0.5f), yMin + ((yMax - yMin) * 0.5f), 0.0f);
  64.  
  65.         if (origin.HasValue) // we're not using (0, 0) as the origin, so scoot it
  66.             Offset += new Vector3(origin.Value.x, origin.Value.y, 0.0f);
  67.  
  68.         for (var i = 0; i < arr.GetLength(0); ++i)
  69.             for (var j = 0; j < arr.GetLength(1); ++j)
  70.                 if (arr[i, j].HasValue)
  71.                     arr[i, j] = arr[i, j].Value - Offset;
  72.  
  73.         return arr;
  74.     }
  75.  
  76.     public static Vector3?[,] BuildHexVectorArrayPoint(float distanceBetweenCenters, Vector2Int sizeLogical, float z = 0.0f)
  77.     { // returns a rectangular array of all positions. All values will be filled
  78.         Vector2 HexSize = new Vector2(distanceBetweenCenters, distanceBetweenCenters * 1.155f);
  79.         Vector3?[,] RetVal = new Vector3?[sizeLogical.x, sizeLogical.y];
  80.         for (var i = 0; i < sizeLogical.x; ++i)
  81.             for (var j = 0; j < sizeLogical.y; ++j)
  82.                 RetVal[i, j] = new Vector3(i * HexSize.x + ((j % 2 != 0) ? (HexSize.x / 2) : 0.0f), j * HexSize.y * 0.75f, z);
  83.  
  84.         return RetVal;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment