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-10-17 22:57:55 +00:00
|
|
|
public float aimDirAngularSpeed = 90f;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-10-16 18:25:37 +00:00
|
|
|
public override void UpdateState2Seconds()
|
|
|
|
{
|
2021-10-16 19:51:37 +00:00
|
|
|
this.ChooseBestWeapon();
|
|
|
|
|
2021-10-16 20:06:54 +00:00
|
|
|
if (null == _ped.CurrentWeapon)
|
|
|
|
{
|
|
|
|
// we have no weapon to attack with, or no ammo
|
|
|
|
_pedAI.StartWalkingAround();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-16 18:25:37 +00:00
|
|
|
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;
|
|
|
|
|
2021-10-17 22:57:55 +00:00
|
|
|
if (diff.ToVec2WithXAndZ().magnitude < 2.5f)
|
|
|
|
aimDir = (ped.transform.position - _ped.transform.position).normalized;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Vector3 currentAimDir = _ped.AimDirection;
|
|
|
|
Vector3 smoothFromDir = Vector3.Angle(aimDir, currentAimDir) < Vector3.Angle(aimDir, dir) ? currentAimDir : dir;
|
|
|
|
aimDir = SmoothOutAimDir(aimDir, smoothFromDir, this.aimDirAngularSpeed * Time.deltaTime);
|
|
|
|
}
|
|
|
|
|
2021-09-19 20:17:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 22:57:55 +00:00
|
|
|
private static Vector3 SmoothOutAimDir(Vector3 desiredAimDir, Vector3 currentAimDir, float maxDegreesDelta)
|
|
|
|
{
|
|
|
|
return Quaternion.RotateTowards(
|
|
|
|
Quaternion.LookRotation(currentAimDir),
|
|
|
|
Quaternion.LookRotation(desiredAimDir),
|
|
|
|
maxDegreesDelta)
|
|
|
|
.TransformDirection(Vector3.forward);
|
|
|
|
}
|
|
|
|
|
2021-09-19 20:17:46 +00:00
|
|
|
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
|
|
|
}
|
2021-10-17 16:36:30 +00:00
|
|
|
|
|
|
|
public bool CanStartChasing()
|
|
|
|
{
|
|
|
|
int slot = this.GetBestWeaponSlot();
|
|
|
|
return slot != WeaponSlot.Hand;
|
|
|
|
}
|
2021-09-19 20:17:46 +00:00
|
|
|
}
|
|
|
|
}
|