Фабрика противников

This commit is contained in:
ThomasCharester committed 2026-03-10 12:44:12 +03:00
1 parent 93819c1dd3
commit 653342d080
35 files changed
+527 -406

No files matched your search

@@ -3,7 +3,6 @@ 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;
@@ -11,40 +10,24 @@ using UnityEngine.ResourceManagement.AsyncOperations;
namespace Code.Example.Platformer.Entity
{
[Config("Configs/Gameplay/Platformer/EntitySettings", true)]
public class PlatformerEntitySettings
public interface IPlatformerEnemyFactory
{
[ToConfig] public PlatformerEntityData EntityData;
UniTaskVoid LoadEnemy(PlatformerEntityType enemyType);
void ReleaseEnemy(PlatformerEntityType racerType);
[ToConfig] public AssetReference prefabReference;
UniTaskVoid SpawnEnemy(
PlatformerEntityType enemyType,
Vector3 position);
}
[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
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)
@@ -52,62 +35,75 @@ namespace Code.Example.Platformer.Entity
_eventBus = eventBus;
_enemyConfigsLabel = configLocator.TryGetManyConfigAssetReferences<PlatformerEntitySettingsConfig>();
LoadConfigs().Forget();
LoadConfigs().Forget();
}
private async UniTaskVoid LoadConfigs()
{
_enemyConfigsHandle = Addressables.LoadAssetsAsync<PlatformerEntitySettingsConfig>(_enemyConfigsLabel);
await _enemyConfigsHandle.ToUniTask();
_enemyConfigs = _enemyConfigsHandle.Result.ToDictionary(x => x.racerData.type);
_enemyConfigs = _enemyConfigsHandle.Result.ToDictionary(x => x.EntityData.type);
_enemyConfigs.Remove(PlatformerEntityType.Player);
_eventBus.RaiseEvent<IPlatformerFactoryInitializedHandle>(x => x.PlatformerFactoryInitialized());
_eventBus.RaiseEvent<IPlatformerEnemyFactoryInitializedHandle>(x => x.PlatformerEnemyFactoryInitialized());
}
public async UniTaskVoid LoadRacer(PlatformerEntityType racerType)
public async UniTaskVoid LoadEnemy(PlatformerEntityType enemyType)
{
if (racerType == PlatformerEntityType.Player) return;
if (_enemyPrefabsHandle.ContainsKey(racerType))
if (enemyType == PlatformerEntityType.Player) return;
if (_enemyPrefabsHandle.ContainsKey(enemyType))
{
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(racerType, null));
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(enemyType, 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));
}
UnityEngine.Debug.LogWarning($"Enemy loaded {enemyType}");
_enemyPrefabsHandle.Add(enemyType,
Addressables.LoadAssetAsync<GameObject>(_enemyConfigs[enemyType].prefabReference));
await _enemyPrefabsHandle[enemyType].ToUniTask();
var racer = _enemyPrefabsHandle[enemyType].Result.GetComponent<Entity>();
racer.SetData(_enemyConfigs[enemyType].EntityData);
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(enemyType, racer));
}
public void ReleaseRacer(PlatformerEntityType racerType)
public void ReleaseEnemy(PlatformerEntityType racerType)
{
_enemyPrefabsHandle[racerType].Release();
_enemyPrefabsHandle.Remove(racerType);
}
public async UniTaskVoid SpawnEnemy(
PlatformerEntityType enemyType,
Vector3 position)
{
if (enemyType == PlatformerEntityType.Player) return;
await Addressables.InstantiateAsync(_enemyConfigs[enemyType].prefabReference, position, Quaternion.identity);
}
public void Dispose()
{
UnityEngine.Debug.LogWarning($"Racers released");
foreach (var prefab in _enemyPrefabsHandle)
UnityEngine.Debug.LogWarning($"Enemies released");
foreach (var prefab in _enemyPrefabsHandle)
prefab.Value.Release();
_enemyPrefabsHandle.Clear();
_enemyConfigsHandle.Release();
}
}
public interface IPlatformerFactoryInitializedHandle : IGlobalSubscriber
public interface IPlatformerEnemyFactoryInitializedHandle : IGlobalSubscriber
{
public void PlatformerFactoryInitialized();
public void PlatformerEnemyFactoryInitialized();
}
public interface IPlatformerEnemyLoadedHandle : IGlobalSubscriber
{
public void PlatformerEnemyLoaded(PlatformerEntityType type, Entity entity);