Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class GridGenerator
- { // handy gizmo that makes 2D arrays of positions
- 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
- public static Vector3?[,] DiamondHexVectorArrayPoint(Vector3?[,] arr, int lengthSide)
- { // convert a points-on-top hex array into a vertically-oriented diamond
- int CenterRow = (lengthSide + (lengthSide - 1)) / 2;
- for (var RowDelta = 0; RowDelta <= CenterRow; ++RowDelta)
- { // make sure the array that got passed to me is actually large enough
- bool RowNegativeExists = (CenterRow - RowDelta < arr.GetLength(1));
- bool RowPositiveExists = (CenterRow + RowDelta < arr.GetLength(1));
- int NumToRemoveLeft = (lengthSide % 2 != 0) ? (int)Math.Floor(RowDelta / 2.0f) : (int)Math.Ceiling(RowDelta / 2.0f);
- for (var i = 0; i < NumToRemoveLeft; ++i)
- { // remove from the left
- if (RowNegativeExists)
- arr[i, CenterRow - RowDelta] = null;
- if (RowPositiveExists)
- arr[i, CenterRow + RowDelta] = null;
- }
- int RowWidth = lengthSide - RowDelta; // the number of hexes in this row
- for (var i = NumToRemoveLeft + RowWidth; i < arr.GetLength(0); ++i)
- { // remove from the right
- if (RowNegativeExists)
- arr[i, CenterRow - RowDelta] = null;
- if (RowPositiveExists)
- arr[i, CenterRow + RowDelta] = null;
- }
- }
- // finally, wipe out anything that might be outside the diamond entirely
- for (var LeftoverY = lengthSide + (lengthSide - 1); LeftoverY < arr.GetLength(1); ++LeftoverY)
- for (var i = 0; i < arr.GetLength(1); ++i)
- arr[i, LeftoverY] = null;
- return arr;
- }
- public static Vector3?[,] CenterVectorArray(Vector3?[,] arr, Vector2? origin = null)
- { // figure out the width and height of the positions and adjust them so the whole thing is centered
- float xMin = int.MaxValue, xMax = int.MinValue, yMin = int.MaxValue, yMax = int.MinValue;
- for (var i = 0; i < arr.GetLength(0); ++i)
- for (var j = 0; j < arr.GetLength(1); ++j)
- {
- Vector3? Pos = arr[i, j];
- if (Pos.HasValue)
- {
- if (Pos.Value.x < xMin)
- xMin = Pos.Value.x;
- if (Pos.Value.x > xMax)
- xMax = Pos.Value.x;
- if (Pos.Value.y < yMin)
- yMin = Pos.Value.y;
- if (Pos.Value.y > yMax)
- yMax = Pos.Value.y;
- }
- }
- Vector3 Offset = new Vector3(xMin + ((xMax - xMin) * 0.5f), yMin + ((yMax - yMin) * 0.5f), 0.0f);
- if (origin.HasValue) // we're not using (0, 0) as the origin, so scoot it
- Offset += new Vector3(origin.Value.x, origin.Value.y, 0.0f);
- for (var i = 0; i < arr.GetLength(0); ++i)
- for (var j = 0; j < arr.GetLength(1); ++j)
- if (arr[i, j].HasValue)
- arr[i, j] = arr[i, j].Value - Offset;
- return arr;
- }
- public static Vector3?[,] BuildHexVectorArrayPoint(float distanceBetweenCenters, Vector2Int sizeLogical, float z = 0.0f)
- { // returns a rectangular array of all positions. All values will be filled
- Vector2 HexSize = new Vector2(distanceBetweenCenters, distanceBetweenCenters * 1.155f);
- Vector3?[,] RetVal = new Vector3?[sizeLogical.x, sizeLogical.y];
- for (var i = 0; i < sizeLogical.x; ++i)
- for (var j = 0; j < sizeLogical.y; ++j)
- RetVal[i, j] = new Vector3(i * HexSize.x + ((j % 2 != 0) ? (HexSize.x / 2) : 0.0f), j * HexSize.y * 0.75f, z);
- return RetVal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment