detect when ped is under aim of other ped

This commit is contained in:
in0finite 2022-01-01 22:28:32 +01:00
parent 1fb28ff19a
commit 15897d8656
3 changed files with 62 additions and 2 deletions

View file

@ -50,6 +50,23 @@ namespace SanAndreasUnity.Behaviours
public static event System.Action<Ped, DamageInfo, DamageResult> onDamaged = delegate {};
public struct UnderAimInfo
{
public DamageInfo damageInfo;
public float time;
public Ped ped;
public UnderAimInfo(DamageInfo damageInfo, float time, Ped ped)
{
this.damageInfo = damageInfo;
this.time = time;
this.ped = ped;
}
}
private List<UnderAimInfo> _underAimInfos = new List<UnderAimInfo>();
public IReadOnlyList<UnderAimInfo> UnderAimInfos => _underAimInfos;
void AwakeForDamage ()
@ -78,6 +95,9 @@ namespace SanAndreasUnity.Behaviours
void UpdateDamageStuff ()
{
this.UpdateHealthBar ();
// remove UnderAim info that is no longer valid
_underAimInfos.RemoveAll(this.ShouldUnderAimInfoBeRemoved);
}
void UpdateHealthBar ()
@ -181,6 +201,30 @@ namespace SanAndreasUnity.Behaviours
this.CurrentState.KillPed();
}
public void OnUnderAimOfOtherPed(DamageInfo damageInfo)
{
Ped attackerPed = damageInfo.GetAttackerPed();
if (null == attackerPed)
throw new System.Exception("No attacker ped given");
int index = _underAimInfos.FindIndex(_ => _.ped == attackerPed);
if (index >= 0)
{
_underAimInfos[index] = new UnderAimInfo(damageInfo, Time.time, attackerPed);
return;
}
_underAimInfos.Add(new UnderAimInfo(damageInfo, Time.time, attackerPed));
}
private bool ShouldUnderAimInfoBeRemoved(UnderAimInfo underAimInfo)
{
if (null == underAimInfo.ped)
return true;
return Time.time - underAimInfo.time > PedManager.Instance.timeIntervalToUpdateUnderAimStatus;
}
}
}

View file

@ -256,8 +256,22 @@ namespace SanAndreasUnity.Behaviours.Peds.States
{
m_weapon.AimAnimState = state;
if (state)
this.UpdateAimAnim (state);
}
{
this.UpdateAimAnim (state);
GetEffectiveFirePosAndDir(m_ped, WeaponAttackParams.Default, out Vector3 pos, out Vector3 dir);
Damageable damagable = m_weapon.ProjectileRaycastForDamagable(
pos, dir, WeaponAttackParams.Default, out bool attackWillBeConducted, out DamageInfo damageInfo);
if (attackWillBeConducted && damagable != null)
{
var targetPed = damagable.GetComponent<Ped>();
if (targetPed != null)
{
targetPed.OnUnderAimOfOtherPed(damageInfo);
}
}
}
}
if (m_weapon && m_weapon.HasFlag(GunFlag.AIMWITHARM))
{

View file

@ -27,6 +27,8 @@ namespace SanAndreasUnity.Behaviours
public float timeUntilAimMovementStateCanBeSwitchedToOtherAimMovementState = 0.166f;
public float timeUntilAimMovementStateCanBeEnteredFromOtherAimMovementState = 0.166f;
public float timeIntervalToUpdateUnderAimStatus = 0.33f;
[Header("Camera")]
public float cameraDistanceFromPed = 3f;