示例#1
0
    private void Update()
    {
        if (turn != 0)         //If game started
        {
            //Find closest point
            int closestCol = 0;

            Ray ray = camera.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit) && hit.transform.CompareTag("Board"))
            {
                float minDistance = float.MaxValue;

                for (int i = 0; i < boardSize.y; i++)
                {
                    Debug.DrawLine(hit.point, bPos[0, i]);
                    if (Vector3.Distance(hit.point, bPos[0, i]) < minDistance)
                    {
                        minDistance = Vector3.Distance(hit.point, bPos[0, i]);
                        closestCol  = i;
                    }
                }

                if (boardController.board[boardSize.x - 1, closestCol] != 0)                 // Check if col is full
                {
                    canPlace = false;
                }
                else if (history.Count == numTurns - 1 && !hasWon)                 //Cant place if rewinding or has won
                {
                    canPlace = true;
                }
            }
            else
            {
                canPlace = false;                 //If mouse is not on board, can't place
            }
            //Until here we have the closest col to the mouse and a bool canPlace.
            if (canPlace)
            {
                Vector2Int placePos = new Vector2Int(boardController.ApplyGravity(closestCol).x, closestCol);
                placeholder.transform.position = bPos[placePos.x, placePos.y];                 //Put placeholder in position
                placeholder.transform.rotation = bRot[placePos.x, placePos.y];

                if (Input.GetMouseButtonDown(0) && boardController.board[3, closestCol] == 0)                 //When clicked and column is not full
                {
                    PlaceActualObject(closestCol, turn);
                }
            }

            if (Input.GetKeyDown("left") && numTurns > 1)               //Go back
            {
                boardController.RemoveObject(history[numTurns - 2]);

                numTurns--;
                canPlace = false;
                UpdateBoard();
            }

            if (Input.GetKeyDown("right") && numTurns < history.Count + 1)               //Go fowards
            {
                boardController.PlaceObject(history[numTurns - 1], GetTurn(turn));
                numTurns++;
                if (history.Count == numTurns - 1)
                {
                    if (!hasWon)
                    {
                        turn = GetTurn(numTurns);
                    }
                }

                UpdateBoard();
            }

            placeholder.gameObject.SetActive(canPlace);             //Set preview according to if mouse is on board

            if (!hasWon)
            {
                timerValue[turn - 1] -= Time.deltaTime;

                if (timerValue[turn - 1] < 0)
                {
                    WinGame(GetTurn(numTurns++), new BoardController.WinObj());
                }
                else
                {
                    timerText.text = FormatText(timerValue);
                }
            }
        }
        afterGameUI.SetActive(hasWon);
    }