Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Level : MonoBehaviour {
- float startTime;
- int numTreasures;
- int collected = 0;
- bool levelComplete = false;
- GameObject player;
- public float levelBottomY = -10f;
- GameObject kamera;
- // Use this for initialization
- void Start () {
- startTime = Time.time;
- GameObject[] treasureObjects = GameObject.FindGameObjectsWithTag("Treasure");
- numTreasures = treasureObjects.Length;
- player = GameObject.FindWithTag("Player");
- Cursor.visible = false;
- kamera = GameObject.FindWithTag("MainCamera");
- }
- // Update is called once per frame
- void Update () {
- if (player!= null && player.transform.position.y < levelBottomY)
- {
- Kill();
- }
- }
- void Kill ()
- {
- kamera.transform.parent = null;
- GameObject.Destroy(player);
- levelComplete = true;
- }
- void TreasureCollected()
- {
- collected++;
- if (collected == numTreasures)
- {
- levelComplete = true;
- // GameObject hiscores = GameObject.Find("Hiscores");
- // if (hiscores != null)
- // {
- // hiscores.GetComponent<Hiscores>().StoreTime(SceneManager.GetActiveScene().buildIndex, Time.time - startTime);
- // }
- }
- }
- bool LevelEndMessage(string message, string buttonText)
- {
- Cursor.visible = true;
- GUI.skin.label.alignment = TextAnchor.MiddleCenter;
- GUI.Label(new Rect(Screen.width / 2 - 200 / 2, Screen.height / 2 -75, 200, 50), message);
- return GUI.Button(new Rect(Screen.width / 2 - 200 / 2, Screen.height / 2 + 25, 200, 50), buttonText);
- }
- private void OnGUI()
- {
- GUI.skin.label.fontSize = 20;
- GUI.skin.label.alignment = TextAnchor.MiddleLeft;
- GUI.skin.label.alignment = TextAnchor.MiddleLeft;
- GUI.Label(new Rect(10, 10, 200, 30), "Time: " + (Time.time - startTime).ToString("#.##"));
- GUI.Label(new Rect(10, 40, 200, 30), "Treasures: " + collected + "/" + numTreasures);
- if (levelComplete)
- {
- if (player != null)
- {
- if (LevelEndMessage("Level completed!", "Continue"))
- {
- int nextLevel = SceneManager.GetActiveScene().buildIndex + 1;
- if (nextLevel >= SceneManager.sceneCountInBuildSettings)
- { nextLevel = 0; }
- SceneManager.LoadScene(nextLevel);
- }
- }
- else if (player == null)
- {
- if (LevelEndMessage("Level failed", "Retry"))
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- }
- if (GUI.Button(new Rect(Screen.width / 2 - 200 / 2, Screen.height / 2 + 100, 200, 50)
- , "Give up"))
- {
- SceneManager.LoadScene(0);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement