public OffsetCoordinate CubeToOffset(CubeCoordinate cubeCoordinate) { if (HexOrientation == HexagonOrientation.Flat) { return(CubeToFlatOffset(cubeCoordinate)); } else //HexOrientation == HexagonOrientation.Pointy { return(CubeToPointyOffset(cubeCoordinate)); } }
public Vector2[] CubeCoordinateCorners(CubeCoordinate cubeCoordinate) { Vector2[] corners = new Vector2[6]; Vector2 center = CubeCoordinateToWorld(cubeCoordinate); for (int i = 0; i < corners.Length; i++) { Vector2 offset = CubeCoordinateCornerOffset(i); corners[i] = new Vector2(center.x + offset.x, center.y + offset.y); } return(corners); }
/// <summary> /// Build the line of hexagon between two hexagons. /// </summary> /// <param name="start">The CubeCoordinate of the first hexagon of the line.</param> /// <param name="end">The CubeCoordinate of the last hexagon of the line.</param> /// <returns>An array of CubeCoordinate of each hexagon between start (included) and end (included)</returns> public static CubeCoordinate[] DrawLinedraw(CubeCoordinate start, CubeCoordinate end) { int N = CubeCoordinate.CubeCoordinate_distance(start, end); CubeCoordinate[] results = new CubeCoordinate[N]; double step = 1.0 / Math.Max(N, 1); for (int i = 0; i < results.Length; i++) { results[i] = FractionalCubeCoordinate.Lerp(start, end, step * i).ToCubeCoordinate(); } return(results); }
public void DebugDrawHexagon(CubeCoordinate coordinate, Color color, float?duration = null) { Vector2[] corners = CubeCoordinateCorners(coordinate); for (int i = 0; i < corners.Length; i++) { int y = (i + 1) % 6; Debug.DrawLine( new Vector3(corners[i].x, 0f, corners[i].y), new Vector3(corners[y].x, 0f, corners[y].y), color, duration.HasValue ? duration.Value : Time.deltaTime ); } }
/// <summary> /// Build the area of CubeCoordinate around the center in a defined range. /// </summary> /// <param name="center">The center of the area.</param> /// <param name="range">The distance in hexagon to reach the edge of the area.</param> /// <returns>An array of CubeCoordinate representing the area around the center.</returns> public static CubeCoordinate[] DrawArea(CubeCoordinate center, int range) { if (range < 0) { range = -range; } List <CubeCoordinate> results = new List <CubeCoordinate>(); for (int x = -range; x <= range; x++) { for (int y = Mathf.Max(-range, -x - range); y <= Mathf.Min(range, -x + range); y++) { results.Add(center + new CubeCoordinate(x, y)); } } return(results.ToArray()); }
/// <summary> /// Build a ring around the center with an inner and outer radius; /// </summary> /// <param name="center">The center of the ring.</param> /// <param name="minRadius">The min radius of the ring.</param> /// <param name="maxRadius">The max radius of the ring.</param> /// <returns>An array of CubeCoordinate representing the ring</returns> public static CubeCoordinate[] DrawLargeRing(CubeCoordinate center, int innerRadius, int outerRadius) { if (innerRadius < 0) { innerRadius = -innerRadius; } if (outerRadius < 0) { outerRadius = -outerRadius; } if (innerRadius == 0 && outerRadius == 0) { return new CubeCoordinate[] { center } } ; if (innerRadius == 0 || outerRadius == 0) { return(DrawArea(center, Mathf.Max(innerRadius, outerRadius))); } if (innerRadius == outerRadius) { return(DrawRing(center, innerRadius)); } if (outerRadius < innerRadius) { int swap = innerRadius; innerRadius = outerRadius; outerRadius = swap; } List <CubeCoordinate> results = new List <CubeCoordinate>(); for (int i = innerRadius; i <= outerRadius; i++) { results.AddRange(DrawRing(center, i)); } return(results.ToArray()); }
/// <summary> /// Rotate the CubeCoordinate around a center on the hexagonal grid. /// </summary> /// <param name="target">The CubeCoordinate to rotate.</param> /// <param name="center">The center of the rotation.</param> /// <param name="rotation">The angle of rotation.</param> /// <returns>A CubeCoordinate representing the rotation around the center on the hexagonal grid</returns> public static CubeCoordinate RotateCoordinate(CubeCoordinate target, CubeCoordinate center, CubeRotation rotation) { CubeCoordinate vectorIN = target - center; CubeCoordinate vectorOut = vectorIN; switch (rotation) { case CubeRotation.CW_60: case CubeRotation.CCW_300: vectorOut = new CubeCoordinate(-vectorIN.S, -vectorIN.Q, -vectorIN.R); break; case CubeRotation.CW_120: case CubeRotation.CCW_240: vectorOut = new CubeCoordinate(vectorIN.R, vectorIN.S, vectorIN.Q); break; case CubeRotation.CW_180: case CubeRotation.CCW_180: vectorOut = vectorIN * -1; break; case CubeRotation.CW_240: case CubeRotation.CCW_120: vectorOut = new CubeCoordinate(vectorIN.S, vectorIN.Q, vectorIN.R); break; case CubeRotation.CW_300: case CubeRotation.CCW_60: vectorOut = new CubeCoordinate(-vectorIN.R, -vectorIN.S, -vectorIN.Q); break; } return(center + vectorOut); }
/// <summary> /// Build the area of CubeCoordinate that intersect two areas. /// </summary> /// <param name="aCenter">The center of the first area.</param> /// <param name="aRange">The distance in hexagon to reach the edge of the first area./param> /// <param name="bCenter">The center of the second area.</param> /// <param name="bRange">The distance in hexagon to reach the edge of the second area.</param> /// <returns>An array of CubeCoordinate representing the intersection between the two areas</returns> public static CubeCoordinate[] DrawIntersectArea(CubeCoordinate aCenter, int aRange, CubeCoordinate bCenter, int bRange) { if (aRange < 0) { aRange = -aRange; } if (bRange < 0) { bRange = -bRange; } List <CubeCoordinate> results = new List <CubeCoordinate>(); int xMin = Mathf.Max(aCenter.Q - aRange, bCenter.Q - bRange); int xMax = Mathf.Min(aCenter.Q + aRange, bCenter.Q + bRange); int yMin = Mathf.Max(aCenter.R - aRange, bCenter.R - bRange); int yMax = Mathf.Min(aCenter.R + aRange, bCenter.R + bRange); int zMin = Mathf.Max(aCenter.S - aRange, bCenter.S - bRange); int zMax = Mathf.Min(aCenter.S + aRange, bCenter.S + bRange); for (int x = xMin; x <= xMax; x++) { for (int y = Mathf.Max(yMin, -x - zMax); y <= Mathf.Min(yMax, -x - zMin); y++) { results.Add(new CubeCoordinate(x, y)); } } return(results.ToArray()); }