57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using System;
|
|
using Code.Configs;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Attributes;
|
|
using Reflex.Core;
|
|
using Reflex.Extensions;
|
|
using Reflex.Injectors;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Code.Gameplay.Enemies
|
|
{
|
|
public class EnemyFactory : IEnemyFactory
|
|
{
|
|
[Inject] private FogConfig _fogConfig;
|
|
[Inject] private DrunkenGrandpaConfig _drunkenGrandpaConfig;
|
|
[Inject] private CockroachConfig _cockroachConfig;
|
|
|
|
public async UniTask<BaseEnemy> CreateEnemy(EnemyType type, Vector3? position = null, Quaternion? rotation = null)
|
|
{
|
|
var prefabRef = GetEnemyPrefab(type);
|
|
|
|
var enemyObj = await Addressables
|
|
.InstantiateAsync(
|
|
prefabRef,
|
|
position: position ?? Vector3.zero,
|
|
rotation: rotation ?? Quaternion.identity
|
|
)
|
|
.ToUniTask();
|
|
|
|
GameObjectInjector.InjectRecursive(enemyObj, SceneManager.GetActiveScene().GetSceneContainer());
|
|
|
|
return enemyObj.GetComponent<BaseEnemy>();
|
|
}
|
|
|
|
public void ReleaseEnemy(BaseEnemy enemy)
|
|
{
|
|
if (!enemy) return;
|
|
|
|
enemy.gameObject.SetActive(false);
|
|
|
|
Addressables.ReleaseInstance(enemy.gameObject);
|
|
}
|
|
|
|
private AssetReference GetEnemyPrefab(EnemyType type)
|
|
{
|
|
return type switch
|
|
{
|
|
EnemyType.Fog => _fogConfig.fogPrefab,
|
|
EnemyType.DrunkenGrandpa => _drunkenGrandpaConfig.drunkenGrandpaPrefab,
|
|
EnemyType.Cockroach => _cockroachConfig.cockroachPrefab,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
|
};
|
|
}
|
|
}
|
|
} |