84 lines
3.4 KiB
C#
84 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Code.Infrastructure.Configs;
|
|
using Code.Infrastructure.EventBus;
|
|
using Cysharp.Threading.Tasks;
|
|
using Unity.Cinemachine;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace Code.Infrastructure.Camera
|
|
{
|
|
public class CameraFactory : IDisposable
|
|
{
|
|
private AsyncOperationHandle<GameCameraConfig> _configHandle;
|
|
private GameCameraConfig _config;
|
|
private AssetReference _gameCameraConfigReference;
|
|
|
|
private AsyncOperationHandle<IList<CameraSettingsConfig>> _settingsConfigsHandle;
|
|
private Dictionary<CameraMode, CameraSettingsConfig> _settingsConfigs;
|
|
private string _cameraConfigsLabel;
|
|
|
|
private EventBus.EventBus _eventBus;
|
|
private UnityEngine.Camera _camera;
|
|
|
|
private Dictionary<CameraMode, CinemachineCamera> _cinemachines = new();
|
|
|
|
public CameraFactory(IConfigLocator configLocator, EventBus.EventBus eventBus)
|
|
{
|
|
_gameCameraConfigReference = configLocator.TryGetConfigAssetReference<GameCameraConfig>();
|
|
_cameraConfigsLabel = configLocator.TryGetManyConfigAssetReferences<CameraSettingsConfig>();
|
|
|
|
_eventBus = eventBus;
|
|
}
|
|
|
|
public async UniTaskVoid CreateCameraStateMachine(CameraStateMachine stateMachine)
|
|
{
|
|
_settingsConfigsHandle = Addressables
|
|
.LoadAssetsAsync<CameraSettingsConfig>(_cameraConfigsLabel, null, Addressables.MergeMode.Union);
|
|
await _settingsConfigsHandle.ToUniTask();
|
|
_settingsConfigs = _settingsConfigsHandle.Result.ToDictionary(x => x.cameraMode);
|
|
|
|
_configHandle = Addressables.LoadAssetAsync<GameCameraConfig>(_gameCameraConfigReference);
|
|
await _configHandle.ToUniTask();
|
|
_config = _configHandle.Result;
|
|
|
|
var cameraObject = await Addressables.InstantiateAsync(_config.Camera).ToUniTask();
|
|
_camera = cameraObject.GetComponent<UnityEngine.Camera>();
|
|
|
|
cameraObject.GetComponent<CameraStateMachineRunner>().StateMachine = stateMachine;
|
|
Object.DontDestroyOnLoad(cameraObject);
|
|
|
|
_eventBus.RaiseEvent<ICameraInitializedHandle>(x => x.CameraInitialized(new(_camera, _settingsConfigs)));
|
|
}
|
|
|
|
public async UniTaskVoid LoadCinemachineCamera(CameraMode cameraMode)
|
|
{
|
|
var cinemachine = await Addressables.InstantiateAsync(_settingsConfigs[cameraMode].cinemachineCamera);
|
|
Object.DontDestroyOnLoad(cinemachine);
|
|
_cinemachines[cameraMode] = cinemachine.GetComponent<CinemachineCamera>();
|
|
_eventBus.RaiseEvent<ICinematicMachineHandle>(x=> x.CinemachineLoaded(_cinemachines[cameraMode]));
|
|
}
|
|
|
|
public CinemachineCamera GetCinematicCamera(CameraMode cameraMode) => _cinemachines[cameraMode];
|
|
|
|
public void ReleaseCinemachineCamera(CameraMode cameraMode)
|
|
{
|
|
Addressables.Release(_cinemachines[cameraMode].gameObject);
|
|
}
|
|
public void Dispose()
|
|
{
|
|
Addressables.Release(_configHandle);
|
|
Addressables.Release(_settingsConfigsHandle);
|
|
Addressables.ReleaseInstance(_camera.gameObject);
|
|
}
|
|
}
|
|
|
|
public interface ICinematicMachineHandle : IGlobalSubscriber
|
|
{
|
|
void CinemachineLoaded(CinemachineCamera cinemachineCamera);
|
|
}
|
|
} |