Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="gridPos"></param>
        /// <param name="connectionMatrix"></param>
        /// <param name="offset"></param>
        /// <param name="adjacentConnection"></param>
        /// <param name="diagonalConnection">Up->TopRight, Right->BottomRight, Down->BottomLeft, Left->TopLeft</param>
        /// <returns></returns>
        public static void GetConnection(this GridPos gridPos, bool[,] connectionMatrix, GridPos offset, out GridPosR.OrientationFlag adjacentConnection, out GridPosR.OrientationFlag diagonalConnection)
        {
            adjacentConnection = GridPosR.OrientationFlag.None;
            diagonalConnection = GridPosR.OrientationFlag.None;
            GridPos localGP = gridPos + offset;

            if (localGP.x < 0 || localGP.x >= connectionMatrix.GetLength(0) || localGP.z < 0 || localGP.z >= connectionMatrix.GetLength(1))
            {
                return;
            }

            if (localGP.x + 1 < connectionMatrix.GetLength(0) && connectionMatrix[localGP.x + 1, localGP.z])
            {
                adjacentConnection |= GridPosR.OrientationFlag.Right;
            }
            if (localGP.x - 1 >= 0 && connectionMatrix[localGP.x - 1, localGP.z])
            {
                adjacentConnection |= GridPosR.OrientationFlag.Left;
            }
            if (localGP.z + 1 < connectionMatrix.GetLength(1) && connectionMatrix[localGP.x, localGP.z + 1])
            {
                adjacentConnection |= GridPosR.OrientationFlag.Up;
            }
            if (localGP.z - 1 >= 0 && connectionMatrix[localGP.x, localGP.z - 1])
            {
                adjacentConnection |= GridPosR.OrientationFlag.Down;
            }

            if (localGP.x + 1 < connectionMatrix.GetLength(0) && localGP.z + 1 < connectionMatrix.GetLength(1) && connectionMatrix[localGP.x + 1, localGP.z + 1])
            {
                diagonalConnection |= GridPosR.OrientationFlag.Up;
            }
            if (localGP.x + 1 < connectionMatrix.GetLength(0) && localGP.z - 1 >= 0 && connectionMatrix[localGP.x + 1, localGP.z - 1])
            {
                diagonalConnection |= GridPosR.OrientationFlag.Right;
            }
            if (localGP.x - 1 >= 0 && localGP.z - 1 >= 0 && connectionMatrix[localGP.x - 1, localGP.z - 1])
            {
                diagonalConnection |= GridPosR.OrientationFlag.Down;
            }
            if (localGP.x - 1 >= 0 && localGP.z + 1 < connectionMatrix.GetLength(1) && connectionMatrix[localGP.x - 1, localGP.z + 1])
            {
                diagonalConnection |= GridPosR.OrientationFlag.Left;
            }
        }
Пример #2
0
        public static void GetConnectionMatrix(this List <GridPos> gridPositions, out bool[,] connectionMatrix, out GridPos offset)
        {
            connectionMatrix = null;
            offset           = GridPos.Zero;
            if (gridPositions.Count == 0)
            {
                return;
            }

            GridRect gridRect = gridPositions.GetBoundingRectFromListGridPos();

            offset            = -gridRect.position;
            gridRect.position = GridPos.Zero;
            connectionMatrix  = new bool[gridRect.size.x, gridRect.size.z];
            foreach (GridPos gp in gridPositions)
            {
                connectionMatrix[gp.x + offset.x, gp.z + offset.z] = true;
            }
        }