| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class MonsterGroupManager : MonoBehaviour
- {
- public MonsterData monsterType;
- public int numberOfMonsters = 5;
- public float respawnTime = 300f; // 5 min
- public Vector3 spawnCenter;
- public float spacing = 1.5f;
- private List<GameObject> spawnedMonsters = new();
- private bool isRespawning = false;
- void Start()
- {
- SpawnMonsters();
- }
- public void SpawnMonsters()
- {
- for (int i = 0; i < numberOfMonsters; i++)
- {
- Vector3 spawnPos = spawnCenter + new Vector3((i % 3) * spacing, 0, -(i / 3) * spacing);
- GameObject m = Instantiate(monsterType.prefab, spawnPos, Quaternion.identity);
- MonsterController ctrl = m.GetComponent<MonsterController>();
- ctrl.data = monsterType;
- ctrl.Setup(this);
- spawnedMonsters.Add(m);
- }
- }
- public void OnMonsterDefeated(GameObject monster)
- {
- spawnedMonsters.Remove(monster);
- Destroy(monster);
- if (!isRespawning && spawnedMonsters.Count == 0)
- StartCoroutine(RespawnDelay());
- }
- IEnumerator RespawnDelay()
- {
- isRespawning = true;
- yield return new WaitForSeconds(respawnTime);
- SpawnMonsters();
- isRespawning = false;
- }
- }
|