public Tile ToTile(Vector3 pos) { Vector3 pt = new Vector3(pos.x / hexRadius, 0.0f, pos.z / hexRadius); switch (hexOrientation) { case HexOrientation.Pointy: { double q = Math.Sqrt(3.0f) / 3.0 * pos.x + (-1.0 / 3.0f) * pt.z; double r = 0.0f * pt.x + (2.0 / 3.0) * pt.z; Tile.FractionalIndex hex = new Tile.FractionalIndex(q, r, -q - r); Tile.CubeIndex cube = Tile.FractionalIndex.HexRound(hex); UnityEngine.Debug.Log(cube.ToString()); return(TileAt(cube)); } case HexOrientation.Flat: { double q = 2.0 / 3.0f * pos.x + 0.0 * pt.z; double r = -1.0 / 3.0f * pt.x + Mathf.Sqrt(3.0f) / 3.0f * pt.z; Tile.FractionalIndex hex = new Tile.FractionalIndex(q, r, -q - r); Tile.CubeIndex cube = Tile.FractionalIndex.HexRound(hex); UnityEngine.Debug.Log(cube.ToString()); return(TileAt(cube)); } default: break; } return(null); }
public Tile TileAt(Tile.CubeIndex index) { if (grid.ContainsKey(index.ToString())) { return(grid[index.ToString()]); } return(null); }
public List <Tile> TilesInRange(Tile center, int range) { //Return tiles rnage steps from center, http://www.redblobgames.com/grids/hexagons/#range List <Tile> ret = new List <Tile>(); Tile.CubeIndex o; for (int dx = -range; dx <= range; dx++) { for (int dy = Mathf.Max(-range, -dx - range); dy <= Mathf.Min(range, -dx + range); dy++) { o = new Tile.CubeIndex(dx, dy, -dx - dy) + center.index; if (grid.ContainsKey(o.ToString())) { ret.Add(grid[o.ToString()]); } } } return(ret); }
public int Distance(Tile.CubeIndex a, Tile.CubeIndex b) { return(Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y) + Mathf.Abs(a.z - b.z)); }
public List <Tile> TilesInRange(Tile.CubeIndex index, int range) { return(TilesInRange(TileAt(index), range)); }
public List <Tile> Neighbours(Tile.CubeIndex index) { return(Neighbours(TileAt(index))); }