Example #1
0
        public void movePiece(string pieceName, RaycastHit hit)
        {
            if ((hit.transform.gameObject.tag == pieceName) && changeTurn)
            {
                //if new piece is clicked on
                if (hit.transform.gameObject.GetComponent <CheckerPieceTs>().getPosition() != currentPiece)
                {
                    //record position of piece currently clicked on
                    currentPiece[0] = hit.transform.gameObject.GetComponent <CheckerPieceTs>().getPosition()[0];
                    currentPiece[1] = hit.transform.gameObject.GetComponent <CheckerPieceTs>().getPosition()[1];
                    //remove previous yellow tiles
                    board.setOriginalColors();
                }

                List <GameObject> Lbc = new List <GameObject>();
                Lbc         = hit.transform.gameObject.GetComponent <CheckerPieceTs>().showValidMoves(board);
                globalValid = Lbc;

                for (int i = 0; i < Lbc.Count; i++)
                {
                    Lbc[i].GetComponent <SpriteRenderer>().color = Color.yellow;
                }
            }

            if (hit.transform.gameObject.tag == "Square")
            {
                //if a yellow tile was clicked on
                if (board.squares[hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[0], hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[1]].GetComponent <SpriteRenderer>().color == Color.yellow)
                {
                    string toForce = "no";

                    if (forceCapture)
                    {
                        toForce = "yes";
                    }

                    if (!forceCapture && toForce == "yes")
                    {
                        forceCapture = true;
                    }

                    if (forceCapture && toForce == "no")
                    {
                        forceCapture = false;
                    }

                    //Usually, we're able to take turns after each move
                    changeTurn = true;

                    board.getPieceOnBoard(currentPiece[0], currentPiece[1]).GetComponent <PieceMovementTs>().move(board.getPieceOnBoard(currentPiece[0], currentPiece[1]), hit.transform.gameObject.GetComponent <BoardCellTs>());
                    board.setPieceOnBoard(board.getPieceOnBoard(currentPiece[0], currentPiece[1]), hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[0], hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[1]);
                    board.clearPieceOnBoard(currentPiece[0], currentPiece[1]); //empty old space

                    //if the difference in y between the old and new space is two spaces, that means a jump was made
                    if (Mathf.Abs(hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[1] - currentPiece[1]) == 2)
                    {
                        //Eliminate piece that was jumped over
                        eliminatePiece(hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[0], hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[1], currentPiece[0], currentPiece[1]);

                        //after jumping set original colors
                        board.setOriginalColors();

                        //can we jump again?
                        List <GameObject> jumpAgain = new List <GameObject>();
                        jumpAgain = hit.transform.gameObject.GetComponent <BoardCellTs>().getPiece().showValidMoves(board);

                        foreach (GameObject jb in jumpAgain)
                        {
                            if (Mathf.Abs(jb.GetComponent <BoardCellTs>().getPosition()[1] - hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[1]) == 2)
                            {
                                jb.GetComponent <SpriteRenderer>().color = Color.yellow;
                                oldPiece[0]     = currentPiece[0];
                                oldPiece[1]     = currentPiece[1];
                                currentPiece[0] = hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[0];
                                currentPiece[1] = hit.transform.gameObject.GetComponent <BoardCellTs>().getPosition()[1];
                                changeTurn      = false;
                            }
                        }
                    }

                    if (changeTurn)
                    {
                        //c.Send("CMOV|" + hit.transform.gameObject.GetComponent<BoardCellTs>().getPosition()[0] + "|" + hit.transform.gameObject.GetComponent<BoardCellTs>().getPosition()[1] + "|" + currentPiece[0] + "|" + currentPiece[1] + "|" + "yes" + "|" + toForce);

                        //remove previous yellow tiles (if were changing turns)
                        board.setOriginalColors();
                    }
                    else
                    {
                        //c.Send("CMOV|" + hit.transform.gameObject.GetComponent<BoardCellTs>().getPosition()[0] + "|" + hit.transform.gameObject.GetComponent<BoardCellTs>().getPosition()[1] + "|" + oldPiece[0] + "|" + oldPiece[1] + "|" + "no" + "|" + toForce);
                    }
                }
            }
        }