79 lines
2.0 KiB
C#
79 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class EnemyHandler : MonoBehaviour
|
|
{
|
|
[Header("Parameters")]
|
|
[SerializeField] [Range(0.1f, 60f)] float spawnRate = 60f;
|
|
[SerializeField] int objectPoolSize = 15;
|
|
|
|
[Header("Prefabs")]
|
|
[SerializeField] GameObject objectPool;
|
|
[SerializeField] List<GameObject> enemyPrefabs = new List<GameObject>();
|
|
|
|
[Header("Lists")]
|
|
[SerializeField] List<GameObject> enemyPools = new List<GameObject>();
|
|
[SerializeField] List<GameObject> allEnemies = new List<GameObject>();
|
|
|
|
private void Start()
|
|
{
|
|
//gridManager.CalculateNewPath();
|
|
|
|
PopulateObjectPools();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.N))
|
|
{
|
|
}
|
|
}
|
|
|
|
void PopulateObjectPools()
|
|
{
|
|
foreach (GameObject enemy in enemyPrefabs)
|
|
{
|
|
GameObject newPool = Instantiate(objectPool, transform);
|
|
newPool.transform.name = $"ObjectPool:{enemy.name}";
|
|
ObjectPool poolScript = newPool.GetComponent<ObjectPool>();
|
|
enemyPools.Add(newPool);
|
|
|
|
for (int i = 0; i < objectPoolSize; i++)
|
|
{
|
|
enemy.SetActive(false);
|
|
poolScript.AddObject(enemy);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddEnemyToAllEnemies(GameObject _enemy)
|
|
{
|
|
allEnemies.Add(_enemy);
|
|
}
|
|
|
|
public void RemoveEnemy(GameObject _enemy)
|
|
{
|
|
allEnemies.Remove(_enemy);
|
|
}
|
|
|
|
public List<GameObject> ReturnAllEnemies()
|
|
{
|
|
return allEnemies;
|
|
}
|
|
|
|
public Vector2Int GetCoordinatesFromPosition(Vector3 position)
|
|
{
|
|
Vector2Int coordinates = new Vector2Int();
|
|
coordinates.x = Mathf.RoundToInt(position.x / UnityEditor.EditorSnapSettings.move.x);
|
|
coordinates.y = Mathf.RoundToInt(position.z / UnityEditor.EditorSnapSettings.move.z);
|
|
|
|
return coordinates;
|
|
}
|
|
|
|
public void NotifyEnemiesOfNewPath()
|
|
{
|
|
BroadcastMessage("RecalculatePath", SendMessageOptions.DontRequireReceiver);
|
|
}
|
|
}
|