vampire-like/Assets/GameMain/Scripts/Runtime/CustomComponent/EnemyManager/EnemyManagerComponent.cs

222 lines
7.1 KiB
C#

using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using SepCore.DataTable;
using SepCore.Definition;
using SepCore.Entity;
using GameFramework.Event;
using SepCore.AsyncTask;
using SepCore.CustomUtility;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace SepCore.EnemyManager
{
public class EnemyManagerComponent : GameFrameworkComponent
{
private const float MinSpawnRateScale = 0.1f;
private const string EnemyGroupName = "Enemy";
private EntityComponent _entity;
private EnemyRegistry _enemyRegistry;
public List<EntityBase> Enemies => _enemyRegistry?.Enemies;
private float _spawnEnemyTimer;
[SerializeField] private int _spawnEnemyMaxCount = 5000;
[SerializeField] private int _spawnDistanceFromPlayer = 20;
private int _currentSpawnEnemyId;
private int _currentLevel;
private float[] _baseSpawnEnemyIntervals;
private float[] _spawnEnemyIntervals;
private EnemyType[] _spawnEnemyTypes;
private int[] _spawnEnemyCounts;
private float _duration;
private float _baseDuration;
private float[] _nextSpawnTimes;
private float _spawnRateScale = 1f;
private Transform _player;
private ISpawnPositionStrategy _spawnPositionStrategy;
public float SpawnRateScale => _spawnRateScale;
public float BattleDuration => _duration;
public float ElapsedBattleTime => _spawnEnemyTimer;
public int CurrentEnemyCount => _enemyRegistry?.Count ?? 0;
#region FSM
private void Start()
{
_entity = GameEntry.Entity;
_enemyRegistry = new EnemyRegistry();
GameEntry.Event.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
}
private void OnDestroy()
{
GameEntry.Event.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
_enemyRegistry = null;
_entity = null;
}
public void OnInit(DRLevel level, Player player)
{
_player = player != null ? player.CachedTransform : null;
_spawnPositionStrategy = new RandomCircleSpawnStrategy(_spawnDistanceFromPlayer);
_baseSpawnEnemyIntervals = (float[])level.Intervals.Clone();
_spawnEnemyIntervals = (float[])_baseSpawnEnemyIntervals.Clone();
_spawnEnemyTypes = level.EntityTypes;
_spawnEnemyCounts = level.EntityCounts;
_baseDuration = level.Duration;
_duration = _baseDuration;
SetSpawnRateScale(_spawnRateScale);
_enemyRegistry.Clear();
_currentSpawnEnemyId = 0;
}
public void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
_spawnEnemyTimer += elapseSeconds;
for (int i = 0; i < _nextSpawnTimes.Length; i++)
{
float nextSpawnTime = _nextSpawnTimes[i];
if (_spawnEnemyTimer < nextSpawnTime) continue;
for (int j = 0; j < _spawnEnemyCounts[i]; j++)
{
SpawnEnemyAsync(_spawnEnemyTypes[i]).Forget();
}
_nextSpawnTimes[i] += _spawnEnemyIntervals[i];
}
}
public void OnReset()
{
_spawnEnemyTimer = 0;
_currentSpawnEnemyId = 0;
_currentLevel = 0;
_baseSpawnEnemyIntervals = null;
_spawnEnemyIntervals = null;
_spawnEnemyTypes = null;
_spawnEnemyCounts = null;
_baseDuration = 0;
_duration = 0;
_nextSpawnTimes = null;
ClearEnemies();
}
#endregion
private async UniTaskVoid SpawnEnemyAsync(EnemyType enemyType)
{
if (_player == null) return;
if (_enemyRegistry.Count >= _spawnEnemyMaxCount) return;
int entityPoolId = _currentSpawnEnemyId % _spawnEnemyMaxCount;
var enemyData = EntityDataFactory.Create(entityPoolId, enemyType, _currentLevel);
enemyData.Position = _spawnPositionStrategy.GetSpawnPosition(_player);
_currentSpawnEnemyId++;
EnemyBase enemy = await _entity.ShowEnemyAsync(enemyData);
if (enemy == null)
{
return;
}
enemy.SetTarget(_player);
_enemyRegistry.Register(enemy);
}
public void ClearEnemies()
{
foreach (var enemy in _enemyRegistry.Enemies)
{
if (enemy == null || !enemy.Available) continue;
_entity.HideEntity(enemy);
}
_enemyRegistry.Clear();
}
public bool TryGetEnemy(int entityId, out EntityBase enemy)
{
return _enemyRegistry.TryGet(entityId, out enemy);
}
public void SetSpawnRateScale(float scale)
{
float newScale = Mathf.Max(MinSpawnRateScale, scale);
if (_baseSpawnEnemyIntervals == null || _baseSpawnEnemyIntervals.Length == 0)
{
_spawnRateScale = newScale;
return;
}
bool hasRuntimeState = _nextSpawnTimes != null && _spawnEnemyIntervals != null &&
_nextSpawnTimes.Length == _baseSpawnEnemyIntervals.Length &&
_spawnEnemyIntervals.Length == _baseSpawnEnemyIntervals.Length;
float oldScale = _spawnRateScale;
_spawnRateScale = newScale;
if (!hasRuntimeState)
{
for (int i = 0; i < _baseSpawnEnemyIntervals.Length; i++)
{
_spawnEnemyIntervals[i] = GetScaledInterval(_baseSpawnEnemyIntervals[i], _spawnRateScale);
}
_nextSpawnTimes = (float[])_spawnEnemyIntervals.Clone();
return;
}
for (int i = 0; i < _baseSpawnEnemyIntervals.Length; i++)
{
float oldInterval = GetScaledInterval(_baseSpawnEnemyIntervals[i], oldScale);
float newInterval = GetScaledInterval(_baseSpawnEnemyIntervals[i], _spawnRateScale);
float remainTime = Mathf.Max(0f, _nextSpawnTimes[i] - _spawnEnemyTimer);
float remainRatio = oldInterval > Mathf.Epsilon ? Mathf.Clamp01(remainTime / oldInterval) : 0f;
_spawnEnemyIntervals[i] = newInterval;
_nextSpawnTimes[i] = _spawnEnemyTimer + newInterval * remainRatio;
}
}
private static float GetScaledInterval(float baseInterval, float scale)
{
float safeScale = Mathf.Max(MinSpawnRateScale, scale);
return baseInterval / safeScale;
}
#region Event Handler
private void OnHideEntityComplete(object sender, GameEventArgs e)
{
if (e is not HideEntityCompleteEventArgs ne) return;
string entityGroupName = ne.EntityGroup.Name;
if (entityGroupName == EnemyGroupName)
{
_enemyRegistry.Remove(ne.EntityId);
}
}
#endregion
}
}