notify ped AI states when ped is under aim

This commit is contained in:
in0finite 2022-01-01 22:29:47 +01:00
parent 15897d8656
commit bdabcfe8cd
2 changed files with 51 additions and 0 deletions

View file

@ -81,6 +81,11 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
{
}
protected internal virtual void OnUnderAim(IReadOnlyList<Ped.UnderAimInfo> underAimInfos)
{
//UnityEngine.Debug.Log($"OnUnderAim() for ped {_ped.DescriptionForLogging}: {string.Join("\n", underAimInfos.Select(_ => $"{_.ped.DescriptionForLogging}, {_.time}"))}");
}
protected internal virtual void OnRecruit(Ped recruiterPed)
{
}

View file

@ -27,6 +27,8 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
private float _timeSinceUpdatedStateOn2Seconds = 0f;
private List<Ped.UnderAimInfo> _lastUnderAimInfos = null;
private void Awake()
{
@ -100,6 +102,8 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
}
this.CurrentState.UpdateState();
this.CheckIfSomeoneAimedAtOurPed();
}
}
@ -238,6 +242,48 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
return pathNodeInfo.node;
}
private void CheckIfSomeoneAimedAtOurPed()
{
var underAimInfos = this.MyPed.UnderAimInfos;
if (underAimInfos.Count == 0)
{
_lastUnderAimInfos = null;
return;
}
// there are active "aims" on our ped
// check if some of them are new
// if yes, notify the current state
if (null == _lastUnderAimInfos)
_lastUnderAimInfos = new List<Ped.UnderAimInfo>();
List<Ped.UnderAimInfo> newInfos = null;
for (int i = 0; i < underAimInfos.Count; i++)
{
if (!_lastUnderAimInfos.Contains(underAimInfos[i]))
{
// new one
if (null == newInfos)
newInfos = new List<Ped.UnderAimInfo>();
newInfos.Add(underAimInfos[i]);
}
}
// remember last state of the list
_lastUnderAimInfos.Clear();
_lastUnderAimInfos.AddRange(underAimInfos);
// notify current state
if (newInfos != null)
{
this.CurrentState.OnUnderAim(newInfos);
}
}
private void OnDrawGizmosSelected()
{
this.CurrentState.OnDrawGizmosSelected();