Exemplo n.º 1
0
    void Update()
    {
        //Button interactability
        UpdateButtonStatus();

        //Controls for menu
        if (Input.GetKeyUp(controls.toggleMenu) && GameManager.IsGameOngoing() && !display.promotionUI.activeSelf)
        {
            if (display.rulesUI.activeSelf)
            {
                display.rulesUI.SetActive(false);
            }
            else if (display.menuUI.activeSelf)
            {
                display.menuUI.SetActive(false);
                display.ingameUI.SetActive(true);
            }
            else
            {
                display.menuUI.SetActive(true);
                display.ingameUI.SetActive(false);
            }
        }

        if (display.ingameUI.activeSelf)
        {
            //Controls for rotating
            if (Input.GetMouseButtonDown(controls.mouseButtonRotate))
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible   = false;
            }
            else if (Input.GetMouseButtonUp(controls.mouseButtonRotate))
            {
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible   = true;
            }
            else if (Input.GetMouseButton(controls.mouseButtonRotate))
            {
                Vector2 move = new Vector2(
                    Input.GetAxis("Mouse Y"),
                    Input.GetAxis("Mouse X")
                    ) * interaction.rotationSpeed * Time.deltaTime;

                transform.RotateAround(interaction.center, transform.up, move.y);
                transform.RotateAround(interaction.center, -transform.right, move.x);
            }

            //Controls for zooming
            if (Application.isFocused)
            {
                if (Input.mouseScrollDelta.y != 0)
                {
                    if (Input.mouseScrollDelta.y > 0 && Vector3.Distance(transform.position, interaction.center) <= interaction.minZoom)
                    {
                        return;
                    }
                    else if (Input.mouseScrollDelta.y < 0 && Vector3.Distance(transform.position, interaction.center) >= interaction.maxZoom)
                    {
                        return;
                    }

                    transform.position = Vector3.MoveTowards(transform.position, interaction.center, Input.mouseScrollDelta.y * interaction.zoomSpeed * Time.deltaTime);
                }
            }

            //Controls for jail
            if (jail.gameObject.activeSelf && !BoardAnimation.Active())
            {
                if (Input.GetMouseButtonDown(controls.mouseButtonSelect))
                {
                    //Find the object the mouse is over
                    RaycastHit hit;
                    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
                    {
                        //Check if its a piece and hold it at the same distance if so
                        if (hit.transform.GetComponent <Piece>())
                        {
                            selection.heldPiece    = hit.transform.gameObject;
                            selection.heldDistance = Vector3.Distance(Camera.main.transform.position, selection.heldPiece.gameObject.transform.position);
                            selection.heldOffset   = selection.heldPiece.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selection.heldDistance));
                        }
                    }
                }
                else if (Input.GetMouseButtonUp(controls.mouseButtonSelect))
                {
                    //Stop holding piece
                    selection.heldPiece = null;
                }

                //Update the pieces position so it follows the cursor and maintains its velocity
                if (selection.heldPiece)
                {
                    selection.heldPiece.transform.position = selection.heldOffset + Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selection.heldDistance));
                    selection.heldPiece.transform.GetComponent <Rigidbody>().velocity = Input.mousePosition - selection.lastMouse;
                    selection.lastMouse = Input.mousePosition;
                }
            }

            //Controls for selecting on board (only possible when no animation is being undergone)
            if (GameManager.IsTurn(team) && !jail.gameObject.activeSelf && Input.GetMouseButtonDown(controls.mouseButtonSelect) && !BoardAnimation.Active())
            {
                RaycastHit hit;
                if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
                {
                    string location = null;

                    if (hit.transform.GetComponent <Piece>())
                    {
                        location = hit.transform.GetComponent <Piece>().GetLocation();
                    }
                    else if (hit.transform.parent && hit.transform.parent.GetComponent <Tile>())
                    {
                        location = hit.transform.name;
                    }

                    if (!string.IsNullOrEmpty(location))
                    {
                        Piece existingPiece = GameManager.board.GetPiece(location);

                        if (selection.selectedPiece)
                        {
                            //Decolor paths
                            SetMovementPathsColor(selection.selectedPiece, null);
                            SetAttackPathsColor(selection.selectedPiece, null);

                            if (existingPiece == selection.selectedPiece)
                            {
                                //Deselect if already selected
                                selection.selectedPiece = null;
                            }
                            else if (existingPiece && existingPiece.team == selection.selectedPiece.team)
                            {
                                //Select new piece and color paths if same team as previously selected
                                selection.selectedPiece = existingPiece;
                                SetMovementPathsColor(existingPiece, BoardConfig.visuals.moveTileColor);
                                SetAttackPathsColor(existingPiece, BoardConfig.visuals.attackTileColor);
                            }
                            else
                            {
                                //Create an instance of the piece to be captured if the move is an attack
                                Piece capturedPiece = null;
                                if (existingPiece)
                                {
                                    capturedPiece = Instantiate(existingPiece);
                                    capturedPiece.SetTransformParent(GameManager.board.GetPlane(location).transform);
                                }

                                Vector3    initialPos = selection.selectedPiece.transform.position;
                                Quaternion initialRot = selection.selectedPiece.transform.rotation;

                                if (GameManager.Move(selection.selectedPiece, location))
                                {
                                    //Animate movement
                                    BoardAnimation.Move(selection.selectedPiece, initialPos, initialRot);

                                    //Animate piece being captured if there was one
                                    if (capturedPiece)
                                    {
                                        BoardAnimation.onComplete = () => {
                                            jail.Add(capturedPiece);
                                        };

                                        BoardAnimation.Capture(capturedPiece);
                                    }

                                    bool shouldPromote = false;

                                    //Check if the piece was a pawn and should be promoted
                                    if (selection.selectedPiece.type == Piece.Type.PAWN)
                                    {
                                        switch (GameManager.board.style)
                                        {
                                        case Chessboard.Style.ANTIPODEAN:                                                 //Promote if the pawn reached an axial tile
                                            shouldPromote = Tile.IsAxial(location);
                                            break;

                                        case Chessboard.Style.SPHERE:                                                 //Promote if the pawn reached the the first or last row
                                            int row = int.Parse("" + location[1]);
                                            shouldPromote = row == BoardConfig.initialRow || row == BoardConfig.finalRow;
                                            break;
                                        }
                                    }

                                    if (shouldPromote)
                                    {
                                        //Show promotion UI if conditions were met
                                        display.ingameUI.SetActive(false);
                                        display.promotionUI.SetActive(true);
                                    }
                                    else
                                    {
                                        //Deselect if piece moved
                                        selection.selectedPiece = null;

                                        //Use turn
                                        GameManager.NextTurn();
                                    }
                                }
                                else
                                {
                                    //Don't animate since a piece wasn't captured
                                    if (capturedPiece)
                                    {
                                        Destroy(capturedPiece.gameObject);
                                    }

                                    //Recolor paths if piece wasn't moved
                                    SetMovementPathsColor(selection.selectedPiece, BoardConfig.visuals.moveTileColor);
                                    SetAttackPathsColor(selection.selectedPiece, BoardConfig.visuals.attackTileColor);
                                }
                            }
                        }
                        else if (existingPiece && (existingPiece.team == team || team == Team.ALL))
                        {
                            //Select and color paths
                            selection.selectedPiece = existingPiece;
                            SetMovementPathsColor(existingPiece, BoardConfig.visuals.moveTileColor);
                            SetAttackPathsColor(existingPiece, BoardConfig.visuals.attackTileColor);
                        }
                    }
                }
            }
        }
    }