From 5d08d583e07b49676103d2a3a345883bc1bbd447 Mon Sep 17 00:00:00 2001 From: Nico Date: Thu, 10 Jul 2025 00:07:05 -0700 Subject: [PATCH] Refactor Enemy AI --- .../AI/EnemyManager/EnemyStateManager.cs | 10 ++++- .../AI/EnemyManager/GoblerStateManager.cs | 7 ++-- .../GameManagement/EnemySpawnerManager.cs | 37 +++++++++++++------ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/Assets/Scripts/Runtime/AI/EnemyManager/EnemyStateManager.cs b/Assets/Scripts/Runtime/AI/EnemyManager/EnemyStateManager.cs index c13945e..2e7d03a 100644 --- a/Assets/Scripts/Runtime/AI/EnemyManager/EnemyStateManager.cs +++ b/Assets/Scripts/Runtime/AI/EnemyManager/EnemyStateManager.cs @@ -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); } } } diff --git a/Assets/Scripts/Runtime/AI/EnemyManager/GoblerStateManager.cs b/Assets/Scripts/Runtime/AI/EnemyManager/GoblerStateManager.cs index 74a2970..9e2a6e6 100644 --- a/Assets/Scripts/Runtime/AI/EnemyManager/GoblerStateManager.cs +++ b/Assets/Scripts/Runtime/AI/EnemyManager/GoblerStateManager.cs @@ -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); } } diff --git a/Assets/Scripts/Runtime/GameManagement/EnemySpawnerManager.cs b/Assets/Scripts/Runtime/GameManagement/EnemySpawnerManager.cs index 06dedc1..81d46eb 100644 --- a/Assets/Scripts/Runtime/GameManagement/EnemySpawnerManager.cs +++ b/Assets/Scripts/Runtime/GameManagement/EnemySpawnerManager.cs @@ -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 Goblers = new Dictionary(); 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 Goblers = new Dictionary(); + public static int MaxGoblers = 100; +}