cache list of ped AIs

This commit is contained in:
in0finite 2021-09-06 23:41:56 +02:00
parent c48d59d7a1
commit f4a2e25d21
2 changed files with 21 additions and 6 deletions

View file

@ -78,7 +78,7 @@ namespace SanAndreasUnity.Behaviours
StartCoroutine(SpawnPedWithAI(targetZone));
}
}
NumberOfPeds = GameObject.FindObjectsOfType<Ped_AI>().Count();
NumberOfPeds = Ped_AI.AllPedAIs.Count;
lastUpdateTime = Time.time;
}
}
@ -113,7 +113,7 @@ namespace SanAndreasUnity.Behaviours
yield return null;
}
if (GameObject.FindObjectsOfType<Ped_AI>().Where(p => Math.Abs(Vector3.Distance(p.transform.position, targetZone)) < MaxNPCDistance).Count() > MaxNumberOfNPCAtSpawnPoint)
if (Ped_AI.AllPedAIs.Count(p => Math.Abs(Vector3.Distance(p.transform.position, targetZone)) < MaxNPCDistance) > MaxNumberOfNPCAtSpawnPoint)
break;
}
}

View file

@ -1,4 +1,6 @@
using Assets.Scripts.Importing.Paths;
using System;
using System.Collections.Generic;
using Assets.Scripts.Importing.Paths;
using UnityEngine;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Net;
@ -13,6 +15,9 @@ namespace SanAndreasUnity.Behaviours
}
public class Ped_AI : MonoBehaviour
{
private static readonly List<Ped_AI> s_allPedAIs = new List<Ped_AI>();
public static IReadOnlyList<Ped_AI> AllPedAIs => s_allPedAIs;
[SerializeField] private Vector3 currentNodePos;
[SerializeField] private Vector3 targetNodePos;
[SerializeField] private Vector2 targetNodeOffset; // Adding random offset to prevent peds to have the exact destination
@ -36,13 +41,23 @@ namespace SanAndreasUnity.Behaviours
public Ped MyPed { get; private set; }
// Use this for initialization
void Start()
private void Awake()
{
this.MyPed = this.GetComponentOrLogError<Ped>();
this.MyPed = this.GetComponentOrThrow<Ped>();
Ped.onDamaged += Ped_onDamaged;
}
private void OnEnable()
{
s_allPedAIs.Add(this);
}
private void OnDisable()
{
s_allPedAIs.Remove(this);
}
private void Ped_onDamaged(Ped hitPed, DamageInfo dmgInfo, Ped.DamageResult dmgResult)
{
if(hitPed.Equals(this.MyPed))