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.UI;
- using UnityEngine.SceneManagement;
- public class GameSceneManager : MonoBehaviour
- {
- float ballIntervalMax = 2.0f;
- float ballIntervalMin = 0.2f;
- float timeToMinimumInterval = 30.0f;
- public float gameTime;
- public Camera mainCamera;
- public Text scoreText;
- public Text gameOverText;
- public PlayerController player;
- public GameObject ballPrefab;
- Vector3 leftBound;
- int score = 0;
- float gameTimer;
- float ballTimer;
- bool gameOver = false;
- void Start()
- {
- Time.timeScale = 1;
- ballTimer = ballIntervalMax;
- leftBound = mainCamera.ViewportToWorldPoint(new Vector3(0,1, -mainCamera.transform.localPosition.z));
- player.OnCollectBall += OnCollectBall;
- }
- void Update()
- {
- gameOverLogic();
- printScoreText();
- setUpTimers();
- }
- void gameOverLogic()
- {
- if(gameOver)
- {
- if(Input.GetKeyDown("r"))
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);
- }
- scoreText.enabled = false;
- gameOverText.enabled = true;
- gameOverText.text = "Game Over Total Score: " + score + "\nPress R to Restart!";
- return;
- }
- }
- void printScoreText()
- {
- scoreText.text = "Score: " + score + "\nTime Left: " + Mathf.Floor(Mathf.Max((gameTime - gameTimer), 0));
- }
- void setUpTimers()
- {
- gameTimer += Time.deltaTime;
- ballTimer -= Time.deltaTime;
- if (ballTimer <= 0)
- {
- float intervalPercentage = Mathf.Min(gameTimer / timeToMinimumInterval, 1);
- ballTimer = ballIntervalMax - (ballIntervalMax - ballIntervalMin) * intervalPercentage;
- GameObject ball = GameObject.Instantiate<GameObject>(ballPrefab);
- ball.transform.SetParent(this.transform);
- ball.transform.position = new Vector3(Random.Range(-5,5), leftBound.y + 2, 0);
- }
- if(gameTimer > gameTime)
- {
- onGameOver();
- }
- }
- public void OnCollectBall()
- {
- if (!gameOver)
- {
- score = score + 100;
- }
- }
- void onGameOver()
- {
- gameOver = true;
- Time.timeScale = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement