Files

164 lines
3.8 KiB
C#
Raw Permalink Normal View History

2026-04-14 17:06:58 +09:00
using NUnit.Framework.Constraints;
2026-04-29 09:22:08 +09:00
using System.Collections;
using System.Collections.Generic;
2026-04-14 17:06:58 +09:00
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject gameoverText;
public Text timeText;
public Text recordText;
private float surviveTime;
2026-04-29 09:22:08 +09:00
public bool isGameover;
2026-04-14 17:06:58 +09:00
public GameObject restartButton;
2026-04-29 09:22:08 +09:00
public static GameManager instance;
2026-04-14 17:06:58 +09:00
2026-04-29 09:22:08 +09:00
public AudioSource leadAudio;
public AudioSource deathAudio;
2026-04-14 17:06:58 +09:00
2026-04-29 09:22:08 +09:00
public List<BulletSpawner> activeSpawners = new List<BulletSpawner>();
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);
2026-04-14 17:06:58 +09:00
2026-04-29 09:22:08 +09:00
public int maxSpawnerCount = 2;
public Transform rotationPlate;
void Awake()
{
if (instance == null) instance = this;
}
2026-04-14 17:06:58 +09:00
void Start()
{
surviveTime = 0;
isGameover = false;
2026-04-29 09:22:08 +09:00
if (leadAudio != null) leadAudio.Play();
if (deathAudio != null) deathAudio.Stop();
2026-04-14 17:06:58 +09:00
}
void Update()
{
if(!isGameover)
{
surviveTime += Time.deltaTime;
timeText.text = "Time: " + (int)surviveTime;
}
else
{
if (Input.GetKeyDown(KeyCode.R))
{
RestartGame();
}
if (Input.GetMouseButtonDown(0))
2026-04-14 17:06:58 +09:00
{
RestartGame();
2026-04-14 17:06:58 +09:00
}
}
}
public float GetSurviveTime()
{
return surviveTime;
}
public void RestartGame()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
}
2026-04-29 09:22:08 +09:00
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();
}
}
}
2026-04-14 17:06:58 +09:00
}