You've already forked zumbi-game
48 lines
1009 B
C#
48 lines
1009 B
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ZombieSpawnerLogic : MonoBehaviour
|
|
{
|
|
public GameObject Prefab;
|
|
public byte CooldownSeconds = 5;
|
|
public byte MaxSpawns = 5;
|
|
|
|
private float _lastSpawn;
|
|
private readonly List<GameObject> _spawns = new();
|
|
|
|
void Start()
|
|
{
|
|
_lastSpawn = Time.time;
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
RemoveDestroyedZombies();
|
|
AttemptZombieSpawn();
|
|
}
|
|
|
|
private void AttemptZombieSpawn()
|
|
{
|
|
if (Time.time - _lastSpawn < CooldownSeconds)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_spawns.Count >= MaxSpawns)
|
|
{
|
|
Debug.Log("Cannot spawn zombie, capacity reached");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("Spawning zombie");
|
|
var newZombie = Instantiate(Prefab, transform);
|
|
_spawns.Add(newZombie);
|
|
_lastSpawn = Time.time;
|
|
}
|
|
|
|
private void RemoveDestroyedZombies()
|
|
{
|
|
_spawns.RemoveAll(zombie => zombie == null);
|
|
}
|
|
}
|