141 lines
4.4 KiB
C#
141 lines
4.4 KiB
C#
using JetBrains.Annotations;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using static EnemySpawnerData;
|
|
using static UnityEngine.EventSystems.EventTrigger;
|
|
|
|
|
|
public class EnemySpawnerManager : MonoBehaviour {
|
|
public static EnemySpawnerManager Instance;
|
|
public static Action UpdateTick;
|
|
public static Action GizmoTick;
|
|
|
|
[SerializeField] public List<Enemies> EnemyPrefabKey;
|
|
[SerializeField] public List<GameObject> EnemyPrefab;
|
|
[SerializeField] public List<int> EnemySpawnCountForTesting;
|
|
[SerializeField] public bool CanStartSpawning = true;
|
|
|
|
private Dictionary<Enemies, int> PrefabIndex = new Dictionary<Enemies, int>();
|
|
private Dictionary<Enemies, int> EnemySpawnCountForTestingDict = new Dictionary<Enemies, int>();
|
|
private List<Enemies> ListOfEnemies;
|
|
|
|
|
|
public void Awake() {
|
|
Instance = this;
|
|
|
|
for (int i = 0; i < EnemyPrefabKey.Count; i++) {
|
|
PrefabIndex.Add(EnemyPrefabKey[i], i);
|
|
EnemySpawnCountForTestingDict.Add(EnemyPrefabKey[i], EnemySpawnCountForTesting[i]);
|
|
}
|
|
|
|
//ListOfEnemies = Enum.GetValues(typeof(Enemies)).Cast<Enemies>().ToList();
|
|
ListOfEnemies = EnemyPrefabKey;
|
|
Owner = this.GameObject();
|
|
RestartData();
|
|
foreach (var enemy in ListOfEnemies) {
|
|
InitializeEnemyData(enemy, EnemyPrefab[PrefabIndex[enemy]]);
|
|
ApplyInitialTestSpawnIfApplicable(enemy);
|
|
}
|
|
}
|
|
|
|
private void ApplyInitialTestSpawnIfApplicable(Enemies enemy) {
|
|
if (!EnemySpawnCountForTestingDict.ContainsKey(enemy)) return;
|
|
MaxSpawnCount[enemy] = EnemySpawnCountForTestingDict[enemy];
|
|
}
|
|
|
|
public static void DestroyEnemy(GameObject gameObject, Enemies enemy) {
|
|
Destroy(gameObject);
|
|
CurrentSpawnCount[enemy]--;
|
|
}
|
|
|
|
public void Update() {
|
|
StartSpawning();
|
|
UpdateTick?.Invoke();
|
|
}
|
|
|
|
public void StartSpawning() {
|
|
if (!CanStartSpawning) return;
|
|
|
|
foreach (var enemy in ListOfEnemies)
|
|
while (CurrentSpawnCount[enemy] < MaxSpawnCount[enemy]) {
|
|
var enemyObject = EnemyGameObjectPools[enemy].Get(transform.position, Quaternion.identity);
|
|
var enemyScript = new Gobler(enemyObject);
|
|
EnemyMap.Add(enemyObject, enemyScript);
|
|
CurrentSpawnCount[enemy]++;
|
|
}
|
|
|
|
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 class EnemyManager : MonoBehaviour {
|
|
public Action UpdateTick;
|
|
public Action GizmoTick;
|
|
|
|
public void Update() {
|
|
UpdateTick?.Invoke();
|
|
}
|
|
|
|
public void OnDrawGizmos() {
|
|
GizmoTick?.Invoke();
|
|
}
|
|
|
|
public void DrawWireSphere(Color color, Vector3 center, float radius) {
|
|
Gizmos.color = color;
|
|
Gizmos.DrawWireSphere(center, radius);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static class EnemySpawnerData {
|
|
public enum Enemies {
|
|
Gobler,
|
|
}
|
|
|
|
public static GameObject Owner;
|
|
public static Dictionary<Enemies, GameObject> ObjectPrefabs = new Dictionary<Enemies, GameObject>();
|
|
public static Dictionary<Enemies, GameObjectPool> EnemyGameObjectPools = new Dictionary<Enemies, GameObjectPool>();
|
|
public static Dictionary<Enemies, EnemyManager> EnemyManagers = new Dictionary<Enemies, EnemyManager>();
|
|
public static Dictionary<Enemies, int> MaxSpawnCount = new Dictionary<Enemies, int>();
|
|
public static Dictionary<Enemies, int> CurrentSpawnCount = new Dictionary<Enemies, int>();
|
|
public static Dictionary<GameObject, Alive> EnemyMap = new Dictionary<GameObject, Alive>();
|
|
|
|
public static void RestartData() {
|
|
ObjectPrefabs = new Dictionary<Enemies, GameObject>();
|
|
EnemyGameObjectPools = new Dictionary<Enemies, GameObjectPool>();
|
|
EnemyManagers = new Dictionary<Enemies, EnemyManager>();
|
|
MaxSpawnCount = new Dictionary<Enemies, int>();
|
|
CurrentSpawnCount = new Dictionary<Enemies, int>();
|
|
EnemyMap = new Dictionary<GameObject, Alive>();
|
|
}
|
|
|
|
public static void InitializeEnemyData(Enemies enemy, GameObject prefab) {
|
|
EnemyGameObjectPools.Add(enemy, new GameObjectPool(prefab, 50, 200));
|
|
EnemyManagers.Add(enemy, Owner.AddComponent<EnemyManager>());
|
|
MaxSpawnCount.Add(enemy, 0);
|
|
CurrentSpawnCount.Add(enemy, 0);
|
|
}
|
|
|
|
//public static Dictionary<GameObject, Gobler> Goblers = new Dictionary<GameObject, Gobler>();
|
|
//public static int MaxGoblers = 100;
|
|
}
|