Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        if (startOfTurn && Input.anyKeyDown)
        {
            hexController.ClearLastMove();    //Clear all highlights after first action
            startOfTurn = false;
        }

        if (selectionMode && myTurn)
        {
            bool boardSelection = false;
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray;
                if (dugoutCamera.pixelRect.Contains(Input.mousePosition))   //If mouse position is in the dugout region
                {
                    ray = dugoutCamera.ScreenPointToRay(Input.mousePosition);
                }
                else //Otherwise mouse is in the board region
                {
                    ray            = Camera.main.ScreenPointToRay(Input.mousePosition);
                    boardSelection = true;
                }

                RaycastHit rch;

                if (Physics.Raycast(ray, out rch, Mathf.Infinity, pieceLayers))
                {
                    if (rch.transform.gameObject.tag.Equals(eligibleColor))  //Check that it's the eligible color for this turn
                    {
                        //If only queen is eligible then check that unit type is queen
                        if (!onlyQueen || (onlyQueen && rch.transform.gameObject.GetComponent <SpecificBehavior>().type == BoardManager.UnitType.Queen))
                        {
                            //If only placement is allow then make sure that selected piece is in the dugout
                            if (!onlyPlacementAllowed || (onlyPlacementAllowed && rch.transform.gameObject.GetComponent <SpecificBehavior>().active == BoardManager.Active.Dugout))
                            {
                                //Debug.Log(rch.transform.name);
                                selectedPiece = rch.transform.gameObject;

                                if (phase == Phase.FirstPlacement)   //If this is the first placement of the game
                                {
                                    selectedPiece.transform.GetComponent <GeneralPieceMovement>().SelectPiece(1);
                                    selectionMode = false;
                                    dragMode      = true;
                                }
                                else if (phase == Phase.SecondPlacement)    //If this is the second placement of the game
                                {
                                    selectedPiece.transform.GetComponent <GeneralPieceMovement>().SelectPiece(2);
                                    selectionMode = false;
                                    dragMode      = true;
                                }

                                else if (boardSelection && selectedPiece.transform.GetComponent <SpecificBehavior>().IsPieceMoveable())  //For board selection, only allow piece to be selected if it is not covered and is on moveable list
                                {
                                    selectedPiece.transform.GetComponent <GeneralPieceMovement>().SelectPiece(3);
                                    selectionMode = false;
                                    dragMode      = true;
                                }
                                else if (!boardSelection)   //If selecting from dugout, then don't need to check for valid moveable pieces
                                {
                                    selectedPiece.transform.GetComponent <GeneralPieceMovement>().SelectPiece(3);
                                    selectionMode = false;
                                    dragMode      = true;
                                }
                            }
                        }
                    }
                }
            }
        }

        //Follow the mouse while piece is selected
        if (dragMode)
        {
            if (!Input.GetMouseButtonUp(0))
            {
                Ray ray;
                if (dugoutCamera.pixelRect.Contains(Input.mousePosition))
                {
                    ray = dugoutCamera.ScreenPointToRay(Input.mousePosition);
                }
                else
                {
                    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                }

                RaycastHit rch;

                if (Physics.Raycast(ray, out rch, Mathf.Infinity, MoveLayer))
                {
                    Vector3 loc = rch.point;

                    //Debug.Log(loc);
                    selectedPiece.transform.position = loc;
                }
            }

            //Drop piece
            else
            {
                selectedPiece.transform.GetComponent <GeneralPieceMovement>().UnSelectPiece();
                dragMode = false;
                //Keep selection mode false
            }
        }
    }