Refactor Enemy AI
This commit is contained in:
parent
0ef60e5828
commit
5d08d583e0
@ -2,6 +2,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Unity.IO.LowLevel.Unsafe;
|
using Unity.IO.LowLevel.Unsafe;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using static UnityEditor.PlayerSettings;
|
||||||
|
|
||||||
namespace AI.Base {
|
namespace AI.Base {
|
||||||
public class EnemyStateManager : MonoBehaviour {
|
public class EnemyStateManager : MonoBehaviour {
|
||||||
@ -18,8 +19,13 @@ namespace AI.Base {
|
|||||||
UpdateTick?.Invoke();
|
UpdateTick?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void OnDrawGizmos() {
|
public void OnDrawGizmos() {
|
||||||
GizmoTick?.Invoke();
|
//GizmoTick?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void DrawWireSphere(Color color, Vector3 center, float radius) {
|
||||||
|
Gizmos.color = color;
|
||||||
|
Gizmos.DrawWireSphere(center, radius);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -172,9 +172,7 @@ public class GoblerStateManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void DrawChaseDistance() {
|
private void DrawChaseDistance() {
|
||||||
Gizmos.color = Color.green;
|
EnemyStateManager.DrawWireSphere(Color.green, MyPos, ChaseDistance);
|
||||||
Vector2 center = MyPos;
|
|
||||||
Gizmos.DrawWireSphere(center, ChaseDistance);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawAttackDistance() {
|
private void DrawAttackDistance() {
|
||||||
@ -189,6 +187,7 @@ public class GoblerStateManager {
|
|||||||
|
|
||||||
Vector2 direction2D = (PlayerPos - MyPos).normalized;
|
Vector2 direction2D = (PlayerPos - MyPos).normalized;
|
||||||
Vector2 point2D = MyPos + direction2D * AttackDistance;
|
Vector2 point2D = MyPos + direction2D * AttackDistance;
|
||||||
Gizmos.DrawWireSphere(point2D, AttackMaskDiameter);
|
//Gizmos.DrawWireSphere(point2D, AttackMaskDiameter);
|
||||||
|
EnemyStateManager.DrawWireSphere(Color.red, point2D, AttackMaskDiameter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,12 +10,13 @@ using static EnemySpawnerManager;
|
|||||||
|
|
||||||
public class EnemySpawnerManager : MonoBehaviour {
|
public class EnemySpawnerManager : MonoBehaviour {
|
||||||
public static EnemySpawnerManager Instance;
|
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 UpdateTick;
|
||||||
public static Action GizmoTick;
|
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 class Gobler {
|
||||||
public GameObject Object;
|
public GameObject Object;
|
||||||
@ -29,24 +30,38 @@ public class EnemySpawnerManager : MonoBehaviour {
|
|||||||
|
|
||||||
public void Awake() {
|
public void Awake() {
|
||||||
Instance = this;
|
Instance = this;
|
||||||
Goblers.Clear();
|
EnemySpawnerData.Goblers.Clear();
|
||||||
GoblerPool = new GameObjectPool(GoblerPreFab, 50, 200);
|
GoblerPool = new GameObjectPool(GoblerPreFab, 50, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RemoveGobler(GoblerStateManager goblerManager) {
|
public static void RemoveGobler(GoblerStateManager goblerManager) {
|
||||||
Destroy(Goblers[goblerManager]);
|
Destroy(EnemySpawnerData.Goblers[goblerManager]);
|
||||||
Goblers.Remove(goblerManager);
|
EnemySpawnerData.Goblers.Remove(goblerManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update() {
|
public void Update() {
|
||||||
while (Goblers.Count() < MaxGoblers) {
|
StartSpawning();
|
||||||
var gobler = GoblerPool.Get(transform.position, Quaternion.identity);
|
|
||||||
Goblers.Add(new GoblerStateManager(gobler), gobler);
|
|
||||||
}
|
|
||||||
UpdateTick?.Invoke();
|
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() {
|
public static void OnDrawGizmos() {
|
||||||
GizmoTick?.Invoke();
|
GizmoTick?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class EnemySpawnerData {
|
||||||
|
public static Dictionary<GoblerStateManager, GameObject> Goblers = new Dictionary<GoblerStateManager, GameObject>();
|
||||||
|
public static int MaxGoblers = 100;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user