using NUnit.Framework.Constraints; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class GameManager : MonoBehaviour { public GameObject gameoverText; public Text timeText; public Text recordText; private float surviveTime; public bool isGameover; public GameObject restartButton; public static GameManager instance; public AudioSource leadAudio; public AudioSource deathAudio; public List activeSpawners = new List(); public Transform player; public SpawnZone spawnZone; public GameObject spawnerPrefab; public void RegisterSpawner(BulletSpawner s) => activeSpawners.Add(s); public void UnregisterSpawner(BulletSpawner s) => activeSpawners.Remove(s); public int maxSpawnerCount = 2; public Transform rotationPlate; void Awake() { if (instance == null) instance = this; } void Start() { surviveTime = 0; isGameover = false; if (leadAudio != null) leadAudio.Play(); if (deathAudio != null) deathAudio.Stop(); } void Update() { if(!isGameover) { surviveTime += Time.deltaTime; timeText.text = "Time: " + (int)surviveTime; } else { if (Input.GetKeyDown(KeyCode.R)) { RestartGame(); } if (Input.GetMouseButtonDown(0)) { RestartGame(); } } } public float GetSurviveTime() { return surviveTime; } public void RestartGame() { UnityEngine.SceneManagement.SceneManager.LoadScene(1); } public void PlayDeathMusic() { if (leadAudio != null) leadAudio.Stop(); if (deathAudio != null) deathAudio.Play(); } public void EndGame() { isGameover = true; gameoverText.SetActive(true); restartButton.SetActive(true); float bestTime = PlayerPrefs.GetFloat("BestTime"); if (surviveTime > bestTime) { bestTime = surviveTime; PlayerPrefs.SetFloat("BestTime", bestTime); } recordText.text = "Best Time: " + (int)bestTime; } public Vector3 GetValidSpawnPosition() { int maxAttempts = 10; for (int i = 0; i < maxAttempts; i++) { Vector3 candidate = spawnZone.GetRandomPosition(); if (Vector3.Distance(candidate, player.position) < 5f) continue; bool isOverlap = false; foreach (var spawner in activeSpawners) { if (Vector3.Distance(candidate, spawner.transform.position) < 3f) { isOverlap = true; break; } } if (!isOverlap) return candidate; } return spawnZone.GetRandomPosition(); } public void RequestRespawn() { StartCoroutine(RespawnRoutine()); } IEnumerator RespawnRoutine() { yield return new WaitForSeconds(3f); Vector3 validPos = GetValidSpawnPosition(); GameObject newSpawner = Instantiate(spawnerPrefab, validPos, Quaternion.identity); if (rotationPlate != null) { newSpawner.transform.SetParent(rotationPlate); } } public void UpdateMaxSpawnerCount(int newCount) { maxSpawnerCount = newCount; int currentCount = activeSpawners.Count; if (currentCount < maxSpawnerCount) { int needed = maxSpawnerCount - currentCount; for (int i = 0; i < needed; i++) { RequestRespawn(); } } } }