2024-08-10 02:25:46 +08:00
|
|
|
/*
|
|
|
|
* author: ryan lin
|
2024-08-11 05:40:22 +08:00
|
|
|
* date: 08/08/2024
|
2024-08-11 13:51:29 +08:00
|
|
|
* 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;
|
2024-08-12 14:07:33 +08:00
|
|
|
using System.Collections.Generic;
|
2024-08-08 16:11:12 +08:00
|
|
|
using UnityEngine;
|
2024-08-12 14:07:33 +08:00
|
|
|
using Random = System.Random;
|
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
|
2024-08-11 05:40:22 +08:00
|
|
|
/// <summary>
|
2024-08-11 13:51:29 +08:00
|
|
|
/// to cull and spawn AI people
|
2024-08-11 05:40:22 +08:00
|
|
|
/// </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 13:51:29 +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;
|
|
|
|
|
2024-08-15 16:25:18 +08:00
|
|
|
/// <summary>
|
|
|
|
/// AI Spawn locations
|
|
|
|
/// </summary>
|
2024-08-16 21:58:40 +08:00
|
|
|
[SerializeField] private GameObject[] aiSpawn;
|
2024-08-15 16:25:18 +08:00
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <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 13:51:29 +08:00
|
|
|
/// to start the manager loop
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 16:11:12 +08:00
|
|
|
private void Start()
|
|
|
|
{
|
2024-08-16 21:58:40 +08:00
|
|
|
aiSpawn = GameObject.FindGameObjectsWithTag($"AISpawn");
|
|
|
|
StartCoroutine(nameof(Manager));
|
|
|
|
|
2024-08-08 16:11:12 +08:00
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 13:51:29 +08:00
|
|
|
/// to show the range in the inspector when the object is selected
|
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()
|
|
|
|
{
|
2024-08-16 21:58:40 +08:00
|
|
|
|
2024-08-08 16:11:12 +08:00
|
|
|
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-12 14:07:33 +08:00
|
|
|
var rand = new Random();
|
2024-08-16 21:58:40 +08:00
|
|
|
var spawnNo = rand.Next(0, aiSpawn.Length);
|
2024-08-15 16:25:18 +08:00
|
|
|
|
2024-08-16 21:58:40 +08:00
|
|
|
var instance = Instantiate(aiPrefab, aiSpawn[spawnNo].transform.position, aiSpawn[spawnNo].transform.rotation);
|
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);
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:58:40 +08:00
|
|
|
yield return new WaitForEndOfFrame();
|
2024-08-10 02:25:46 +08:00
|
|
|
}
|
2024-08-08 16:11:12 +08:00
|
|
|
}
|
|
|
|
}
|