127 lines
4.5 KiB
C#
127 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Code.Infrastructure.Configs;
|
|
using Code.Infrastructure.EventBus;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Core;
|
|
using Reflex.Injectors;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace Code.Example.Platformer.Entity
|
|
{
|
|
public interface IPlatformerEnemyFactory
|
|
{
|
|
UniTaskVoid WarmEnemy(PlatformerEntityType enemyType);
|
|
void ReleaseEnemy(PlatformerEntityType racerType);
|
|
|
|
void SpawnEnemy(
|
|
PlatformerEntityType enemyType,
|
|
Vector3 position);
|
|
}
|
|
|
|
public class PlatformerEnemyFactory : IPlatformerEnemyFactory, IDisposable
|
|
{
|
|
private EventBus _eventBus;
|
|
|
|
private string _enemyConfigsLabel;
|
|
private AsyncOperationHandle<IList<PlatformerEntitySettingsConfig>> _enemyConfigsHandle;
|
|
private Dictionary<PlatformerEntityType, AsyncOperationHandle<GameObject>> _enemyPrefabsHandle = new();
|
|
|
|
private Dictionary<PlatformerEntityType, PlatformerEntitySettingsConfig> _enemyConfigs;
|
|
|
|
public PlatformerEnemyFactory(IConfigLocator configLocator, EventBus eventBus)
|
|
{
|
|
_eventBus = eventBus;
|
|
_enemyConfigsLabel = configLocator.TryGetManyConfigAssetReferences<PlatformerEntitySettingsConfig>();
|
|
|
|
LoadConfigs().Forget();
|
|
}
|
|
|
|
private async UniTaskVoid LoadConfigs()
|
|
{
|
|
_enemyConfigsHandle = Addressables.LoadAssetsAsync<PlatformerEntitySettingsConfig>(_enemyConfigsLabel);
|
|
|
|
await _enemyConfigsHandle.ToUniTask();
|
|
|
|
_enemyConfigs = _enemyConfigsHandle.Result.ToDictionary(x => x.EntityData.type);
|
|
|
|
_eventBus.RaiseEvent<IPlatformerEnemyFactoryInitializedHandle>(x => x.PlatformerEnemyFactoryInitialized());
|
|
}
|
|
|
|
public async UniTaskVoid WarmEnemy(PlatformerEntityType enemyType)
|
|
{
|
|
if (enemyType == PlatformerEntityType.Player) return;
|
|
|
|
if (_enemyPrefabsHandle.ContainsKey(enemyType))
|
|
{
|
|
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(enemyType, null));
|
|
|
|
return;
|
|
}
|
|
|
|
UnityEngine.Debug.LogWarning($"Enemy loaded {enemyType}");
|
|
_enemyPrefabsHandle.Add(enemyType,
|
|
Addressables.LoadAssetAsync<GameObject>(_enemyConfigs[enemyType].prefabReference));
|
|
|
|
await _enemyPrefabsHandle[enemyType].ToUniTask();
|
|
|
|
var entity = _enemyPrefabsHandle[enemyType].Result.GetComponent<Entity>();
|
|
|
|
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(enemyType, entity));
|
|
}
|
|
|
|
public void ReleaseEnemy(PlatformerEntityType racerType)
|
|
{
|
|
_enemyPrefabsHandle[racerType].Release();
|
|
_enemyPrefabsHandle.Remove(racerType);
|
|
}
|
|
|
|
public void SpawnEnemy(
|
|
PlatformerEntityType enemyType,
|
|
Vector3 position)
|
|
{
|
|
if (enemyType == PlatformerEntityType.Player) return;
|
|
|
|
var gameObject = Object.Instantiate(_enemyPrefabsHandle[enemyType].Result, position, Quaternion.identity);
|
|
|
|
var entity = gameObject.GetComponent<Entity>();
|
|
entity.SetData(_enemyConfigs[enemyType].EntityData);
|
|
|
|
var health = gameObject.GetComponent<EntityHealth>();
|
|
health.Setup(_enemyConfigs[enemyType].EntityData.maxHealth, _enemyConfigs[enemyType].EntityData.damageRecieveThreshold);
|
|
|
|
GameObjectInjector.InjectObject(gameObject, Container.RootContainer);
|
|
|
|
_eventBus.RaiseEvent<IPlatformerEnemySpawnedHandle>(x=>x.PlatformerEnemySpawned(entity));
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
UnityEngine.Debug.LogWarning($"Enemies released");
|
|
foreach (var prefab in _enemyPrefabsHandle)
|
|
prefab.Value.Release();
|
|
|
|
_enemyPrefabsHandle.Clear();
|
|
|
|
_enemyConfigsHandle.Release();
|
|
}
|
|
}
|
|
|
|
public interface IPlatformerEnemyFactoryInitializedHandle : IGlobalSubscriber
|
|
{
|
|
public void PlatformerEnemyFactoryInitialized();
|
|
}
|
|
|
|
public interface IPlatformerEnemyLoadedHandle : IGlobalSubscriber
|
|
{
|
|
public void PlatformerEnemyLoaded(PlatformerEntityType type, Entity entity);
|
|
}
|
|
public interface IPlatformerEnemySpawnedHandle : IGlobalSubscriber
|
|
{
|
|
public void PlatformerEnemySpawned(Entity entity);
|
|
}
|
|
} |