TowerDefenseGame/Assets/Scripts/Runtime/GameManagement/EnemySpawnerManager.cs

72 lines
1.8 KiB
C#

using AI.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UIElements;
using static EnemySpawnerManager;
public class EnemySpawnerManager : MonoBehaviour {
public static EnemySpawnerManager Instance;
public static Action UpdateTick;
public static Action GizmoTick;
[SerializeField] public GameObject GoblerPreFab;
private GameObjectPool GoblerPool;
[SerializeField] public bool CanStartSpawning = true;
public class Gobler {
public GameObject Object;
public GoblerStateManager Manager;
public Gobler(GameObject obj, GoblerStateManager manager) {
Object = obj;
Manager = manager;
}
}
public void Awake() {
Instance = this;
EnemySpawnerData.Goblers.Clear();
GoblerPool = new GameObjectPool(GoblerPreFab, 50, 200);
}
public static void RemoveGobler(GameObject gameObject) {
Destroy(gameObject);
EnemySpawnerData.Goblers.Remove(gameObject);
}
public void Update() {
StartSpawning();
UpdateTick?.Invoke();
}
public void StartSpawning() {
if (!CanStartSpawning) return;
while (EnemySpawnerData.Goblers.Count() < EnemySpawnerData.MaxGoblers) {
var gobler = GoblerPool.Get(transform.position, Quaternion.identity);
EnemySpawnerData.Goblers.Add(gobler, new GoblerStateManager(gobler));
}
CanStartSpawning = false;
}
public void OnDrawGizmos() {
GizmoTick?.Invoke();
}
public static void DrawWireSphere(Color color, Vector3 center, float radius) {
Gizmos.color = color;
Gizmos.DrawWireSphere(center, radius);
}
}
public static class EnemySpawnerData {
public static Dictionary<GameObject, GoblerStateManager> Goblers = new Dictionary<GameObject, GoblerStateManager>();
public static int MaxGoblers = 100;
}