unity学习:三消基础逻辑及其代码4 -用Coroutine来实现游戏循环

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : Singleton<GameManager>
{
    //最大步数。
    public int movesLeft = 30;
    public int scoreGoal = 10000;
    public ScreenFader screenFader;
    public Text levelNameText;
    public Text movesLeftTtext;   
    public Board m_board;   
    bool m_isReadyToBegin = false;
    bool m_isGameOver = false;
    bool m_isWinner = false;
    bool m_isReadyToReload = false;    
    public MessageWindow messageWindow;
    public Sprite loseIcon;
    public Sprite winIcon;
    public Sprite goalIcon;
    // Start is called before the first frame update
    void Start()
    {
        m_board = GameObject.FindObjectOfType<Board>().GetComponent<Board>();
        Scene scene = SceneManager.GetActiveScene();
        if (levelNameText != null)
        {
            levelNameText.text = scene.name;
        }
        UpdateMove();
        StartCoroutine(ExecuteGameLoop());
    }
    public void UpdateMove()
    {
        if (movesLeftTtext != null)
        {
            movesLeftTtext.text = movesLeft.ToString();
        }
    }
    IEnumerator ExecuteGameLoop()
    {
        yield return StartCoroutine("StartGameRoutine");
        yield return StartCoroutine("PlayGameRoutine");
        yield return StartCoroutine("EndGameRoutine");
    }
    public void BeginGame()
    {
        m_isReadyToBegin = true;
    }
    public void ReloadScene()
    {
        m_isReadyToReload = true;
    }    IEnumerator StartGameRoutine()
    {        if (messageWindow != null)
        {
            messageWindow.GetComponent<RectXformMover>().MoveOn();
            messageWindow.ShowMessage(goalIcon, "score goal\n"+scoreGoal.ToString(),"start");
        }
        while (!m_isReadyToBegin)
        {
            yield return null;        }
        if (screenFader != null)
        {
            screenFader.FadeOff();
        }
        yield return new WaitForSeconds(0.5f);
        if (m_board != null)
        {
            m_board.SetupBoard();
        }
    }    IEnumerator PlayGameRoutine()
    {
        while (!m_isGameOver)
        {
            if (ScoreManager.Instance != null)
            {
                if (ScoreManager.Instance.CurrentScore >= scoreGoal)
                {
                    m_isGameOver = true;
                    m_isWinner = true;
                }
            }
            if (movesLeft == 0)
            {
                m_isWinner = false;
                m_isGameOver = true;
            }
            yield return null;
        }
    }
    IEnumerator EndGameRoutine()
    {
        m_isReadyToReload = false;
        if (m_isWinner)
        {
            if (messageWindow != null)
            {
                messageWindow.GetComponent<RectXformMover>().MoveOn();
                messageWindow.ShowMessage(winIcon, "YOU WIN!", "OK");
                if (SoundManager.Instance != null)
                {
                    SoundManager.Instance.PlayWinSound();
                }
            }
        }
        else
        {
            if (messageWindow != null)
            {
                messageWindow.GetComponent<RectXformMover>().MoveOn();
                messageWindow.ShowMessage(loseIcon, "YOU LOSE!", "OK");
                if (SoundManager.Instance != null)
                {
                    SoundManager.Instance.PlayLoseSound();
                }
            }
        }
        yield return new WaitForSeconds(1f);
        if (screenFader != null)
        {
            screenFader.FadeOn();
        }        while (!m_isReadyToReload)
        {
            yield return null;        }
        SceneManager.LoadScene("level1");
    }}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值