/// <summary> /// returns the periodic tile coordinate of the input cartesian coordinate /// </summary> public Vector3Int FromCartesianCoordinate(Vector3 cartesianCoordinate) { if (coordinateWrapper != null) { cartesianCoordinate = coordinateWrapper.WrapCartesianCoordinate(cartesianCoordinate); } Vector3Int coord = HexConverter.CartesianCoordToTileCoord(cartesianCoordinate); return(coord); }
/// <summary> /// returns the periodic tile coordinate of the input cartesian coordinate, the out parameter specifies if that coordinate belongs to the map /// </summary> public Vector3Int FromCartesianCoordinate(Vector3 cartesianCoordinate, out bool tileIsOnMap) { if (coordinateWrapper != null) { cartesianCoordinate = coordinateWrapper.WrapCartesianCoordinate(cartesianCoordinate); } Vector3Int coord = HexConverter.CartesianCoordToTileCoord(cartesianCoordinate); tileIsOnMap = false; if (tileIndexByPosition.ContainsKey(coord)) { tileIsOnMap = true; } return(coord); }
/// <summary> /// returns the shortest path of corners from the origin to the target corner - optionally including the origin /// </summary> /// ![green = origin , purple = target, blue/purple = result - origin can optionally be included](GetCorners_PathAlongGrid.png) public static List <Vector3Int> PathAlongGrid(Vector3Int originCorner, Vector3Int targetCorner, bool includeOrigin, float horizontalNudgeFromOriginCenter = NudgePositive) { if (originCorner == targetCorner) { throw new System.ArgumentException("origin corner and target corner are the same - can't create a Path"); } List <Vector3Int> corners = new List <Vector3Int>(); if (includeOrigin) { corners.Add(originCorner); } Vector3Int previousCorner = originCorner; Vector3 cartesianOrigin = HexConverter.CornerCoordToCartesianCoord(originCorner) + new Vector3(horizontalNudgeFromOriginCenter, 0, 0); Vector3 cartesianTarget = HexConverter.CornerCoordToCartesianCoord(targetCorner); int dist = GetDistance.BetweenCorners(originCorner, targetCorner); for (int i = 1; i <= dist; i++) { Vector3 lerped = Vector3.Lerp(cartesianOrigin, cartesianTarget, (1f / dist) * i); Vector3Int tileCoord = HexConverter.CartesianCoordToTileCoord(lerped); List <Vector3Int> cornerCoords = HexGrid.GetCorners.OfTile(tileCoord); cornerCoords.RemoveAll(x => HexGrid.GetDistance.BetweenCorners(previousCorner, x) != 1); Vector3Int closestCorner = new Vector3Int(); float minDistanceSoFar = float.MaxValue; for (int j = 0; j < cornerCoords.Count; j++) { Vector3 worldPos = HexConverter.CornerCoordToCartesianCoord(cornerCoords[j]); float distance = Vector3.Distance(worldPos, lerped); if (distance < minDistanceSoFar) { closestCorner = cornerCoords[j]; minDistanceSoFar = distance; } } corners.Add(closestCorner); previousCorner = closestCorner; } return(corners); }
/// <summary> /// returns if the input cartesian coordinate belongs to the map /// </summary> public bool IsInputCoordinateOnMap(Vector3 inputCartesianCoordinate) { if (coordinateWrapper != null) { inputCartesianCoordinate = coordinateWrapper.WrapCartesianCoordinate(inputCartesianCoordinate); } Vector3Int tileCoordinate = HexConverter.CartesianCoordToTileCoord(inputCartesianCoordinate); if (tileIndexByPosition.ContainsKey(tileCoordinate)) { return(true); } else { return(false); } }
/// <summary> /// updates all the mouse position data /// </summary> private void UpdatePlayerPositionData() { CursorIsOnMap = false; CartesianCoordInfiniteGrid = transform.position; CartesianCoordWrapped = transform.position; CubeCoordRaw = HexConverter.CartesianCoordToTileCoord(CartesianCoordInfiniteGrid); TileCoord = CubeCoordRaw; OffsetCoordInfiniteGrid = HexConverter.CartesianCoordToOffsetCoord(CartesianCoordInfiniteGrid); OffsetCoord = OffsetCoordInfiniteGrid; ClosestEdgeCoordInfiniteGrid = HexConverter.CartesianCoordToClosestEdgeCoord(CartesianCoordInfiniteGrid); ClosestEdgeCoord = ClosestEdgeCoordInfiniteGrid; ClosestCornerCoordInfiniteGrid = HexConverter.CartesianCoordToClosestCornerCoord(CartesianCoordInfiniteGrid); ClosestCornerCoord = ClosestCornerCoordInfiniteGrid; //SelectionRay = Camera.main.ScreenPointToRay(Input.mousePosition); if (hexMap != null) { if (hexMap.CoordinateWrapper != null) { CartesianCoordWrapped = hexMap.CoordinateWrapper.WrapCartesianCoordinate(CartesianCoordInfiniteGrid); } CursorIsOnMap = hexMap.GetTilePosition.IsInputCoordinateOnMap(CartesianCoordWrapped); TileCoord = HexConverter.CartesianCoordToTileCoord(CartesianCoordWrapped); OffsetCoord = HexConverter.TileCoordToOffsetTileCoord(TileCoord); ClosestEdgeCoord = HexConverter.CartesianCoordToClosestEdgeCoord(CartesianCoordWrapped); ClosestCornerCoord = HexConverter.CartesianCoordToClosestCornerCoord(CartesianCoordWrapped); } }
/// <summary> /// returns the tile coordinate of the input cartesian coordinate /// </summary> public static Vector3Int OfCartesianCoordinate(Vector3 cartesianCoordinate) { return(HexConverter.CartesianCoordToTileCoord(cartesianCoordinate)); }