Exemple #1
0
    void Update()
    {
        switch (gameState)
        {
        case GameState.Start:
            if (GameInput.IsTriggered())
            {
                score = 0;
                scorePanel.SetScore(score);
                HidePanels();

                sounds.PlayStartRound();

                //Note: this code is not recommended because it uses reflection.
                Invoke("StartRound", 1);

                gameState = GameState.Idle;
            }

            break;

        case GameState.Playing:
            if (GameInput.IsTriggered())
            {
                sounds.PlayShoot();

                var start   = theCamera.transform.position;
                var end     = start + theCamera.transform.forward * hitDistance;
                var hitInfo = new RaycastHit();
                var hit     = Physics.Linecast(start, end, out hitInfo);

                bullets--;
                scorePanel.SetBullets(bullets);

                if (hit)
                {
                    var colliderHit = hitInfo.collider;
                    if (colliderHit.tag == "Duck")
                    {
                        score++;

                        var duck = colliderHit.GetComponent <Duck>();
                        duck.Hit();
                        sounds.StopFly();

                        scorePanel.SetScore(score);
                        sounds.PlayScoring();

                        if (score < totalDucksPerRound)
                        {
                            gameState = GameState.Scoring;
                            Invoke("StartRound", 1);
                        }
                        else
                        {
                            gameState = GameState.YouWin;
                            Invoke("ShowYouWin", 1);
                        }
                    }
                }
                else if (bullets <= 0)
                {
                    gameState = GameState.GameOver;

                    FlyAway();

                    sounds.StopFly();
                    sounds.PlayLose();

                    Invoke("ShowGameOver", 1);
                }
            }

            break;
        }
    }