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