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");
}}
unity学习:三消基础逻辑及其代码4 -用Coroutine来实现游戏循环
最新推荐文章于 2024-09-18 07:45:00 发布