/*
* author: ryan lin
* date: 08/08/2024
* description: to cull and spawn AI people
*/
using System.Collections;
using UnityEngine;
///
/// to cull and spawn AI people
///
public class AIManager : MonoBehaviour
{
///
/// a reference to the player
///
public Transform player;
///
/// the distance of which the AI would be "killed"
///
public float cullingDistance;
///
/// the prefab to spawn
///
public GameObject aiPrefab;
///
/// the maximum number of AI that can be spawned at any time
///
public int maxAI;
///
/// An array that contains the game objects of the AI objects
///
private GameObject[] _ais;
///
/// a temporary float to find the distance between the player and the AI
///
private float _distance;
///
/// to start the
///
private void Start()
{
StartCoroutine(Manager());
}
///
/// to show the range in the inspector
///
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(player.position, cullingDistance);
}
///
/// to allow the IEnumerator to destroy or instantiate AI
///
private IEnumerator Manager()
{
while (true)
{
// FIXME: feels weird
_ais = GameObject.FindGameObjectsWithTag("AIs");
if (_ais.Length < maxAI)
{
// var instance = Instantiate(aiPrefab, gameObject.transform);
}
foreach (var i in _ais)
{
_distance = Vector3.Distance(i.transform.position, player.position);
if (_distance > cullingDistance) Destroy(i.gameObject);
}
yield return new WaitForSeconds(1);
}
}
}