示例#1
0
    void Update()
    {
        // Frame counter code, every update per frame the the total frames shown will be displayed
        fpsCount++;
        fpsTimer += Time.deltaTime;
        if (fpsTimer >= 1.0f)
        {
            fpsText.text = "FPS: " + fpsCount;
            fpsCount     = 0;
            fpsTimer    -= 1;
        }


        switch (m_GameState)
        {
        case GameState.Start:
            if (Input.GetKeyUp(KeyCode.Return) == true)
            {
                m_TimerText.gameObject.SetActive(true);
                m_MessageText.text = "";
                m_GameState        = GameState.Playing;
                for (int i = 0; i < m_Characters.Length; i++)
                {
                    m_Characters[i].SetActive(true);
                }
            }
            break;

        case GameState.Playing:
            bool isGameOver = false;

            m_gameTime += Time.deltaTime;
            int seconds = Mathf.RoundToInt(m_gameTime);
            m_TimerText.text = string.Format("{0:D2}:{1:D2}",
                                             (seconds / 60), (seconds % 60));


            if (OneTankLeft() == true)
            {
                isGameOver = true;
            }
            if (IsPlayerDead() == true)
            {
                isGameOver = true;
            }
            if (IsEnemyDead() == true)
            {
                isGameOver = true;
            }
            if (isGameOver == true && m_Characters[1] != null)
            {
                m_GameState = GameState.GameOver;
                m_TimerText.gameObject.SetActive(false);

                m_NewGameButton.gameObject.SetActive(true);
                m_HighScoresButton.gameObject.SetActive(true);

                if (IsPlayerDead() == true)
                {
                    m_MessageText.text = "MISSION FAILED, YOU'LL GET 'EM NEXT TIME";
                }
                else if (IsEnemyDead() == true)
                {
                    m_MessageText.text = "YOU HAVE ELIMINATED THE TAGERT!";
                    // The last section of code is going to save the highscore
                    m_HighScores.Addscore(Mathf.RoundToInt(m_gameTime));
                    m_HighScores.SaveScoresToFile();
                }
            }
            break;

        case GameState.GameOver:
            if (Input.GetKeyUp(KeyCode.Return) == true)
            {
                m_gameTime         = 0;
                m_GameState        = GameState.Playing;
                m_MessageText.text = "";
                m_TimerText.gameObject.SetActive(true);


                for (int i = 0; i < m_Characters.Length; i++)
                {
                    m_Characters[i].SetActive(true);
                }
            }
            break;
        }
    }