示例#1
0
    ////////////////////////////////////////////////////////////////////////////// UPDATE
    // Update is called once per frame
    void Update()
    {
        /////////////////////////////////////////////////////////////////// SWITCH BALLS (TURN ONLY)
        //if space is pressed, toggle the active ball
        if (Input.GetKeyUp(KeyCode.Space) && gameRules.CurrentState == GameRulesScript.GameState.Turn)
        {
            toggleBall();
        }

        /////////////////////////////////////////////////////////////////// WAIT AFTER UNPAUSING
        /// If the game was paused, wait one frame before unpausing it
        /// So clicking to unpause doesnt also click in the game
        if (gameRules.previousState == GameRulesScript.GameState.Pause)
        {
            print("Unpausing game");
            gameRules.previousState = gameRules.CurrentState;
            return;
        }

        ////////////////////////////////////////////////////////////////// ZOOM OUT/IN/CAMERA CONTROLS
        //if the mouse button is pressed, zoom out to see the whole board
        if (Input.GetMouseButtonDown(1))
        {
            //set the position to the centre of the board, and zoom out
            mainCamera.transform.position = new Vector3(0, 0, -10);
            mainCamera.orthographicSize   = 15;

            //set the parent of the camera to null (so it doesnt follow the ball any more)
            mainCamera.transform.SetParent(null);

            //mark the camera as being zoomed out, so if the active ball changes it doesn't follow
            zoomedOut = true;
        }

        //when the mouse button is released, zoom back in
        else if (Input.GetMouseButtonUp(1))
        {
            //set the parent of the camera to the active ball
            mainCamera.transform.SetParent(activeBall.transform);

            //set the ball's size to normal and the position to that of the active ball
            mainCamera.transform.position =
                new Vector3(mainCamera.transform.parent.position.x, mainCamera.transform.parent.position.y, -10);
            mainCamera.orthographicSize = 5;

            //mark the camera as no longer zoomed out
            zoomedOut = false;
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// TURN/ROQUET/CONTINUATION /////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// Only difference between turn and the bonus turns is whether you can switch balls or not.
        if (gameRules.CurrentState == GameRulesScript.GameState.Roquet || gameRules.CurrentState == GameRulesScript.GameState.Continuation ||
            gameRules.CurrentState == GameRulesScript.GameState.Turn)
        {
            /////////////////////////////////////////////////////////////// HITTING AND DRAWING (TURN/BONUSTURN ONLY)
            //Only do the following if no balls are moving
            if (!ballsMoving)
            {
                ////////////////////////////////////////////////////////// BUILDING UP STRENGTH
                //if the mouse is held down, increase the strength of the incoming hit
                if (Input.GetMouseButton(0))
                {
                    //increase the hit strength
                    incrementHitStrength();

                    //draw the line for the strength of the hit
                    line.ShowHitLine(hitStrength);
                }

                ////////////////////////////////////////////////////////// HIT ACTIVE BALL
                //When the mouse is released, hit the ball with the given strength
                else if (Input.GetMouseButtonUp(0))
                {
                    //save the start positions for all balls, in case this hit is a foul.
                    MarkStartPositions();

                    //randomly adjust the vector angle
                    float   randAngle   = Random.Range(-line.GetMaxAngle(hitStrength), line.GetMaxAngle(hitStrength));
                    Vector2 deflectVect = Quaternion.AngleAxis(randAngle, new Vector3(0, 0, 1)) * line.GetVect();
                    deflectVect.Normalize();

                    //apply a force in the direction of the vector
                    activeBallScript.hitBall(-deflectVect * hitStrength * 10);

                    //reset the strength of the hit and the direction the hit builds
                    hitStrength = 0;
                    hitBuilding = true;

                    //mark the ball as having been hit so we can't move it again
                    ballsMoving = true;
                    lastHit     = Time.realtimeSinceStartup;

                    //hide the line
                    line.Line.enabled = false;
                }
                ////////////////////////////////////////////////////////////////// DRAW DEFAULT LINE
                //if nothing is being held down, draw the default line
                else
                {
                    line.ShowAimLine();
                }
            }    // end of "is the ball moving"
        }        // end of "is it some kind of turn"

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// PLACE BALL////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// The "Placing Ball" mode
        else if (gameRules.CurrentState == GameRulesScript.GameState.PlaceBall && !ballsMoving)
        {
            //////////////////////////////////////////////////////////// PLACE BALL NEXT TO BALL
            /// Place the active ball next to the hit ball, and then when m1 is pressed, leave the ball there and switch state to BonusTurn

            //get the target ball's position
            Vector3 centrePos = gameRules.HitBallToPlaceNextTo.transform.position;

            //get the mouse position in world co-ordinates
            Vector3 posInScreen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
            posInWorld = Camera.main.ScreenToWorldPoint(posInScreen);

            //subtract the co-ords of this point from the ball position
            float x = posInWorld.x - centrePos.x;
            float y = posInWorld.y - centrePos.y;

            //reduce this down to a unit vector from the centre ball in the direction of the mouse
            Vector2 vect = new Vector2(x, y);
            vect = vect.normalized;

            //now set the Active Ball's position to the centre ball's position, plus that normalised vector.
            activeBall.gameObject.transform.position = (Vector2)centrePos + 2 * vect / 3;

            //and just to look nice, draw a line from the mouse to the ball
            line.ShowPlacementLine();

            //Finally, when M1 is released, set the state as "Roquet"
            if (Input.GetMouseButtonUp(0))
            {
                gameRules.CurrentState = GameRulesScript.GameState.Roquet;
            }
        }        // end of "if placing the ball"

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// END TURN //////////////////////////////////////////////////////////////////////////////////////////////////////
        /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// If the turn has ended, pause for a second before continuing
        if (gameRules.CurrentState == GameRulesScript.GameState.EndTurn)
        {
            //three seconds after the end of the turn, pick the next state and move to it, starting the next turn
            if (Time.fixedTime >= timeLastTurnEnded + (float)3.0)
            {
                gameRules.PickNextState();
            }

            //two seconds after the end of the turn, reset the positions of the foul balls
            else if (Time.fixedTime >= timeLastTurnEnded + (float)2.0)
            {
                FoulBallCleanup();
                Debug.Log("cleaning fouls");
            }
        }
        else
        {
            //if the turn has not ended, check if all balls have stopped
            if (CheckAllBallsStopped())
            {
                //if they have, save the time the turn ended so we can wait before the next turn
                timeLastTurnEnded = Time.fixedTime;
                ballsMoving       = false;

                //change state to "EndTurn" for the wait
                gameRules.EndTurn();
            }
        }


        //////////////////////////////////////////////////////////////////// PAUSE GAME
        /// if escape is pressed, pause the game
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            gameRules.Pause();
        }
    }