SanAndreasUnity/Assets/Scripts/Behaviours/PedAI/ChaseState.cs

192 lines
5.8 KiB
C#
Raw Normal View History

2021-09-19 20:17:46 +00:00
using System.Linq;
using SanAndreasUnity.Utilities;
using UnityEngine;
namespace SanAndreasUnity.Behaviours.Peds.AI
{
public class ChaseState : BaseState
{
public Ped TargetPed { get; private set; }
2021-10-10 17:45:34 +00:00
private bool _wasInRange = false;
2021-10-16 19:51:37 +00:00
private static int[] s_weaponSlotsOrdered = new[]
{
WeaponSlot.Machine,
WeaponSlot.Submachine,
WeaponSlot.Rifle,
WeaponSlot.Heavy,
WeaponSlot.Shotgun,
WeaponSlot.Pistol,
};
2021-09-19 20:17:46 +00:00
2021-09-19 22:44:31 +00:00
public override void OnBecameActive()
{
base.OnBecameActive();
2021-10-10 17:45:34 +00:00
_wasInRange = false;
2021-09-19 22:44:31 +00:00
this.TargetPed = this.ParameterForEnteringState as Ped;
if (this.TargetPed != null)
_enemyPeds.AddIfNotPresent(this.TargetPed);
}
public override void UpdateState2Seconds()
{
2021-10-16 19:51:37 +00:00
this.ChooseBestWeapon();
if (null == _ped.CurrentWeapon)
{
// we have no weapon to attack with, or no ammo
_pedAI.StartWalkingAround();
return;
}
if (null == this.TargetPed)
{
this.TargetPed = this.GetNextPedToAttack();
return;
}
if (this.IsInRange(this.TargetPed))
return; // current target is in range, continue attacking it
this.TargetPed = this.GetNextPedToAttack();
/*Ped nextPedToAttack = this.GetNextPedToAttack();
if (null == nextPedToAttack || nextPedToAttack == this.TargetPed)
return;
// check if we should switch to next target
Vector3 myPosition = _ped.transform.position;
float currentDistance = Vector3.Distance(this.TargetPed.transform.position, myPosition);
float nextDistance = Vector3.Distance(nextPedToAttack.transform.position, myPosition);
if (currentDistance - nextDistance > 6f)
{
// next target is closer by some delta value - switch to it
this.TargetPed = nextPedToAttack;
return;
}*/
}
2021-09-19 20:17:46 +00:00
public override void UpdateState()
{
if (null == this.TargetPed)
this.TargetPed = this.GetNextPedToAttack();
if (null == this.TargetPed)
{
// we finished attacking all enemies, now start walking
_pedAI.StartWalkingAround();
return;
}
2021-10-16 18:55:58 +00:00
if (_ped.IsInVehicle)
{
_ped.OnSubmitPressed();
return;
}
2021-10-10 17:45:34 +00:00
this.UpdateAttackOnPed(this.TargetPed, ref _wasInRange);
2021-09-19 20:17:46 +00:00
}
public Ped GetNextPedToAttack()
{
_enemyPeds.RemoveDeadObjectsIfNotEmpty();
if (_enemyPeds.Count == 0)
return null;
Vector3 myPosition = _ped.transform.position;
2021-10-16 17:05:44 +00:00
Ped closestPed = _enemyPeds.MinBy(p => Vector3.Distance(p.transform.position, myPosition), null);
2021-09-19 20:17:46 +00:00
return closestPed;
}
2021-10-10 19:29:52 +00:00
public bool IsInRange(Ped ped)
{
return Vector3.Distance(ped.transform.position, _ped.transform.position) < 10f;
}
2021-10-10 17:45:34 +00:00
public void UpdateAttackOnPed(Ped ped, ref bool wasInRange)
2021-09-19 20:17:46 +00:00
{
//var weapon = this.MyPed.CurrentWeapon;
Vector3 myHeadPos = GetHeadOrTransform(this.MyPed).position;
Vector3 targetHeadPos = GetHeadOrTransform(ped).position;
Vector3 firePos = this.MyPed.IsAiming ? this.MyPed.FirePosition : myHeadPos;
Vector3 diff = targetHeadPos - myHeadPos;
Vector3 dir = diff.normalized;
this.MyPed.Heading = dir;
Vector3 aimDir = (targetHeadPos - firePos).normalized;
if (this.MyPed.IsInVehicle)
{
if (diff.magnitude < 10f)
{
this.MyPed.AimDirection = aimDir;
if (!this.MyPed.IsAiming)
this.MyPed.OnAimButtonPressed();
this.MyPed.IsFireOn = true;
}
else
{
if (this.MyPed.IsAiming)
this.MyPed.OnAimButtonPressed();
}
}
else
{
2021-10-10 17:45:34 +00:00
float rangeRequired = wasInRange ? 10f : 8f;
wasInRange = false;
if (diff.magnitude < rangeRequired)
2021-09-19 20:17:46 +00:00
{
2021-10-10 17:45:34 +00:00
wasInRange = true;
2021-09-19 20:17:46 +00:00
this.MyPed.AimDirection = aimDir;
this.MyPed.IsAimOn = true;
this.MyPed.IsFireOn = true;
}
else if (Vector2.Distance(ped.transform.position.ToVec2WithXAndZ(), this.MyPed.transform.position.ToVec2WithXAndZ()) > 3f)
{
this.MyPed.IsRunOn = true;
this.MyPed.Movement = dir;
}
}
}
private static Transform GetHeadOrTransform(Ped ped)
{
return ped.PlayerModel.Head != null ? ped.PlayerModel.Head : ped.transform;
}
2021-10-16 19:51:37 +00:00
public void ChooseBestWeapon()
2021-10-17 16:35:03 +00:00
{
_ped.WeaponHolder.SwitchWeapon(this.GetBestWeaponSlot());
}
public int GetBestWeaponSlot()
2021-10-16 19:51:37 +00:00
{
for (int i = 0; i < s_weaponSlotsOrdered.Length; i++)
{
int slot = s_weaponSlotsOrdered[i];
Weapon weapon = _ped.WeaponHolder.GetWeaponAtSlot(slot);
if (weapon != null && weapon.TotalAmmo > 0)
2021-10-17 16:35:03 +00:00
return slot;
2021-10-16 19:51:37 +00:00
}
2021-10-16 22:57:11 +00:00
2021-10-17 16:35:03 +00:00
return WeaponSlot.Hand;
2021-10-16 19:51:37 +00:00
}
public bool CanStartChasing()
{
int slot = this.GetBestWeaponSlot();
return slot != WeaponSlot.Hand;
}
2021-09-19 20:17:46 +00:00
}
}