Files
Infratest/Assets/Code/Example/Platformer/Entity/PlatformerEnemyFactory.cs
T

115 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Code.Infrastructure.Configs;
using Code.Infrastructure.EventBus;
using Code.Infrastructure.Generators;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace Code.Example.Platformer.Entity
{
[Config("Configs/Gameplay/Platformer/EntitySettings", true)]
public class PlatformerEntitySettings
{
[ToConfig] public PlatformerEntityData EntityData;
[ToConfig] public AssetReference prefabReference;
}
[Serializable]
public struct PlatformerEntityData
{
public string name;
public float speed;
public float acceleration;
public int damage;
public PlatformerEntityType type;
public int maxHealth;
}
public enum PlatformerEntityType
{
Player,
Boar,
Bee,
Snail
}
public class PlatformerEnemyFactory
{
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.racerData.type);
_enemyConfigs.Remove(PlatformerEntityType.Player);
_eventBus.RaiseEvent<IPlatformerFactoryInitializedHandle>(x => x.PlatformerFactoryInitialized());
}
public async UniTaskVoid LoadRacer(PlatformerEntityType racerType)
{
if (racerType == PlatformerEntityType.Player) return;
if (_enemyPrefabsHandle.ContainsKey(racerType))
{
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(racerType, null));
return;
}
UnityEngine.Debug.LogWarning($"Racer loaded {racerType}");
_enemyPrefabsHandle.Add(racerType,
Addressables.LoadAssetAsync<GameObject>(_enemyConfigs[racerType].prefabReference));
await _enemyPrefabsHandle[racerType].ToUniTask();
var racer = _enemyPrefabsHandle[racerType].Result.GetComponent<Entity>();
racer.SetData(_enemyConfigs[racerType].racerData);
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(racerType, racer));
}
public void ReleaseRacer(PlatformerEntityType racerType)
{
_enemyPrefabsHandle[racerType].Release();
_enemyPrefabsHandle.Remove(racerType);
}
public void Dispose()
{
UnityEngine.Debug.LogWarning($"Racers released");
foreach (var prefab in _enemyPrefabsHandle)
prefab.Value.Release();
_enemyPrefabsHandle.Clear();
_enemyConfigsHandle.Release();
}
}
public interface IPlatformerFactoryInitializedHandle : IGlobalSubscriber
{
public void PlatformerFactoryInitialized();
}
public interface IPlatformerEnemyLoadedHandle : IGlobalSubscriber
{
public void PlatformerEnemyLoaded(PlatformerEntityType type, Entity entity);
}
}