Advertisement
nstruth2

GameScecneManager Code

Feb 18th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class GameSceneManager : MonoBehaviour
  8. {
  9.  
  10.     float ballIntervalMax = 2.0f;
  11.     float ballIntervalMin = 0.2f;
  12.     float timeToMinimumInterval = 30.0f;
  13.     public float gameTime;
  14.  
  15.     public Camera mainCamera;
  16.     public Text  scoreText;
  17.     public Text gameOverText;
  18.     public PlayerController player;
  19.     public GameObject ballPrefab;
  20.  
  21.     Vector3 leftBound;
  22.     int score = 0;
  23.     float gameTimer;
  24.     float ballTimer;
  25.     bool gameOver = false;
  26.  
  27.     void Start()
  28.     {
  29.         Time.timeScale = 1;
  30.         ballTimer = ballIntervalMax;
  31.  
  32.         leftBound = mainCamera.ViewportToWorldPoint(new Vector3(0,1, -mainCamera.transform.localPosition.z));
  33.  
  34.         player.OnCollectBall += OnCollectBall;
  35.  
  36.     }
  37.  
  38.  
  39.     void Update()
  40.     {
  41.         gameOverLogic();
  42.         printScoreText();
  43.         setUpTimers();
  44.     }
  45.  
  46.     void gameOverLogic()
  47.     {
  48.         if(gameOver)
  49.         {
  50.             if(Input.GetKeyDown("r"))
  51.             {
  52.                 SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  53.             }
  54.  
  55.             scoreText.enabled = false;
  56.             gameOverText.enabled = true;
  57.  
  58.  
  59.             gameOverText.text = "Game Over Total Score: " + score + "\nPress R to Restart!";
  60.  
  61.             return;
  62.         }
  63.     }
  64.  
  65.     void printScoreText()
  66.     {
  67.         scoreText.text = "Score: " + score + "\nTime Left: " + Mathf.Floor(Mathf.Max((gameTime - gameTimer), 0));
  68.     }
  69.     void setUpTimers()
  70.     {
  71.         gameTimer += Time.deltaTime;
  72.         ballTimer -= Time.deltaTime;
  73.         if (ballTimer <= 0)
  74.         {
  75.             float intervalPercentage = Mathf.Min(gameTimer / timeToMinimumInterval, 1);
  76.             ballTimer = ballIntervalMax - (ballIntervalMax - ballIntervalMin) * intervalPercentage;
  77.  
  78.             GameObject ball = GameObject.Instantiate<GameObject>(ballPrefab);
  79.             ball.transform.SetParent(this.transform);
  80.             ball.transform.position = new Vector3(Random.Range(-5,5), leftBound.y + 2, 0);
  81.         }
  82.  
  83.     if(gameTimer > gameTime)
  84.     {
  85.         onGameOver();
  86.     }
  87.     }
  88.  
  89.     public void OnCollectBall()
  90.     {
  91.         if (!gameOver)
  92.         {
  93.             score = score + 100;
  94.         }
  95.     }
  96.  
  97.     void onGameOver()
  98.     {
  99.         gameOver = true;
  100.         Time.timeScale = 0;
  101.     }
  102.  
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement