调整 EnemyManagerComponent 部分逻辑到异步操作
This commit is contained in:
parent
0bab6ecbc5
commit
734f2cd5af
|
|
@ -50,7 +50,7 @@ namespace SepCore.Procedure
|
||||||
}
|
}
|
||||||
|
|
||||||
_levelTimeLeft = drLevel.Duration;
|
_levelTimeLeft = drLevel.Duration;
|
||||||
_enemyManager.OnInit(drLevel);
|
_enemyManager.OnInit(drLevel, Player);
|
||||||
|
|
||||||
if (Player != null) Player.Enable = true;
|
if (Player != null) Player.Enable = true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Cysharp.Threading.Tasks;
|
||||||
using SepCore.DataTable;
|
using SepCore.DataTable;
|
||||||
using SepCore.Definition;
|
using SepCore.Definition;
|
||||||
using SepCore.Entity;
|
using SepCore.Entity;
|
||||||
using GameFramework.Event;
|
using GameFramework.Event;
|
||||||
|
using SepCore.AsyncTask;
|
||||||
using SepCore.CustomUtility;
|
using SepCore.CustomUtility;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityGameFramework.Runtime;
|
using UnityGameFramework.Runtime;
|
||||||
|
|
@ -59,25 +61,22 @@ namespace SepCore.EnemyManager
|
||||||
_enemies = new List<EntityBase>();
|
_enemies = new List<EntityBase>();
|
||||||
_enemyById = new Dictionary<int, EntityBase>();
|
_enemyById = new Dictionary<int, EntityBase>();
|
||||||
|
|
||||||
GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
|
|
||||||
GameEntry.Event.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
|
GameEntry.Event.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDestroy()
|
private void OnDestroy()
|
||||||
{
|
{
|
||||||
if (GameEntry.Event != null)
|
|
||||||
{
|
|
||||||
GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
|
|
||||||
GameEntry.Event.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
|
GameEntry.Event.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
|
||||||
}
|
|
||||||
|
|
||||||
_enemies = null;
|
_enemies = null;
|
||||||
_enemyById = null;
|
_enemyById = null;
|
||||||
_entity = null;
|
_entity = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnInit(DRLevel level)
|
public void OnInit(DRLevel level, Player player)
|
||||||
{
|
{
|
||||||
|
_player = player != null ? player.CachedTransform : null;
|
||||||
|
|
||||||
_baseSpawnEnemyIntervals = (float[])level.Intervals.Clone();
|
_baseSpawnEnemyIntervals = (float[])level.Intervals.Clone();
|
||||||
_spawnEnemyIntervals = (float[])_baseSpawnEnemyIntervals.Clone();
|
_spawnEnemyIntervals = (float[])_baseSpawnEnemyIntervals.Clone();
|
||||||
_spawnEnemyTypes = level.EntityTypes;
|
_spawnEnemyTypes = level.EntityTypes;
|
||||||
|
|
@ -101,7 +100,7 @@ namespace SepCore.EnemyManager
|
||||||
if (_spawnEnemyTimer < nextSpawnTime) continue;
|
if (_spawnEnemyTimer < nextSpawnTime) continue;
|
||||||
for (int j = 0; j < _spawnEnemyCounts[i]; j++)
|
for (int j = 0; j < _spawnEnemyCounts[i]; j++)
|
||||||
{
|
{
|
||||||
SpawnEnemy(_spawnEnemyTypes[i]);
|
SpawnEnemyAsync(_spawnEnemyTypes[i]).Forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
_nextSpawnTimes[i] += _spawnEnemyIntervals[i];
|
_nextSpawnTimes[i] += _spawnEnemyIntervals[i];
|
||||||
|
|
@ -130,7 +129,7 @@ namespace SepCore.EnemyManager
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private void SpawnEnemy(EnemyType enemyType)
|
private async UniTaskVoid SpawnEnemyAsync(EnemyType enemyType)
|
||||||
{
|
{
|
||||||
if (_player == null) return;
|
if (_player == null) return;
|
||||||
|
|
||||||
|
|
@ -138,8 +137,15 @@ namespace SepCore.EnemyManager
|
||||||
int entityPoolId = _currentSpawnEnemyId % _spawnEnemyMaxCount;
|
int entityPoolId = _currentSpawnEnemyId % _spawnEnemyMaxCount;
|
||||||
var enemyData = EntityDataFactory.Create(entityPoolId, enemyType, _currentLevel);
|
var enemyData = EntityDataFactory.Create(entityPoolId, enemyType, _currentLevel);
|
||||||
enemyData.Position = GetRandomPosition();
|
enemyData.Position = GetRandomPosition();
|
||||||
_entity.ShowEnemy(enemyData);
|
|
||||||
_currentSpawnEnemyId++;
|
_currentSpawnEnemyId++;
|
||||||
|
|
||||||
|
EnemyBase enemy = await _entity.ShowEnemyAsync(enemyData);
|
||||||
|
if (enemy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RegisterEnemy(enemy);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector3 GetRandomPosition()
|
private Vector3 GetRandomPosition()
|
||||||
|
|
@ -227,14 +233,13 @@ namespace SepCore.EnemyManager
|
||||||
|
|
||||||
#region Event Handler
|
#region Event Handler
|
||||||
|
|
||||||
private void OnShowEntitySuccess(object sender, GameEventArgs e)
|
private void RegisterEnemy(EnemyBase enemy)
|
||||||
{
|
{
|
||||||
if (!(e is ShowEntitySuccessEventArgs ne)) return;
|
if (enemy == null)
|
||||||
|
|
||||||
string entityGroupName = ne.Entity?.EntityGroup?.Name;
|
|
||||||
|
|
||||||
if (entityGroupName == EnemyGroupName && ne.Entity.Logic is EnemyBase enemy)
|
|
||||||
{
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_currentEnemyCount++;
|
_currentEnemyCount++;
|
||||||
enemy.SetTarget(_player);
|
enemy.SetTarget(_player);
|
||||||
RemoveEnemyFromCache(enemy.Id);
|
RemoveEnemyFromCache(enemy.Id);
|
||||||
|
|
@ -242,16 +247,10 @@ namespace SepCore.EnemyManager
|
||||||
_enemyById[enemy.Id] = enemy;
|
_enemyById[enemy.Id] = enemy;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ne.EntityLogicType == typeof(Player))
|
|
||||||
{
|
|
||||||
_player = ne.Entity.transform;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnHideEntityComplete(object sender, GameEventArgs e)
|
private void OnHideEntityComplete(object sender, GameEventArgs e)
|
||||||
{
|
{
|
||||||
if (e is HideEntityCompleteEventArgs ne)
|
if (e is not HideEntityCompleteEventArgs ne) return;
|
||||||
{
|
|
||||||
string entityGroupName = ne.EntityGroup.Name;
|
string entityGroupName = ne.EntityGroup.Name;
|
||||||
if (entityGroupName == EnemyGroupName)
|
if (entityGroupName == EnemyGroupName)
|
||||||
{
|
{
|
||||||
|
|
@ -263,7 +262,6 @@ namespace SepCore.EnemyManager
|
||||||
RemoveEnemyFromCache(ne.EntityId);
|
RemoveEnemyFromCache(ne.EntityId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void RemoveEnemyFromCache(int entityId)
|
private void RemoveEnemyFromCache(int entityId)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue