/// <summary> /// returns the cartesian coordinate of the input corner coordinate. /// </summary> public static Vector3 CornerCoordToCartesianCoord(Vector3Int cornerCoord, float yCoord = 0) { Vector3 worldPos = HexConverter.TileCoordToCartesianCoord(cornerCoord); worldPos = new Vector3(worldPos.x / 3f, yCoord, worldPos.z / 3f); return(worldPos); }
/// <summary> /// returns the euclidean distance of the 2 input tiles. /// </summary> public static float BetweenTilesEuclidean(Vector3Int tileA, Vector3Int tileB) { Vector3 a = HexConverter.TileCoordToCartesianCoord(tileA); Vector3 b = HexConverter.TileCoordToCartesianCoord(tileB); return(Vector3.Distance(a, b)); }
/// <summary> /// returns the midpoint of the edge in cartesian coordinates. /// </summary> public static Vector3 EdgeCoordToCartesianCoord(Vector3Int edgeCoord, float yCoord = 0) { Vector3 worldPos = HexConverter.TileCoordToCartesianCoord(edgeCoord); worldPos = new Vector3(worldPos.x / 2f, yCoord, worldPos.z / 2f); return(worldPos); }
/// <summary> /// returns the euclidian distance of a straight line from the center of tileA to the center of tileB accounting for map wrapping /// </summary> public float Euclidean(Vector3Int tileA, Vector3Int tileB) { if (coordinateWrapper != null) { tileB = coordinateWrapper.ShiftTargetToClosestPeriodicTilePosition(tileA, tileB); } return(Vector3.Distance(HexConverter.TileCoordToCartesianCoord(tileA), HexConverter.TileCoordToCartesianCoord(tileB))); }
public Vector3 GetRealPosition() { if (!CursorIsOnMap) { return(Vector3.zero); } return(HexConverter.TileCoordToCartesianCoord(TileCoord, 0.1f)); //we put our tile marker on the tile in front of the player }
/// <summary> /// converts the input cartesian coordinate to the cord coordinate closest to it. /// </summary> //TODO: is a bit inefficient and allocates memory, so improve later /// <param name="cartesianCoord"></param> /// <returns></returns> public static Vector3Int CartesianCoordToClosestCornerCoord(Vector3 cartesianCoord) { Vector3Int tileCoord = CartesianCoordToTileCoord(cartesianCoord); List <Vector3Int> cornerCoords = HexGrid.GetCorners.OfTile(tileCoord); Vector3Int closestCorner = new Vector3Int(-1, -1, -1); //invalid default coordinate float minDistanceSoFar = float.MaxValue; for (int i = 0; i < cornerCoords.Count; i++) { Vector3 worldPos = HexConverter.TileCoordToCartesianCoord(cornerCoords[i]); worldPos = new Vector3(worldPos.x / 3f, 1, worldPos.z / 3f); float distance = Vector3.Distance(worldPos, cartesianCoord); if (distance < minDistanceSoFar) { closestCorner = cornerCoords[i]; minDistanceSoFar = distance; } } return(closestCorner); }
/// <summary> /// returns all tiles of a cone starting at a point with given direction, length and width /// </summary> /// ![yellow = origin , blue = direction, green/blue = cone tiles](GetTiles_Cone.png) public static List <Vector3Int> Cone(Vector3Int origin, Vector3Int targetDirection, float coneWidthHalfAngle, int coneLength) { Vector3 originCartesian = HexConverter.TileCoordToCartesianCoord(origin); IEnumerable <Vector3Int> ring = GetTiles.Ring(origin, coneLength, 1); HashSet <Vector3Int> cone = new HashSet <Vector3Int>(); foreach (Vector3Int target in ring) { Vector3 targetWorldPos = HexConverter.TileCoordToCartesianCoord(target); Vector3 lookWorldPos = HexConverter.TileCoordToCartesianCoord((origin + targetDirection)); float angle = Vector3.Angle(targetWorldPos - originCartesian, lookWorldPos - originCartesian); if (Mathf.Abs(angle) > coneWidthHalfAngle + 0.001) { continue; } List <Vector3Int> linePointsL = GetTiles.Line(origin, target, false, -0.00001f); //for more consistent results we use 2 lines, one slightly left of center List <Vector3Int> linePointsR = GetTiles.Line(origin, target, false, +0.00001f); //and this her slightly right of center cone.UnionWith(linePointsL); cone.UnionWith(linePointsR); } return(cone.ToList()); }
public static Vector2 TileCoordToNormalizedPosition(Vector3Int tileCoordinate, float minX, float maxX, float minZ, float maxZ) { Vector3 cartesianCoord = HexConverter.TileCoordToCartesianCoord(tileCoordinate); return(CartesianCoordToNormalizedPosition(cartesianCoord, minX, maxX, minZ, maxZ)); }