Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. CoinCollector
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CoinCollector : MonoBehaviour
- {
- public int collectedCounter = 0;
- private void OnTriggerEnter(Collider other)
- {
- if (other.CompareTag("Collectable"))
- {
- Collect(other.gameObject);
- }
- }
- private void Collect(GameObject coin)
- {
- coin.SetActive(false);
- collectedCounter += 1;
- }
- public void ResetCounter()
- {
- collectedCounter = 0;
- }
- }
- // 2. Spinner
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Spinner : MonoBehaviour
- {
- public Transform target;
- public Vector3 speed = new Vector3(0f, 0f, 90f);
- void Update()
- {
- Vector3 rotationVector = speed * Time.deltaTime;
- target.Rotate(rotationVector);
- }
- }
- // 3. CoinGameManager
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using TMPro;
- using UnityEngine;
- public class CoinGameManager : MonoBehaviour
- {
- [Header("Game settings")]
- public float timeLimit = 20f;
- public int coinsToCollect = 0;
- [Header("References")]
- public CoinCollector collector;
- public FPSPlayerController player;
- public TextMeshProUGUI scoreText;
- public TextMeshProUGUI completionText;
- public TextMeshProUGUI timerText;
- public GameObject menu;
- private bool isGameFinished = false;
- private bool isGameWon = false;
- private float timeLeft;
- private List<GameObject> coins = new List<GameObject>();
- private void Start()
- {
- InitializeGame();
- }
- void Update()
- {
- UpdateTimer();
- UpdateGameState();
- UpdateScore();
- UpdateCompletion();
- }
- public void InitializeGame()
- {
- // reset game state
- isGameFinished = false;
- player.enabled = true;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- // respawn coins
- foreach (var coin in coins)
- {
- coin.SetActive(true);
- }
- // reset coin count
- coins = GameObject.FindGameObjectsWithTag("Collectable").ToList();
- coinsToCollect = coins.Count;
- timeLimit = coinsToCollect * 3f;
- collector.ResetCounter();
- // start timer
- timeLeft = timeLimit;
- }
- void UpdateTimer()
- {
- if (isGameFinished) return;
- timeLeft -= Time.deltaTime;
- if (timeLeft <= 0)
- {
- timeLeft = 0f;
- }
- timerText.text = $"{timeLeft:F2}";
- }
- void UpdateGameState()
- {
- bool allCoinsCollected = collector.collectedCounter >= coinsToCollect;
- bool timeRanOut = timeLeft <= 0;
- isGameFinished = allCoinsCollected || timeRanOut;
- isGameWon = allCoinsCollected;
- if (isGameFinished)
- {
- player.enabled = false;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- }
- void UpdateScore()
- {
- scoreText.text = $"{collector.collectedCounter} / {coinsToCollect}";
- }
- void UpdateCompletion()
- {
- if (!isGameFinished)
- {
- menu.SetActive(false);
- completionText.gameObject.SetActive(false);
- return;
- }
- // game finished
- menu.SetActive(true);
- completionText.gameObject.SetActive(true);
- if (isGameWon)
- {
- completionText.text = "Congratulations! All coins collected!";
- }
- else
- {
- completionText.text = "Game over. Start again?";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement