Created
March 15, 2017 13:34
-
-
Save arun02139/ece61853f922954f11a0d4739a6ee549 to your computer and use it in GitHub Desktop.
Abstract class (inheriting from Cell) for all types of hexagonal cells
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
// for now, let's simplify our possible representations to match the core examples | |
// in the link below; full set of layout options is: odd_r, even_r, odd_q, even_q | |
public enum HexGridType { odd_q } | |
// implementation of hexagonal cell | |
// helpful link: http://www.redblobgames.com/grids/hexagons/ | |
public abstract class Hexagon : Cell | |
{ | |
// hexGrids can come in four types regarding the layout. This distinction is necessary to convert cube coordinates to offset and vice versa. | |
//[HideInInspector] | |
public HexGridType hexGridType; | |
// cube coordinates is another system of coordinates that makes calculation on hex grids easier | |
public CubeCoordinate cubeCoord | |
{ | |
get | |
{ | |
int x, y, z; | |
int col = offsetCoord.col; | |
int row = offsetCoord.row; | |
switch (hexGridType) | |
{ | |
case HexGridType.odd_q: | |
x = col; | |
z = row - (col - (Mathf.Abs(col) % 2)) / 2; | |
y = -x - z; | |
break; | |
// case HexGridType.even_q: | |
// x = col; | |
// z = row - (col + (Mathf.Abs(col) % 2)) / 2; | |
// y = -x - z; | |
// break; | |
default: | |
throw new NotImplementedException ("Hexagon.CubeCoord.get: HexGridType=" + hexGridType); | |
break; | |
} | |
return new CubeCoordinate (x, y, z); | |
} | |
} | |
protected OffsetCoordinate CubeToOffsetCoords(CubeCoordinate cubeCoords) | |
{ | |
int col; | |
int row; | |
int x = cubeCoords.x; | |
int y = cubeCoords.y; | |
int z = cubeCoords.z; | |
switch (hexGridType) | |
{ | |
case HexGridType.odd_q: | |
col = x; | |
row = z + (x - (Mathf.Abs (x) % 2)) / 2; | |
break; | |
// case HexGridType.even_q: | |
// col = x; | |
// row = z + (x + (Mathf.Abs (x) % 2)) / 2; | |
// break; | |
default: | |
throw new System.NotImplementedException (); | |
} | |
return new OffsetCoordinate(col, row); | |
} | |
// NOTE, WARNING, HARDCODE: cubic coordinate directions must be ordered to match | |
// Enum.AbsoluteDirection6 (HARDCODE-d for 'odd-q' offset coordinates) | |
protected static readonly CubeCoordinate[] _directions = { | |
new CubeCoordinate(0, +1, -1), // North | |
new CubeCoordinate(+1, 0, -1), // NorthEast | |
new CubeCoordinate(+1, -1, 0), // SouthEast | |
new CubeCoordinate(0, -1, +1), // South | |
new CubeCoordinate(-1, 0, +1), // SouthWest | |
new CubeCoordinate(-1, +1, 0), // NorthWest | |
}; | |
public override int GetDistance(Cell other) | |
{ | |
var _other = other as Hexagon; | |
// distance calculated using Manhattan norm | |
int distance = (int)(Mathf.Abs(cubeCoord.x - _other.cubeCoord.x) + Mathf.Abs(cubeCoord.y - _other.cubeCoord.y) + Mathf.Abs(cubeCoord.z - _other.cubeCoord.z)) / 2; | |
return distance; | |
} | |
// each hexagonal cell has six neighbors, who's positions on grid relative to the cell are stored in the _directions constant. | |
public override List<Cell> GetNeighbours(List<Cell> cells) | |
{ | |
List<Cell> ret = new List<Cell>(); | |
foreach (var direction in _directions) | |
{ | |
var neighbour = cells.Find(c => c.offsetCoord == CubeToOffsetCoords(cubeCoord + direction)); | |
if (neighbour == null) continue; | |
ret.Add(neighbour); | |
} | |
return ret; | |
} | |
// get a neighbor give an absolution direction 6 (i.e. N/NE/SE/S/SW/NW) | |
public Cell GetNeighbour(List<Cell> cells, AbsoluteDirection6 dir) | |
{ | |
var cubicDirection = _directions [(int)dir]; | |
var neighbour = cells.Find(c => c.offsetCoord == CubeToOffsetCoords(cubeCoord + cubicDirection)); | |
return neighbour; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment