Esempio n. 1
0
    public void OnMouseClick()
    {
        //CREATING THE RAY
        var ray = _rayProvider.CreateRay();

        //SHOTING THE RAY
        //shooting a raycast to get the tile that the player clicked
        if (_selector.Check(ray))
        {
            TileField tile;
            _physicsCache.TryGetTileField(_selector.GetSelection(), out tile);
            if (_phaseManager.GetCurrentPhase() == Phase.MOVEMENT)
            {
                //Two possibilities: the field can or not have a piece
                if (tile.Piece != null)
                {
                    // One possibility: If its a piece from the current turn, we try to highlight
                    if (ValidateHighlight(tile.Piece))
                    {
                        // Three possibilities:
                        // If the clicked tile is the current highlighted, we only dehighlight
                        // If the highlighted tile is null, we only highlight
                        // If the clicked tile isn't the current highlighted, we dehighlight and highlight the clicked one
                        if (tile == m_HighlightedTile)
                        {
                            Deselect();
                        }
                        else if (m_HighlightedTile == null)
                        {
                            TrySelectEmptyAdjacents(tile);
                        }
                        else if (tile != m_HighlightedTile)
                        {
                            Deselect();
                            TrySelectEmptyAdjacents(tile);
                        }
                    }
                }
                else
                {
                    //One possibilities: if there is not a piece on the field we clicked, and we already have a highlighted field, we try to move
                    if (m_HighlightedTile != null)
                    {
                        //MOVE PIECE
                        if (tile.IsHighlighting)
                        {
                            //If this tile is highlighted, we can move to this tile.
                            m_HighlightedTile.MovePieceTo(tile);
                            UpdateBoard(tile, m_HighlightedTile);
                            Deselect();
                            _turnManager.NextTurn();
                        }
                    }
                }
            }
            else if (_phaseManager.GetCurrentPhase() == Phase.POSITIONING)
            {
                //One Possibility: There's no piece on the field, we can only move to an empty space
                if (tile.Piece == null)
                {
                    //No piece can be put in the middle on the positioning phase
                    if (tile.Coordinates.x == 2 && tile.Coordinates.y == 2)
                    {
                        return;
                    }

                    if (_turnManager.IsWhiteTurn())
                    {
                        tile.SetPiece(_pieceProvider.GetNonPlacedWhitePiece());
                    }
                    else
                    {
                        tile.SetPiece(_pieceProvider.GetNonPlacedBlackPiece());
                    }

                    _turnIndex++;

                    // Each player places 2 pieces before the next turn
                    if (_turnIndex % 2 == 0)
                    {
                        _turnManager.NextTurn();
                    }

                    //when all pieces are placed
                    if (_turnIndex == 24)
                    {
                        _phaseManager.StartMovementPhase();
                    }
                }
            }
        }
    }
Esempio n. 2
0
    //On click in the game
    public void OnMouseClick()
    {
        //Create a ray from the camera to the mouse position
        var ray = _rayProvider.CreateRay();

        //check if we hit some collider
        if (_selector.Check(ray))
        {
            // if so, we get the correspoding cached tile associated with that collider
            _physicsCache.TryGetTileField(_selector.GetSelection(), out TileField tile);
            if (_phaseManager.Phase == Phase.MOVEMENT)
            {
                //Two possibilities: the field we clicked can/not have a piece
                if (tile.Piece != null) // does have a piece
                {
                    // If its a piece from the current turn, we try to select
                    if (_movementValidator.CanSelect(tile.Piece.type, _turnManager.Turn))
                    {
                        // Three possibilities:
                        // If the clicked tile is the current selected, we deselect
                        // If there's no selected tile, we select
                        // If the clicked tile isn't the current selected, we deselect then select the clicked one
                        if (tile == _movementValidator.SelectedField)
                        {
                            _movementValidator.Deselect();
                        }
                        else if (_movementValidator.SelectedField == null)
                        {
                            _movementValidator.Select(tile);
                        }
                        else if (tile != _movementValidator.SelectedField)
                        {
                            _movementValidator.Deselect();
                            _movementValidator.Select(tile);
                        }
                    }
                }
                else // does not have a piece
                {
                    //One possibility: if there's no piece in the tile clicked, and we already have a tile selected, we try to move
                    if (_movementValidator.SelectedField != null)
                    {
                        //MOVE PIECE
                        if (tile.IsSelected)
                        {
                            //If this tile is highlighted, we can move to this tile.
                            _movementValidator.SelectedField.MovePieceTo(tile);
                            UpdateBoard(tile, _movementValidator.SelectedField);
                            _movementValidator.Deselect();
                            _turnManager.NextTurn();
                        }
                    }
                }
            }
            else if (_phaseManager.Phase == Phase.POSITIONING)
            {
                // If there's no piece on the clicked tile , we can only move to an empty space
                if (tile.Piece == null)
                {
                    //No piece can be put in the middle in the positioning phase
                    if (tile.Coordinates.x == 2 && tile.Coordinates.y == 2)
                    {
                        return;
                    }

                    if (_turnManager.IsWhiteTurn())
                    {
                        tile.SetPiece(_pieceProvider.GetNonPlacedWhitePiece());
                    }
                    else
                    {
                        tile.SetPiece(_pieceProvider.GetNonPlacedBlackPiece());
                    }

                    _turnIndex++;

                    // Each player places 2 pieces before the next turn
                    if (_turnIndex % 2 == 0)
                    {
                        _turnManager.NextTurn();
                    }

                    //when all pieces are placed, we start he movement phase
                    if (_turnIndex == _pieceProvider.PieceCount)
                    {
                        _phaseManager.StartMovementPhase();
                        _gameFinisher.VerifyWall();
                    }
                }
            }
        }
    }