feat: Register and unregister BatchLOSSystem in EnemyBase lifecycle methods
This commit is contained in:
@@ -2,6 +2,7 @@ using System.Collections.Generic;
|
|||||||
using Unity.Collections;
|
using Unity.Collections;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Jobs;
|
using UnityEngine.Jobs;
|
||||||
|
using BaseGames.Core;
|
||||||
|
|
||||||
namespace BaseGames.Enemies.AI
|
namespace BaseGames.Enemies.AI
|
||||||
{
|
{
|
||||||
@@ -20,6 +21,9 @@ namespace BaseGames.Enemies.AI
|
|||||||
{
|
{
|
||||||
[SerializeField, Min(1)] private int _maxRequestersPerFrame = 8;
|
[SerializeField, Min(1)] private int _maxRequestersPerFrame = 8;
|
||||||
|
|
||||||
|
private void Awake() => ServiceLocator.Register<BatchLOSSystem>(this);
|
||||||
|
private void OnDestroy() => ServiceLocator.Unregister<BatchLOSSystem>(this);
|
||||||
|
|
||||||
private readonly List<ILOSRequester> _requesters = new();
|
private readonly List<ILOSRequester> _requesters = new();
|
||||||
private readonly HashSet<ILOSRequester> _requesterSet = new(); // O(1) 包含查询
|
private readonly HashSet<ILOSRequester> _requesterSet = new(); // O(1) 包含查询
|
||||||
// _indexMap 记录每个 requester 在 _requesters 中的下标,供 Unregister 实现 O(1) 删除
|
// _indexMap 记录每个 requester 在 _requesters 中的下标,供 Unregister 实现 O(1) 删除
|
||||||
|
|||||||
@@ -644,10 +644,12 @@ namespace BaseGames.Enemies
|
|||||||
protected virtual void OnEnable()
|
protected virtual void OnEnable()
|
||||||
{
|
{
|
||||||
_onPlayerSpawned?.Subscribe(SetPlayerTransform).AddTo(_subs);
|
_onPlayerSpawned?.Subscribe(SetPlayerTransform).AddTo(_subs);
|
||||||
|
Core.ServiceLocator.GetOrDefault<AI.BatchLOSSystem>()?.Register(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnDisable()
|
protected virtual void OnDisable()
|
||||||
{
|
{
|
||||||
|
Core.ServiceLocator.GetOrDefault<AI.BatchLOSSystem>()?.Unregister(this);
|
||||||
_subs.Clear();
|
_subs.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user