45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
using System;
|
|
using Code.Infrastructure.Utils;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Attributes;
|
|
using Reflex.Core;
|
|
using Reflex.Injectors;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
namespace Code.Game.Gameplay.Player.Factory
|
|
{
|
|
public interface IPlayerFactory
|
|
{
|
|
UniTask<GameObject> CreatePlayer();
|
|
void InjectPlayer(Container container);
|
|
}
|
|
|
|
public class PlayerFactory : IDisposable, IPlayerFactory
|
|
{
|
|
private readonly SingleConfig<PlayerFactorySettings> _config;
|
|
|
|
private GameObject _playerInstance;
|
|
|
|
public PlayerFactory(SingleConfig<PlayerFactorySettings> config)
|
|
{
|
|
_config = config;
|
|
}
|
|
public async UniTask<GameObject> CreatePlayer()
|
|
{
|
|
_playerInstance ??= await Addressables.InstantiateAsync(_config.Config.Player).ToUniTask();
|
|
|
|
return _playerInstance;
|
|
}
|
|
|
|
public void InjectPlayer(Container container)
|
|
{
|
|
GameObjectInjector.InjectRecursive(_playerInstance, container);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Addressables.ReleaseInstance(_playerInstance);
|
|
}
|
|
}
|
|
} |