added enemy factory and pool
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Darkmatter.Core;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
using VContainer;
|
||||
|
||||
namespace Darkmatter.Presentation
|
||||
@@ -7,14 +8,54 @@ namespace Darkmatter.Presentation
|
||||
public class EnemiesSpawnner : MonoBehaviour
|
||||
{
|
||||
[Inject] IEnemyFactory _enemyFactory;
|
||||
public int enemiesCount = 2;
|
||||
public int enemiesPerWave = 5;
|
||||
private ObjectPool<IEnemyPawn> _enemyPool;
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
EnemyMotor.OnEnemyDead += ReturnEnemy;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
EnemyMotor.OnEnemyDead -= ReturnEnemy;
|
||||
}
|
||||
private void Awake()
|
||||
{
|
||||
_enemyPool = new ObjectPool<IEnemyPawn>(
|
||||
createFunc: () => _enemyFactory.GetEnemy(GetRandomType()),
|
||||
actionOnGet: enemy => enemy.GameObject.SetActive(true),
|
||||
actionOnRelease: enemy => enemy.GameObject.SetActive(false),
|
||||
actionOnDestroy: enemy => Destroy(enemy.GameObject),
|
||||
collectionCheck: true,
|
||||
defaultCapacity: 10,
|
||||
maxSize: 50
|
||||
);
|
||||
}
|
||||
|
||||
private ZombieType GetRandomType()
|
||||
{
|
||||
return Random.value > 0.5f ? ZombieType.Fat : ZombieType.slim;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
for (int i = 0;i< enemiesCount;i++)
|
||||
SpawnWave();
|
||||
}
|
||||
|
||||
private void SpawnWave()
|
||||
{
|
||||
for (int i = 0; i < enemiesPerWave; i++)
|
||||
{
|
||||
_enemyFactory.GetEnemy(ZombieType.Fat);
|
||||
IEnemyPawn enemy = _enemyPool.Get();
|
||||
enemy.GameObject.transform.position = enemy.PatrolPoints[Random.Range(0, enemy.PatrolPoints.Count)].position;
|
||||
}
|
||||
}
|
||||
|
||||
public void ReturnEnemy(IEnemyPawn enemy)
|
||||
{
|
||||
_enemyPool.Release(enemy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ namespace Darkmatter.Presentation
|
||||
{
|
||||
public class EnemyMotor : MonoBehaviour, IEnemyPawn
|
||||
{
|
||||
public static event Action<IEnemyPawn> OnEnemyDead;
|
||||
public GameObject GameObject => this.gameObject;
|
||||
[SerializeField] private NavMeshAgent enemyAI;
|
||||
[Inject] private EnemyConfigSO enemyConfig;
|
||||
|
||||
public Transform PlayerTarget { get; private set; }
|
||||
public NavMeshAgent EnemyAI => enemyAI;
|
||||
@@ -24,6 +25,7 @@ namespace Darkmatter.Presentation
|
||||
|
||||
public void Die()
|
||||
{
|
||||
if(isDead) return;
|
||||
isDead = true;
|
||||
enemyAI.enabled = false;
|
||||
Invoke(nameof(Hide), 8f);
|
||||
@@ -31,7 +33,7 @@ namespace Darkmatter.Presentation
|
||||
|
||||
private void Hide()
|
||||
{
|
||||
this.gameObject.SetActive(false);
|
||||
OnEnemyDead?.Invoke(this);
|
||||
}
|
||||
|
||||
public Vector3 ReturnMyPos()
|
||||
@@ -51,14 +53,6 @@ namespace Darkmatter.Presentation
|
||||
OnHealthDecreased?.Invoke(Health);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawWireSphere(transform.position, enemyConfig.visionRange);
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, enemyConfig.attackRange);
|
||||
}
|
||||
|
||||
public void InitializeFromFactory(Transform player, List<Transform> patrolPoints)
|
||||
{
|
||||
this.PlayerTarget = player;
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace Darkmatter.Presentation
|
||||
{
|
||||
public class PlayerMotor : MonoBehaviour, IPlayerPawn
|
||||
{
|
||||
|
||||
[Header("LookSetting")]
|
||||
public Transform cinemachineFollowTarget;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user