Refactor Enemy AI

This commit is contained in:
Nico 2025-07-10 00:07:05 -07:00
parent 0ef60e5828
commit 5d08d583e0
3 changed files with 37 additions and 17 deletions

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Unity.IO.LowLevel.Unsafe;
using UnityEngine;
using static UnityEditor.PlayerSettings;
namespace AI.Base {
public class EnemyStateManager : MonoBehaviour {
@ -18,8 +19,13 @@ namespace AI.Base {
UpdateTick?.Invoke();
}
public static void OnDrawGizmos() {
GizmoTick?.Invoke();
public void OnDrawGizmos() {
//GizmoTick?.Invoke();
}
public static void DrawWireSphere(Color color, Vector3 center, float radius) {
Gizmos.color = color;
Gizmos.DrawWireSphere(center, radius);
}
}
}

View File

@ -172,9 +172,7 @@ public class GoblerStateManager {
}
private void DrawChaseDistance() {
Gizmos.color = Color.green;
Vector2 center = MyPos;
Gizmos.DrawWireSphere(center, ChaseDistance);
EnemyStateManager.DrawWireSphere(Color.green, MyPos, ChaseDistance);
}
private void DrawAttackDistance() {
@ -189,6 +187,7 @@ public class GoblerStateManager {
Vector2 direction2D = (PlayerPos - MyPos).normalized;
Vector2 point2D = MyPos + direction2D * AttackDistance;
Gizmos.DrawWireSphere(point2D, AttackMaskDiameter);
//Gizmos.DrawWireSphere(point2D, AttackMaskDiameter);
EnemyStateManager.DrawWireSphere(Color.red, point2D, AttackMaskDiameter);
}
}

View File

@ -10,12 +10,13 @@ using static EnemySpawnerManager;
public class EnemySpawnerManager : MonoBehaviour {
public static EnemySpawnerManager Instance;
[SerializeField] public GameObject GoblerPreFab;
private GameObjectPool GoblerPool;
public static Dictionary<GoblerStateManager, GameObject> Goblers = new Dictionary<GoblerStateManager, GameObject>();
public static Action UpdateTick;
public static Action GizmoTick;
public static int MaxGoblers = 100;
[SerializeField] public GameObject GoblerPreFab;
private GameObjectPool GoblerPool;
[SerializeField] public bool CanStartSpawning = true;
public class Gobler {
public GameObject Object;
@ -29,24 +30,38 @@ public class EnemySpawnerManager : MonoBehaviour {
public void Awake() {
Instance = this;
Goblers.Clear();
EnemySpawnerData.Goblers.Clear();
GoblerPool = new GameObjectPool(GoblerPreFab, 50, 200);
}
public static void RemoveGobler(GoblerStateManager goblerManager) {
Destroy(Goblers[goblerManager]);
Goblers.Remove(goblerManager);
Destroy(EnemySpawnerData.Goblers[goblerManager]);
EnemySpawnerData.Goblers.Remove(goblerManager);
}
public void Update() {
while (Goblers.Count() < MaxGoblers) {
var gobler = GoblerPool.Get(transform.position, Quaternion.identity);
Goblers.Add(new GoblerStateManager(gobler), gobler);
}
StartSpawning();
UpdateTick?.Invoke();
GizmoTick?.Invoke();
}
public void StartSpawning() {
if (!CanStartSpawning) return;
while (EnemySpawnerData.Goblers.Count() < EnemySpawnerData.MaxGoblers) {
var gobler = GoblerPool.Get(transform.position, Quaternion.identity);
EnemySpawnerData.Goblers.Add(new GoblerStateManager(gobler), gobler);
}
CanStartSpawning = false;
}
public static void OnDrawGizmos() {
GizmoTick?.Invoke();
}
}
public static class EnemySpawnerData {
public static Dictionary<GoblerStateManager, GameObject> Goblers = new Dictionary<GoblerStateManager, GameObject>();
public static int MaxGoblers = 100;
}