Exemple #1
0
        // GetMovePositions(): given MoveType, MoveStep, currentCell, return list of Cells representing available moves
        // .. return Vector2Int to decouple PieceManager and Board
        // .. assume currentCell is valid
        List <Vector2Int> GetMovePositions(BasePiece.MoveType moveType, BasePiece.MoveStep moveStep,
                                           Cell currentCell, Board.Direction frontDirection)
        {
            Debug.Log("PieceManager::GetMovePositions()");

            List <Vector2Int> result          = new List <Vector2Int>();
            Vector2Int        currentPosition = currentCell.GetBoardPosition();

            if (moveType == BasePiece.MoveType.Vertical)
            {
                List <Vector2Int> topPositions = GetDirectionPositions(Board.Direction.N, moveStep, currentCell, frontDirection);
                topPositions.ForEach(position => result.Add(position));
                List <Vector2Int> bottomPositions = GetDirectionPositions(Board.Direction.S, moveStep, currentCell, frontDirection);
                bottomPositions.ForEach(position => result.Add(position));
            }
            else if (moveType == BasePiece.MoveType.Diagonal)
            {
                List <Vector2Int> topLeftPositions = GetDirectionPositions(Board.Direction.NW, moveStep, currentCell, frontDirection);
                topLeftPositions.ForEach(position => result.Add(position));
                List <Vector2Int> topRightPositions = GetDirectionPositions(Board.Direction.NE, moveStep, currentCell, frontDirection);
                topRightPositions.ForEach(position => result.Add(position));

                List <Vector2Int> bottomRightPositions = GetDirectionPositions(Board.Direction.SE, moveStep, currentCell, frontDirection);
                bottomRightPositions.ForEach(position => result.Add(position));
                List <Vector2Int> bottomLeftPositions = GetDirectionPositions(Board.Direction.SW, moveStep, currentCell, frontDirection);
                bottomLeftPositions.ForEach(position => result.Add(position));
            }
            else if (moveType == BasePiece.MoveType.Horizontal)
            {
                List <Vector2Int> leftPositions = GetDirectionPositions(Board.Direction.W, moveStep, currentCell, frontDirection);
                leftPositions.ForEach(position => result.Add(position));

                List <Vector2Int> rightPositions = GetDirectionPositions(Board.Direction.E, moveStep, currentCell, frontDirection);
                rightPositions.ForEach(position => result.Add(position));
            }
            else if (moveType == BasePiece.MoveType.L)
            {
                List <Vector2Int> lPositions = GetLPositions(currentCell);
                lPositions.ForEach(position => result.Add(position));
            }
            else if (moveType == BasePiece.MoveType.Pawn)
            {
                List <Vector2Int> pawnPositions = GetPawnPositions(currentCell, frontDirection);

                pawnPositions.ForEach(position => result.Add(position));
            }

            return(result);
        }
Exemple #2
0
        // GetDirectionPositions(): given Direction, current cell, moveStep, return all available positions
        List <Vector2Int> GetDirectionPositions(Board.Direction direction, BasePiece.MoveStep moveStep, Cell currentCell, Board.Direction frontDirection)
        {
            List <Vector2Int> result          = new List <Vector2Int>();
            Vector2Int        currentPosition = currentCell.GetBoardPosition();
            BasePiece         currentPiece    = currentCell.GetCurrentPiece();

            // determine direction to step to
            Vector2Int offset = Vector2Int.zero;

            if (direction == Board.Direction.NW)
            {
                offset = new Vector2Int(-1, 1);
            }
            else if (direction == Board.Direction.N)
            {
                offset = Vector2Int.up;
            }
            else if (direction == Board.Direction.NE)
            {
                offset = new Vector2Int(1, 1);
            }
            else if (direction == Board.Direction.E)
            {
                offset = Vector2Int.right;
            }
            else if (direction == Board.Direction.SE)
            {
                offset = new Vector2Int(1, -1);
            }
            else if (direction == Board.Direction.S)
            {
                offset = Vector2Int.down;
            }
            else if (direction == Board.Direction.SW)
            {
                offset = new Vector2Int(-1, -1);
            }
            else if (direction == Board.Direction.W)
            {
                offset = Vector2Int.left;
            }

            // determine front direction adjustment
            Vector2Int frontOffset = Vector2Int.zero;

            if (frontDirection == Board.Direction.N)
            {
                frontOffset = Vector2Int.one;
            }
            else if (frontDirection == Board.Direction.S)
            {
                frontOffset = new Vector2Int(1, -1);
            }

            offset = Vector2Int.Scale(offset, frontOffset);

            // determine how many times we are stepping
            int stepNum = 0;

            if (moveStep == BasePiece.MoveStep.Single)
            {
                stepNum = 1;
            }
            else if (moveStep == BasePiece.MoveStep.Double)
            {
                stepNum = 2;
            }
            else if (moveStep == BasePiece.MoveStep.Multiple)
            {
                stepNum = -1;
            }

            while (stepNum != 0)
            {
                Vector2Int nextPosition = currentPosition + offset;
                if (!Board.IsOutOfBound(nextPosition) && mBoard.IsTargetCellValid(currentPiece, nextPosition))
                {
                    result.Add(nextPosition);

                    // cannot advance if path is blocked by another piece
                    if (!mBoard.IsCellEmpty(nextPosition))
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }

                currentPosition = nextPosition;
                stepNum        -= 1;
            }

            return(result);
        }