This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
sota/RunningLateGame/Assets/Scripts/AiManager.cs

95 lines
2.4 KiB
C#
Raw Normal View History

/*
* author: ryan lin
2024-08-10 21:40:22 +00:00
* date: 08/08/2024
2024-08-11 05:51:29 +00:00
* description: to cull and spawn AI people
*/
2024-08-08 08:11:12 +00:00
using System.Collections;
2024-08-12 06:07:33 +00:00
using System.Collections.Generic;
2024-08-08 08:11:12 +00:00
using UnityEngine;
2024-08-12 06:07:33 +00:00
using Random = System.Random;
2024-08-11 05:51:29 +00:00
2024-08-10 21:40:22 +00:00
/// <summary>
2024-08-11 05:51:29 +00:00
/// to cull and spawn AI people
2024-08-10 21:40:22 +00:00
/// </summary>
public class AIManager : MonoBehaviour
2024-08-08 08:11:12 +00:00
{
/// <summary>
2024-08-10 21:40:22 +00:00
/// a reference to the player
/// </summary>
2024-08-08 08:11:12 +00:00
public Transform player;
/// <summary>
2024-08-10 21:40:22 +00:00
/// the distance of which the AI would be "killed"
/// </summary>
2024-08-08 08:11:12 +00:00
public float cullingDistance;
/// <summary>
2024-08-11 05:51:29 +00:00
/// the prefab to spawn
/// </summary>
2024-08-08 08:11:12 +00:00
public GameObject aiPrefab;
/// <summary>
2024-08-10 21:40:22 +00:00
/// the maximum number of AI that can be spawned at any time
/// </summary>
public int maxAI;
/// <summary>
2024-08-10 21:40:22 +00:00
/// An array that contains the game objects of the AI objects
/// </summary>
2024-08-08 08:11:12 +00:00
private GameObject[] _ais;
/// <summary>
2024-08-10 21:40:22 +00:00
/// a temporary float to find the distance between the player and the AI
/// </summary>
2024-08-08 08:11:12 +00:00
private float _distance;
2024-08-12 06:07:33 +00:00
/// <summary>
/// AI Spawn locations
/// </summary>
[SerializeField]private List<Transform> aiSpawn;
/// <summary>
2024-08-11 05:51:29 +00:00
/// to start the manager loop
/// </summary>
2024-08-08 08:11:12 +00:00
private void Start()
{
StartCoroutine(Manager());
}
/// <summary>
2024-08-11 05:51:29 +00:00
/// to show the range in the inspector when the object is selected
/// </summary>
2024-08-10 21:40:22 +00:00
private void OnDrawGizmosSelected()
2024-08-08 08:11:12 +00:00
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(player.position, cullingDistance);
}
/// <summary>
2024-08-10 21:40:22 +00:00
/// to allow the IEnumerator to destroy or instantiate AI
/// </summary>
2024-08-08 08:11:12 +00:00
private IEnumerator Manager()
{
while (true)
{
// FIXME: feels weird
2024-08-08 08:11:12 +00:00
_ais = GameObject.FindGameObjectsWithTag("AIs");
if (_ais.Length < maxAI)
2024-08-08 08:11:12 +00:00
{
2024-08-12 06:07:33 +00:00
var rand = new Random();
var spawnNo=rand.Next(0, aiSpawn.Count);
var instance = Instantiate(aiPrefab, aiSpawn[spawnNo]);
2024-08-08 08:11:12 +00: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-08 08:11:12 +00:00
}
}