/// <summary> /// <para>Uses a hex coordinate system to represent where a hex is in the complete grid. /// Since grids are offset between rows or columns, it's assumed that the center of (0,0,0) /// is at position (0,0). (0,1,-1) diagonally up right to the the original. (0, 2) is in line with the original. /// This format assumes a TopPoint configuration. See below for coords</para> /// /// <para> /// z___y /// __z y__ ///xxxx 0 xxxx /// __y z__ /// y___z /// </para> /// /// <para> /// This is remember about this system. x is 0 along the x axis and increase as you go above /// or decreases below. Same for z and y. All coordinates will always sum to zero. So coordinates /// can be understood as distance from a line, not notches across the line /// </para> /// /// <para>Using answer here to calculate: https://stackoverflow.com/questions/2459402/hexagonal-grid-coordinates-to-pixel-coordinates</para> /// </summary> /// <param name="coordinate"></param> /// <returns></returns> public Vector2 EuclidianPosition(SHexCoordinate coordinate) { return(new Vector2( InnerRadius * (coordinate.X + coordinate.Y * 2), coordinate.X * HexVerticalDistance )); }
/// <summary> /// Returns all coordinates radial to the coordinate based on radius, including the original coordinate /// </summary> /// <param name="radius"></param> /// <returns></returns> public SHexCoordinate[] RadialStep(int radius) { List <SHexCoordinate> coords = new List <SHexCoordinate>() { this }; SHexCoordinate currentCoord = this; // step in rings to find coords for (int ring = 1; ring <= radius; ring++) { currentCoord = currentCoord.Step(EDirection.XNegZ); // each ring has 6 segments for (int segment = 0; segment < 6; segment++) { // each step needs to be calculated in a particular direction with ring steps for (int step = 0; step < ring; step++) { currentCoord = currentCoord.Step(ClockWiseDirections[segment]); coords.Add(currentCoord); } } } return(coords.ToArray()); }
/// <summary> /// Returns neighbour coordinates of the input coordinate /// </summary> /// <param name="coordinate"></param> /// <returns></returns> public SHexCoordinate[] GetNeighbours(SHexCoordinate coordinate) { return(new SHexCoordinate[] { GetNeighbour(coordinate, EHexDirection.UP_RIGHT), GetNeighbour(coordinate, EHexDirection.RIGHT), GetNeighbour(coordinate, EHexDirection.DOWN_RIGHT), GetNeighbour(coordinate, EHexDirection.DOWN_LEFT), GetNeighbour(coordinate, EHexDirection.LEFT), GetNeighbour(coordinate, EHexDirection.UP_LEFT) }); }
/// <summary> /// Returns the neighbouring hex coordinate in a direction /// </summary> /// <param name="coordinate"></param> /// <param name="direction"></param> /// <returns></returns> public SHexCoordinate GetNeighbour(SHexCoordinate coordinate, EHexDirection direction) { switch (direction) { case EHexDirection.UP_RIGHT: return(new SHexCoordinate(coordinate.X + 1, coordinate.Y)); case EHexDirection.RIGHT: return(new SHexCoordinate(coordinate.X, coordinate.Y + 1)); case EHexDirection.DOWN_RIGHT: return(new SHexCoordinate(coordinate.X - 1, coordinate.Y + 1)); case EHexDirection.DOWN_LEFT: return(new SHexCoordinate(coordinate.X - 1, coordinate.Y)); case EHexDirection.LEFT: return(new SHexCoordinate(coordinate.X, coordinate.Y - 1)); case EHexDirection.UP_LEFT: return(new SHexCoordinate(coordinate.X + 1, coordinate.Y - 1)); } return(SHexCoordinate.Zero); }
/// <summary> /// Construct a 2D array of Hexcoordinates to represent the grid /// </summary> /// <param name="dimensions"></param> public HexGrid2D(DiscreteVector2 dimensions) { Grid = SHexCoordinate.Grid2D(dimensions); }
/// <summary> /// Print the SHexCooridinate in the format [x, y, z] /// </summary> /// <param name="coord"></param> public static string Print(this SHexCoordinate c) { return($"[{c.X}, {c.Y}, {c.Z}]"); }
/// <summary> /// Returns the euclidian position of a hex based on coords /// </summary> /// <param name="coord"></param> /// <returns></returns> public Vector3 GetPosition(SHexCoordinate coord) { Vector2 pos = _converter.Calculator.EuclidianPosition(coord); return(new Vector3(pos.x + transform.position.x, transform.position.y, pos.y + transform.position.z)); }