void DebugHexMath() { Vector3Int origin = new Vector3Int(0, 0, 0); Vector3Int target = new Vector3Int(2, 2, 0); Vector3Int targetCube = new Vector3Int(1, -3, 2); // Distance testing: Oddr then Cube Assert.AreEqual(HexMath.OddrDistance(origin, origin), 0); Assert.AreEqual(HexMath.OddrDistance(origin, origin + HexMath.CubeDirection(0)), 1); Assert.AreEqual(HexMath.OddrDistance(origin, origin + HexMath.CubeDirection(0) * 2), 2); Assert.AreEqual(HexMath.OddrDistance(origin, target), 3); Assert.AreEqual(HexMath.OddrDistance(origin, target + HexMath.CubeDirection(0)), 2); Assert.AreEqual(HexMath.OddrDistance(origin, new Vector3Int(-6, -6, 0)), 9); Assert.AreEqual(HexMath.CubeDistance(origin, origin), 0); Assert.AreEqual(HexMath.CubeDistance(origin, origin + HexMath.CubeDirection(0) * 2), 2); Assert.AreEqual(HexMath.CubeDistance(origin, new Vector3Int(0, 0, 0)), 0); Assert.AreEqual(HexMath.CubeDistance(origin, targetCube), 3); Assert.AreEqual(HexMath.CubeDistance(origin, target + HexMath.CubeDirection(0)), 2); Assert.AreEqual(HexMath.CubeDistance(origin, new Vector3Int(-1, 4, -3)), 4); // Cube - Hex conversion testing. Vector3Int Hex = new Vector3Int(1, 1, 0); Vector3Int Cube = new Vector3Int(1, -2, 1); Assert.AreEqual(Hex, HexMath.CubeToOddr(Cube)); Assert.AreEqual(Cube, HexMath.OddrToCube(Hex)); Hex = new Vector3Int(-2, -2, 0); Cube = new Vector3Int(-1, 3, -2); Assert.AreEqual(Hex, HexMath.CubeToOddr(Cube)); Assert.AreEqual(Cube, HexMath.OddrToCube(Hex)); Hex = new Vector3Int(1, -2, 0); Cube = new Vector3Int(2, 0, -2); Assert.AreEqual(Hex, HexMath.CubeToOddr(Cube)); Assert.AreEqual(Cube, HexMath.OddrToCube(Hex)); Hex = new Vector3Int(-2, 2, 0); Cube = new Vector3Int(-3, 1, 2); Assert.AreEqual(Hex, HexMath.CubeToOddr(Cube)); Assert.AreEqual(Cube, HexMath.OddrToCube(Hex)); /* Neighbor Testing: Testing around Origin: (0, 0, 0). * - Radius 0. It's a marsh, so a neighbor test of radius 0 should simply return one marsh. * - Radius 1. Should add 6 soil tiles and count equal 7 * - Radius 2. Should add 12 barren tiles and count equal 19. * No tiles should be water for this setup. */ NeighborTestHelper(0); NeighborTestHelper(1); NeighborTestHelper(2); NeighborTestHelper(3); }
// Get Tendrils: There are 18 unique tendrils that form using a given tile as a base, not including the // tendrils that are also considered clusters. private static List <List <Vector3Int> > GetTendrils(Vector3Int baseTilePosition) { List <List <Vector3Int> > tileTendrils = new List <List <Vector3Int> >(); // The for loop represents the 6 possible tiles that surround the base tile. for (int i = 0; i < 6; i++) { // Add the center tile since it will be part of every tendril. List <Vector3Int> tendril = new List <Vector3Int>(); tendril.Add(baseTilePosition); // Add this loop's tile to this tednril, based on its position relative to the base. // That is, baseTilePosition + cubeDirections. Of course we have to convert to and from cube coords. Vector3Int targetCubePosition = HexMath.cubeDirections[i] + HexMath.OddrToCube(baseTilePosition); Vector3Int targetOddrPosition = HexMath.CubeToOddr(targetCubePosition); tendril.Add(targetOddrPosition); // The third tile slightly is more complicated. It can be in any three directions based on the initial // direction, deviating by -1, 0, and +1, and making sure to wrap from -1 to 5. for (int d = -1; d <= 1; d++) { List <Vector3Int> triple = new List <Vector3Int>(); int newDirection = ((i + d) % 6 + 6) % 6; targetCubePosition = HexMath.cubeDirections[newDirection] + HexMath.OddrToCube(baseTilePosition); targetOddrPosition = HexMath.CubeToOddr(targetCubePosition); triple.Add(targetOddrPosition); triple.AddRange(tendril); // Now add this tendril of three tiles to the tileTendrils list. tileTendrils.Add(triple); } } return(tileTendrils); }
// Get Clusters: There are 15 unique clusters that form around a central tile. // This method makes a list of them and returns them. private static List <List <Vector3Int> > GetClusters(Vector3Int centerTilePosition) { List <List <Vector3Int> > tileClusters = new List <List <Vector3Int> >(); // The outer loop represents the 6 possible tiles that surround the central tile. for (int i = 0; i < 6; i++) { // Add the center tile since it will be part of every cluster. List <Vector3Int> cluster = new List <Vector3Int>(); cluster.Add(centerTilePosition); // Add the first outer loop tile of this cluster, based on its position relative to center. // That is, centerTilePosition + cubeDirections. Of course we have to convert to and from cube coords. Vector3Int targetCubePosition = HexMath.cubeDirections[i] + HexMath.OddrToCube(centerTilePosition); Vector3Int targetOddrPosition = HexMath.CubeToOddr(targetCubePosition); cluster.Add(targetOddrPosition); // The inner loop goes through each unique third tile addition to the group. for (int j = i + 1; j < 6; j++) { List <Vector3Int> triple = new List <Vector3Int>(); // Add the inner loop tile of this cluster, based on its position relative to center. // That is, centerTilePosition + cubeDirections. Of course we have to convert to and from cube coords. targetCubePosition = HexMath.cubeDirections[j] + HexMath.OddrToCube(centerTilePosition); targetOddrPosition = HexMath.CubeToOddr(targetCubePosition); triple.Add(targetOddrPosition); triple.AddRange(cluster); // Now add this cluster of three tiles to the tileClusters list. tileClusters.Add(triple); } } return(tileClusters); }