Files
WildRoot/Assets/Script/GameManager.cs
T
jongjae0305 8298b2559c [Update] 게임형식 및 오류수정
1. 게임형태 변경
  - Game스텝에서 기존 1단계, 2단계 수정(회전만 가능하게).
  - ShootButton을 만들어 플레이어도 총알을 쏠수 있도록 수정.
  - Bullet, BulletSpawner, PlayerController를 수정
  - SpawnZone을 생성하여 기존 고정이던 스포너를 랜덤으로 생성.

2. 오류 수정
 - 모바일 버전 시 플레이어가 한방향만 보던 형상 수정.
 - 펜스에서 플레이어 캐릭터가 닿으면 회전하던 형상 수정.
2026-04-29 09:22:08 +09:00

164 lines
3.8 KiB
C#

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<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);
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();
}
}
}
}