Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class GameManager : MonoBehaviour
- {
- public goalScript blue, green, red, orange;
- private bool isGameOver = false;
- private GUIStyle gameOverStyle;
- private GUIStyle buttonStyle;
- private float elapsedTime = 0f;
- void Start()
- {
- blue.OnSolve += CheckGameOver;
- green.OnSolve += CheckGameOver;
- red.OnSolve += CheckGameOver;
- orange.OnSolve += CheckGameOver;
- }
- void Update()
- {
- if (!isGameOver)
- {
- elapsedTime += Time.deltaTime;
- }
- }
- GUIStyle GetGameOverStyle()
- {
- if (gameOverStyle == null)
- {
- gameOverStyle = new GUIStyle(GUI.skin.box)
- {
- fontSize = 24,
- alignment = TextAnchor.MiddleCenter
- };
- }
- return gameOverStyle;
- }
- GUIStyle GetButtonStyle()
- {
- if (buttonStyle == null)
- {
- buttonStyle = new GUIStyle(GUI.skin.button)
- {
- fontSize = 20,
- normal = { textColor = Color.white, background = MakeTex(2, 2, new Color(0.5f, 0.5f, 0.5f, 1.0f)) },
- hover = { background = MakeTex(2, 2, new Color(0.7f, 0.7f, 0.7f, 1.0f)) },
- active = { background = MakeTex(2, 2, new Color(0.3f, 0.3f, 0.3f, 1.0f)) }
- };
- }
- return buttonStyle;
- }
- void CheckGameOver()
- {
- isGameOver = blue.isSolved && green.isSolved && red.isSolved && orange.isSolved;
- }
- void OnGUI()
- {
- if (isGameOver)
- {
- Rect rect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100);
- GUI.Box(rect, "Game Over", GetGameOverStyle());
- Rect rect2 = new Rect(Screen.width / 2 - 50, Screen.height / 2 + 55, 100, 50);
- if (GUI.Button(rect2, "Restart", GetButtonStyle()))
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);
- }
- Rect rect3 = new Rect(Screen.width / 2 - 100, Screen.height / 2 + 110, 200, 50);
- GUI.Box(rect3, "Time: " + elapsedTime.ToString("F2") + " seconds", GetGameOverStyle());
- }
- else
- {
- Rect rect4 = new Rect(10, 10, 300, 50);
- GUI.Box(rect4, "Time: " + elapsedTime.ToString("F2") + " seconds", GetGameOverStyle());
- }
- }
- void OnDestroy()
- {
- blue.OnSolve -= CheckGameOver;
- green.OnSolve -= CheckGameOver;
- red.OnSolve -= CheckGameOver;
- orange.OnSolve -= CheckGameOver;
- }
- Texture2D MakeTex(int width, int height, Color col)
- {
- Color[] pix = new Color[width * height];
- for (int i = 0; i < pix.Length; ++i)
- {
- pix[i] = col;
- }
- Texture2D result = new Texture2D(width, height);
- result.SetPixels(pix);
- result.Apply();
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement