Advertisement
JGroxz

XR course week 5

Mar 7th, 2025 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. // 1. CoinCollector
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7.  
  8. public class CoinCollector : MonoBehaviour
  9. {
  10.     public int collectedCounter = 0;
  11.    
  12.     private void OnTriggerEnter(Collider other)
  13.     {
  14.         if (other.CompareTag("Collectable"))
  15.         {
  16.             Collect(other.gameObject);
  17.         }
  18.     }
  19.  
  20.     private void Collect(GameObject coin)
  21.     {
  22.         coin.SetActive(false);
  23.         collectedCounter += 1;
  24.     }
  25.  
  26.     public void ResetCounter()
  27.     {
  28.         collectedCounter = 0;
  29.     }
  30. }
  31.  
  32.  
  33.  
  34.  
  35. // 2. Spinner
  36.  
  37. using System.Collections;
  38. using System.Collections.Generic;
  39. using UnityEngine;
  40.  
  41. public class Spinner : MonoBehaviour
  42. {
  43.     public Transform target;
  44.     public Vector3 speed = new Vector3(0f, 0f, 90f);
  45.    
  46.     void Update()
  47.     {
  48.         Vector3 rotationVector = speed * Time.deltaTime;
  49.         target.Rotate(rotationVector);
  50.     }
  51. }
  52.  
  53.  
  54.  
  55. // 3. CoinGameManager
  56.  
  57. using System;
  58. using System.Collections;
  59. using System.Collections.Generic;
  60. using System.Linq;
  61. using TMPro;
  62. using UnityEngine;
  63.  
  64. public class CoinGameManager : MonoBehaviour
  65. {
  66.     [Header("Game settings")]
  67.     public float timeLimit = 20f;
  68.     public int coinsToCollect = 0;
  69.    
  70.     [Header("References")]
  71.     public CoinCollector collector;
  72.     public FPSPlayerController player;
  73.     public TextMeshProUGUI scoreText;
  74.     public TextMeshProUGUI completionText;
  75.     public TextMeshProUGUI timerText;
  76.     public GameObject menu;
  77.    
  78.     private bool isGameFinished = false;
  79.     private bool isGameWon = false;
  80.     private float timeLeft;
  81.  
  82.     private List<GameObject> coins = new List<GameObject>();
  83.  
  84.     private void Start()
  85.     {
  86.         InitializeGame();
  87.     }
  88.  
  89.     void Update()
  90.     {
  91.         UpdateTimer();
  92.         UpdateGameState();
  93.  
  94.         UpdateScore();
  95.         UpdateCompletion();
  96.     }
  97.  
  98.     public void InitializeGame()
  99.     {
  100.         // reset game state
  101.         isGameFinished = false;
  102.         player.enabled = true;
  103.         Cursor.lockState = CursorLockMode.Locked;
  104.         Cursor.visible = false;
  105.  
  106.         // respawn coins
  107.         foreach (var coin in coins)
  108.         {
  109.             coin.SetActive(true);
  110.         }
  111.        
  112.         // reset coin count
  113.         coins = GameObject.FindGameObjectsWithTag("Collectable").ToList();
  114.         coinsToCollect = coins.Count;
  115.         timeLimit = coinsToCollect * 3f;
  116.         collector.ResetCounter();
  117.  
  118.         // start timer
  119.         timeLeft = timeLimit;
  120.     }
  121.  
  122.     void UpdateTimer()
  123.     {
  124.         if (isGameFinished) return;
  125.  
  126.         timeLeft -= Time.deltaTime;
  127.         if (timeLeft <= 0)
  128.         {
  129.             timeLeft = 0f;
  130.         }
  131.  
  132.         timerText.text = $"{timeLeft:F2}";
  133.     }
  134.  
  135.     void UpdateGameState()
  136.     {
  137.         bool allCoinsCollected = collector.collectedCounter >= coinsToCollect;
  138.         bool timeRanOut = timeLeft <= 0;
  139.  
  140.         isGameFinished = allCoinsCollected || timeRanOut;
  141.         isGameWon = allCoinsCollected;
  142.  
  143.         if (isGameFinished)
  144.         {
  145.             player.enabled = false;
  146.             Cursor.lockState = CursorLockMode.None;
  147.             Cursor.visible = true;
  148.         }
  149.     }
  150.  
  151.     void UpdateScore()
  152.     {
  153.         scoreText.text = $"{collector.collectedCounter} / {coinsToCollect}";
  154.     }
  155.  
  156.     void UpdateCompletion()
  157.     {
  158.         if (!isGameFinished)
  159.         {
  160.             menu.SetActive(false);
  161.             completionText.gameObject.SetActive(false);
  162.             return;
  163.         }
  164.  
  165.         // game finished
  166.         menu.SetActive(true);
  167.         completionText.gameObject.SetActive(true);
  168.         if (isGameWon)
  169.         {
  170.             completionText.text = "Congratulations! All coins collected!";
  171.         }
  172.         else
  173.         {
  174.             completionText.text = "Game over. Start again?";
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement