コード例 #1
0
    public Hex GetHex(
        CubeVector coordinates
        )
    {
        int z = coordinates.Z;

        // Check for array index out of bounds.
        if (z < 0 || z >= HexOffsetColumns)
        {
            return(null);
        }

        int x = coordinates.X + z / 2;

        // Check for array index out of bounds.
        if (x < 0 || x >= HexOffsetColumns)
        {
            return(null);
        }

        return(HexGrid.GetElement(
                   coordinates.X,
                   coordinates.Y,
                   coordinates.Z
                   ));
    }
コード例 #2
0
 /// <summary>
 /// Gets the cross product of CubeVector a with CubeVector b.
 /// </summary>
 /// <param name="a">
 /// A vector to be used as the multiplicand.
 /// </param>
 /// <param name="b">
 /// A vector to be used as the multiplier.
 /// </param>
 /// <returns></returns>
 public static CubeVector CrossProduct(
     CubeVector a,
     CubeVector b
     )
 {
     return(a.Cross(b));
 }
コード例 #3
0
 /// <summary>
 /// Get a CubeVector of length 1 representing the direction from
 /// one cube vector to antoher, where the provided cube vectors
 /// are considered to be coordinates on a plane.
 /// </summary>
 /// <param name="from">
 /// The origin of the direction.
 /// </param>
 /// <param name="to">
 /// The destination of the direction.
 /// </param>
 /// <returns>
 /// A CubeVector of length 1 representing the direction from
 /// one cube vector to antoher, where the provided cube vectors
 /// are considered to be coordinates on a plane.
 /// </returns>
 public static CubeVector CubeDirection(
     CubeVector from,
     CubeVector to
     )
 {
     return((to - from).Normalized);
 }
コード例 #4
0
    private static bool IsWrappingRight(
        CubeVector source,
        CubeVector target,
        int wrapSize
        )
    {
        if (wrapSize < 0)
        {
            throw new ArgumentException(
                      "The wrap size must be greater than or equal to 0."
                      );
        }

        int unwrappedDistance = UnwrappedHexTileDistance(source, target);

        CubeVector rightWrapped =
            new CubeVector(
                target._x + wrapSize,
                target._z
                );

        int rightWrappedDistance = UnwrappedHexTileDistance(source, rightWrapped);

        if (rightWrappedDistance < unwrappedDistance)
        {
            return(true);
        }

        return(false);
    }
コード例 #5
0
 /// <summary>
 /// Get the axial distance between two cube vectors.
 /// </summary>
 /// <param name="source">
 /// The source cube vector.
 /// </param>
 /// <param name="target">
 /// The target cube vector.
 /// </param>
 /// <returns>
 /// The axial distance between two cube vectors.
 /// </returns>
 public float OffsetDistance(
     CubeVector source,
     CubeVector target
     )
 {
     throw new NotImplementedException();
 }
コード例 #6
0
 /// <summary>
 /// Scale the size of a CubeVector by the provided value.
 /// </summary>
 /// <param name="cubeVector"></param>
 /// <param name="scalar"></param>
 /// <returns></returns>
 public static CubeVector Scale(
     CubeVector cubeVector,
     int scalar
     )
 {
     return(cubeVector.Scale(scalar));
 }
コード例 #7
0
 /// <summary>
 /// Get the cubic distance between two cube vectors.
 /// </summary>
 /// <param name="source">
 ///     The source cube vector.
 /// </param>
 /// <param name="target">
 ///     The target cube vector.
 /// </param>
 /// <returns>
 ///     The raw distance between two cube vectors using cube coordinates.
 /// </returns>
 public static float CubicDistance(CubeVector source, CubeVector target)
 {
     return(Mathf.Sqrt(
                Mathf.Pow(source._x - target._x, 2f) +
                Mathf.Pow(source.Y - target.Y, 2f) +
                Mathf.Pow(source._z - target._z, 2f)
                ));
 }
コード例 #8
0
    public CubeVector Plus(CubeVector other)
    {
        CubeVector result = new CubeVector(
            _x + other.X,
            Z + other.Z
            );

        return(result);
    }
コード例 #9
0
    public CubeVector Minus(CubeVector other)
    {
        CubeVector result = new CubeVector(
            X - other.X,
            Z - other.Z
            );

        return(result);
    }
コード例 #10
0
    /// <summary>
    /// Get an index converted from the specified cube coordinates.
    /// </summary>
    /// <param name="x">
    /// The x (right diagonal longitudinal) cube coordinate.
    /// </param>
    /// <param name="y">
    /// The y (left diagonal longitudinal) cube coordinate.
    /// </param>
    /// <param name="z">
    /// The z (latitudinal) cube coordinate.
    /// </param>
    /// <returns>
    /// An index converted from the specified cube coordinates.
    /// </returns>
    private int ConvertCubeToIndex(int x, int y, int z)
    {
        Vector2 offsetIndex = CubeVector.CubeToOffset(x, z);

        return(ConvertOffsetToIndex(
                   (int)offsetIndex.x,
                   (int)offsetIndex.y
                   ));
    }
コード例 #11
0
    /// <summary>
    /// Get a hex direction pointing from one CubeVector to another,
    /// where the provided CubeVectors are considered to be
    /// coordinates on a plane.
    /// </summary>
    /// <param name="from">
    /// The origin of the direction.
    /// </param>
    /// <param name="to">
    /// The destination of the direction.
    /// </param>
    /// <returns>
    /// A hex direction pointing from one CubeVector to another,
    /// where the provided CubeVectors are considered to be
    /// coordinates on a plane.
    /// </returns>
    public static HexDirections HexDirection(
        CubeVector from,
        CubeVector to
        )
    {
        CubeVector direction = CubeDirection(from, to);

        return(HexDirectionInternal(direction));
    }
コード例 #12
0
    /// <summary>
    /// Get a hex direction pointing from one CubeVector to another,
    /// where the provided CubeVectors are considered to be
    /// coordinates on a plane.
    /// </summary>
    /// <param name="from">
    /// The origin of the direction.
    /// </param>
    /// <param name="to">
    /// The destination of the direction.
    /// </param>
    /// <param name="wrapOffsetX">
    /// The offset required to wrap cube coordinates around the
    /// x axis. Should be set to the width of the bounds of the
    /// plane which the coordinate is located on.
    /// </param>
    /// <returns>
    /// A hex direction pointing from one CubeVector to another,
    /// where the provided CubeVectors are considered to be
    /// coordinates on a plane.
    /// </returns>
    public static HexDirections HexDirectionWrapping(
        CubeVector from,
        CubeVector to,
        int wrapOffsetX
        )
    {
        CubeVector direction = CubeDirectionWrapping(from, to, wrapOffsetX);

        return(HexDirectionInternal(direction));
    }
コード例 #13
0
    /// <summary>
    /// Get the distance in hex tiles between two cube vectors
    /// representing tiles on a hex grid.
    /// </summary>
    /// <param name="source">
    /// A cube vector representing the source tile.
    /// </param>
    /// <param name="target">
    /// A cube vector representing the target tile.
    /// </param>
    /// <returns>
    /// The distance in hex tiles between the source tile and the
    /// target tile.
    /// </returns>
    public static int UnwrappedHexTileDistance(
        CubeVector source,
        CubeVector target
        )
    {
        int result =
            (int)Mathf.Floor(
                CubicDistance(source, target)
                );

        return(result);
    }
コード例 #14
0
    public static ElevationDigraph FromHexGrid(
        HexGrid <Hex> hexGrid
        )
    {
        List <ElevationEdge> edges =
            new List <ElevationEdge>();

        for (
            int i = 0;
            i < hexGrid.Rows * hexGrid.Columns;
            i++
            )
        {
            Hex current = hexGrid[i];

            foreach (
                Hex neighbor in hexGrid.GetNeighbors(i)
                )
            {
                ElevationEdge outEdge = new ElevationEdge(
                    current,
                    neighbor,
                    CubeVector.HexDirectionWrapping(
                        current.CubeCoordinates,
                        neighbor.CubeCoordinates,
                        hexGrid.WrapSize
                        )
                    );

                edges.Add(outEdge);

                ElevationEdge inEdge = new ElevationEdge(
                    neighbor,
                    current,
                    CubeVector.HexDirectionWrapping(
                        neighbor.CubeCoordinates,
                        current.CubeCoordinates,
                        hexGrid.WrapSize
                        )
                    );

                edges.Add(inEdge);
            }
        }

        ElevationDigraph result =
            new ElevationDigraph();

        result.AddVerticesAndEdgeRange(edges);
        return(result);
    }
コード例 #15
0
    public override void OnGUI(
        Rect position,
        SerializedProperty property,
        GUIContent label
        )
    {
        CubeVector cubeVector = new CubeVector(
            property.FindPropertyRelative("_x").intValue,
            property.FindPropertyRelative("_z").intValue
            );

        position = EditorGUI.PrefixLabel(position, label);
        GUI.Label(position, cubeVector.ToString());
    }
コード例 #16
0
    /// <summary>
    /// Gets the distance between to hex tiles.
    /// </summary>
    /// <param name="a">
    /// A cube vector representing the first hex tile.
    /// </param>
    /// <param name="b">
    /// A cube vector representing the second hex tile.
    /// </param>
    /// <param name="wrapOffsetX">
    /// The offset required to wrap cube coordinates around the x
    /// axis. Should be set to the width of the bounds of the plane
    /// which the coordinate is located on.
    /// </param>
    /// <returns></returns>
    public static int WrappedHexTileDistance(
        CubeVector a,
        CubeVector b,
        int wrapOffsetX
        )
    {
        if (wrapOffsetX < 0)
        {
            throw new ArgumentException(
                      "The wrap size must be greater than 0."
                      );
        }

        int unwrappedDistance = UnwrappedHexTileDistance(a, b);

        CubeVector rightWrapped =
            new CubeVector(
                b._x + wrapOffsetX,
                b._z
                );

        int rightWrappedDistance = UnwrappedHexTileDistance(
            a,
            rightWrapped
            );

        if (rightWrappedDistance < unwrappedDistance)
        {
            return(rightWrappedDistance);
        }

        CubeVector leftWrapped =
            new CubeVector(
                b._x - wrapOffsetX,
                b._z
                );

        int leftWrappedDistance = UnwrappedHexTileDistance(
            a,
            leftWrapped
            );

        if (leftWrappedDistance < unwrappedDistance)
        {
            return(leftWrappedDistance);
        }

        return(unwrappedDistance);
    }
コード例 #17
0
// ~~ private

// INDEXERS ~~~~~~~~~~

// ~ Static

// ~~ public

// ~~ private

// ~ Non-Static

// ~~ public

// ~~ private

// METHODS ~~~~~~~~~

// ~ Static

// ~~ public
    public static Hex Instantiate(
        int offsetX,
        int offsetZ,
        int wrapSize
        )
    {
        GameObject resultObj  = new GameObject("Hex");
        Hex        resultMono = resultObj.AddComponent <Hex>();

        resultMono.CubeCoordinates = CubeVector.FromOffsetCoordinates(
            offsetX,
            offsetZ,
            wrapSize
            );

        return(resultMono);
    }
コード例 #18
0
    public override bool Equals(object other)
    {
        if (other is CubeVector)
        {
            CubeVector otherVec = (CubeVector)other;
            if (
                otherVec.X == X &&
                otherVec.Z == Z &&
                otherVec.Y == Y
                )
            {
                return(true);
            }
        }

        return(false);
    }
コード例 #19
0
    private static HexDirections HexDirectionInternal(
        CubeVector vector
        )
    {
        if (vector.Equals(Northeast))
        {
            return(HexDirections.Northeast);
        }

        if (vector.Equals(Northwest))
        {
            return(HexDirections.Northwest);
        }

        if (vector.Equals(Southeast))
        {
            return(HexDirections.Southeast);
        }

        if (vector.Equals(Southwest))
        {
            return(HexDirections.Southwest);
        }

        if (vector.Equals(East))
        {
            return(HexDirections.East);
        }

        if (vector.Equals(West))
        {
            return(HexDirections.West);
        }

        throw new NotImplementedException(
                  "There is no matching hex direction for the specified " +
                  "cube vector:\n\t" + vector
                  );
    }
コード例 #20
0
    public Hex GetHex(
        Vector3 position,
        float hexOuterRadius
        )
    {
        if (HexGrid == null)
        {
            throw new System.NullReferenceException(
                      "HexMap has not been initialized with a HexGrid."
                      );
        }

        position = transform.InverseTransformPoint(position);

        CubeVector coordinates =
            CubeVector.FromVector3(
                position,
                hexOuterRadius,
                HexGrid.WrapSize
                );

        return(GetHex(coordinates));
    }
コード例 #21
0
    /// <summary>
    /// Get a CubeVector of length 1 representing the direction from
    /// one cube vector to antoher, where the provided cube vectors
    /// are considered to be coordinates on a plane.
    /// </summary>
    /// <param name="from">
    /// The origin of the direction.
    /// </param>
    /// <param name="to">
    /// The destination of the direction.
    /// </param>
    /// <param name="wrapOffsetX">
    /// The offset required to wrap cube coordinates around the
    /// x axis. Should be set to the width of the bounds of the
    /// plane which the coordinate is located on.
    /// </param>
    /// <returns>
    /// A CubeVector of length 1 representing the direction from
    /// one cube vector to antoher, where the provided cube vectors
    /// are considered to be coordinates on a plane.
    /// </returns>
    public static CubeVector CubeDirectionWrapping(
        CubeVector from,
        CubeVector to,
        int wrapOffsetX
        )
    {
        if (
            IsWrappingRight(
                from, to, wrapOffsetX
                )
            )
        {
            CubeVector toWrapRight =
                new CubeVector(
                    to.X + wrapOffsetX,
                    to.Z
                    );

            return(CubeDirection(from, toWrapRight));
        }
        else if (
            IsWrappingLeft(
                from, to, wrapOffsetX
                )
            )
        {
            CubeVector toWrapLeft =
                new CubeVector(
                    to.X - wrapOffsetX,
                    to.Z
                    );

            return(CubeDirection(from, toWrapLeft));
        }

        return(CubeDirection(from, to));
    }
