Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 public Tile TileAt(Tile.CubeIndex index)
 {
     if (grid.ContainsKey(index.ToString()))
     {
         return(grid[index.ToString()]);
     }
     return(null);
 }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
 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));
 }
Esempio n. 5
0
 public List <Tile> TilesInRange(Tile.CubeIndex index, int range)
 {
     return(TilesInRange(TileAt(index), range));
 }
Esempio n. 6
0
 public List <Tile> Neighbours(Tile.CubeIndex index)
 {
     return(Neighbours(TileAt(index)));
 }