MonsterGroupManager.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class MonsterGroupManager : MonoBehaviour
  5. {
  6. public MonsterData monsterType;
  7. public int numberOfMonsters = 5;
  8. public float respawnTime = 300f; // 5 min
  9. public Vector3 spawnCenter;
  10. public float spacing = 1.5f;
  11. private List<GameObject> spawnedMonsters = new();
  12. private bool isRespawning = false;
  13. void Start()
  14. {
  15. SpawnMonsters();
  16. }
  17. public void SpawnMonsters()
  18. {
  19. for (int i = 0; i < numberOfMonsters; i++)
  20. {
  21. Vector3 spawnPos = spawnCenter + new Vector3((i % 3) * spacing, 0, -(i / 3) * spacing);
  22. GameObject m = Instantiate(monsterType.prefab, spawnPos, Quaternion.identity);
  23. MonsterController ctrl = m.GetComponent<MonsterController>();
  24. ctrl.data = monsterType;
  25. ctrl.Setup(this);
  26. spawnedMonsters.Add(m);
  27. }
  28. }
  29. public void OnMonsterDefeated(GameObject monster)
  30. {
  31. spawnedMonsters.Remove(monster);
  32. Destroy(monster);
  33. if (!isRespawning && spawnedMonsters.Count == 0)
  34. StartCoroutine(RespawnDelay());
  35. }
  36. IEnumerator RespawnDelay()
  37. {
  38. isRespawning = true;
  39. yield return new WaitForSeconds(respawnTime);
  40. SpawnMonsters();
  41. isRespawning = false;
  42. }
  43. }