コード例 #22
0
    /// <summary>
    /// Gets a list of elements adjacent to the element at the specified index.
    /// </summary>
    /// <param name="rowMajorIndex">
    /// The index of the element whose neighbors should be returned.
    /// </param>
    /// <returns>
    /// A list of elements adjacent to the specified index.
    /// </returns>
    public List <T> GetNeighbors(int rowMajorIndex)
    {
        List <T> result = new List <T>();

        if (IsInBounds(rowMajorIndex))
        {
            int row    = GetRowIndex(rowMajorIndex);
            int column = GetOffsetColumnIndex(rowMajorIndex);

            T origin = GetElement(rowMajorIndex);

            HexDirections currentDirection = HexDirections.Southwest;

            for (
                int neighborRow = row - 1;
                neighborRow <= row + 1;
                neighborRow++
                )
            {
                for (
                    int neighborColumn = column - 1;
                    neighborColumn <= column + 1;
                    neighborColumn++
                    )
                {
                    if (neighborRow == row && neighborColumn == column)
                    {
                        continue;
                    }

                    try {
                        int neighborIndex = ConvertOffsetToIndex(
                            neighborColumn, neighborRow
                            );

                        T neighborCandidate =
                            this[neighborIndex];

                        if (
                            CubeVector.WrappedHexTileDistance(
                                origin.CubeCoordinates,
                                neighborCandidate.CubeCoordinates,
                                WrapSize
                                ) == 1
                            )
                        {
                            result.Add(
                                neighborCandidate
                                );

                            currentDirection =
                                currentDirection.NextClockwise();
                        }
                    }
                    catch (OutOfGridBoundsException) {
                        /*RootLog.Log(
                         *  "Row: " + neighborRow + ", Column: " + neighborColumn + " is " +
                         *  "outside of the grid bounds.",
                         *  Severity.Warning,
                         *  "HexGrid.Neighbors"
                         * );*/
                    }
                }
            }
        }

        return(result);
    }
コード例 #23
0
    private int SinkTerrain(
        int landBudget,
        int maximumRegionDensity,
        MapRegionRect region,
        float highRiseProbability,
        int elevationMin,
        int globalWaterLevel,
        float jitterProbability,
        float hexOuterRadius
        )
    {
        int result = landBudget;
        PriorityQueue <Hex> open   = new PriorityQueue <Hex>();
        List <Hex>          closed = new List <Hex>();

        // Get a random hex within the region bounds to be the first hex
        // searched.
        Hex firstHex = GetRandomHex(region);

        open.Enqueue(firstHex, 0);
        CubeVector center        = firstHex.CubeCoordinates;
        int        sink          = Random.value < highRiseProbability ? 2 : 1;
        int        regionDensity = 0;

        while (
            regionDensity < maximumRegionDensity &&
            open.Count > 0
            )
        {
            Hex current = open.Dequeue();
            closed.Add(current);

            int originalElevation = current.elevation;

            int newElevation = current.elevation - sink;

            if (newElevation < elevationMin)
            {
                continue;
            }

            current.SetElevation(
                newElevation,
                hexOuterRadius,
                _hexMap.WrapSize
                );

            if (
                originalElevation >= globalWaterLevel &&
                newElevation < globalWaterLevel
                )
            {
                result++;
            }

            regionDensity += 1;

            List <Hex> neighbors;

            if (_hexMap.TryGetNeighbors(current, out neighbors))
            {
                foreach (Hex neighbor in neighbors)
                {
                    if (closed.Contains(neighbor))
                    {
                        continue;
                    }

                    int priority =
                        CubeVector.WrappedHexTileDistance(
                            neighbor.CubeCoordinates,
                            center,
                            _hexMap.WrapSize
                            ) +
                        Random.value < jitterProbability ? 1 : 0;

                    open.Enqueue(
                        neighbor,
                        priority
                        );
                }
            }
        }

        return(result);
    }
コード例 #24
0
 public CubeVector Dot(CubeVector other)
 {
     throw new NotImplementedException();
 }