Развитие гоночек
This commit is contained in:
1 parent
ec60c47652
commit
b54083521f
532 files changed
+16091
-28231
No files matched your search
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
using EventBus = Code.Infrastructure.EventBus.EventBus;
|
||||
|
||||
namespace Code.Example.Race.Racer
|
||||
{
|
||||
public interface IRacerFactory
|
||||
{
|
||||
RacerSettingsConfig GetConfig(RacerType racerType);
|
||||
UniTaskVoid LoadRacer(RacerType racerType);
|
||||
void ReleaseRacer(RacerType racerType);
|
||||
}
|
||||
|
||||
public class RacerFactory : IDisposable, IRacerFactory
|
||||
{
|
||||
private Dictionary<RacerType, RacerSettingsConfig> _racerConfigs;
|
||||
private Dictionary<RacerType, AsyncOperationHandle<GameObject>> _racerPrefabsHandle = new();
|
||||
|
||||
private string _racerConfigsLabel;
|
||||
private AsyncOperationHandle<IList<RacerSettingsConfig>> _racerConfigsHandle;
|
||||
|
||||
private EventBus _eventBus;
|
||||
|
||||
public RacerFactory(IConfigLocator configLocator, EventBus eventBus)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
_racerConfigsLabel = configLocator.TryGetManyConfigAssetReferences<RacerSettingsConfig>();
|
||||
|
||||
LoadConfigs().Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadConfigs()
|
||||
{
|
||||
_racerConfigsHandle = Addressables.LoadAssetsAsync<RacerSettingsConfig>(_racerConfigsLabel);
|
||||
|
||||
await _racerConfigsHandle.ToUniTask();
|
||||
|
||||
_racerConfigs = _racerConfigsHandle.Result.ToDictionary(x => x.racerData.type);
|
||||
|
||||
_eventBus.RaiseEvent<IRacerFactoryInitializedHandle>(x => x.RacerFactoryInitialized());
|
||||
}
|
||||
|
||||
public RacerSettingsConfig GetConfig(RacerType racerType) => _racerConfigs[racerType];
|
||||
|
||||
public async UniTaskVoid LoadRacer(RacerType racerType)
|
||||
{
|
||||
if (_racerPrefabsHandle.ContainsKey(racerType))
|
||||
{
|
||||
_eventBus.RaiseEvent<IRacerLoadedHandle>(x => x.RacerLoaded(racerType, null));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_racerPrefabsHandle.Add(racerType,
|
||||
Addressables.LoadAssetAsync<GameObject>(_racerConfigs[racerType].prefabReference));
|
||||
|
||||
await _racerPrefabsHandle[racerType].ToUniTask();
|
||||
var racer = _racerPrefabsHandle[racerType].Result.GetComponent<AI.Racer>();
|
||||
racer.SetData(_racerConfigs[racerType].racerData);
|
||||
|
||||
_eventBus.RaiseEvent<IRacerLoadedHandle>(x => x.RacerLoaded(racerType, racer));
|
||||
}
|
||||
|
||||
public void ReleaseRacer(RacerType racerType)
|
||||
{
|
||||
_racerPrefabsHandle[racerType].Release();
|
||||
_racerPrefabsHandle.Remove(racerType);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var prefab in _racerPrefabsHandle)
|
||||
prefab.Value.Release();
|
||||
|
||||
_racerConfigsHandle.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IRacerFactoryInitializedHandle : IGlobalSubscriber
|
||||
{
|
||||
public void RacerFactoryInitialized();
|
||||
}
|
||||
|
||||
public interface IRacerLoadedHandle : IGlobalSubscriber
|
||||
{
|
||||
public void RacerLoaded(RacerType type, AI.Racer racer);
|
||||
public void RacerCreated(RacerType type, AI.Racer racer);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user