mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-27 14:30:17 +00:00
93 lines
No EOL
2.9 KiB
C#
93 lines
No EOL
2.9 KiB
C#
using System.Linq;
|
|
using SanAndreasUnity.Utilities;
|
|
using UnityEngine;
|
|
|
|
namespace SanAndreasUnity.Behaviours.Peds.AI
|
|
{
|
|
public class ChaseState : BaseState
|
|
{
|
|
public Ped TargetPed { get; private set; }
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
this.UpdateAttackOnPed(this.TargetPed);
|
|
}
|
|
|
|
public Ped GetNextPedToAttack()
|
|
{
|
|
_enemyPeds.RemoveDeadObjectsIfNotEmpty();
|
|
if (_enemyPeds.Count == 0)
|
|
return null;
|
|
|
|
Vector3 myPosition = _ped.transform.position;
|
|
|
|
var closestPed = _enemyPeds.Aggregate((p1, p2) =>
|
|
Vector3.Distance(p1.transform.position, myPosition)
|
|
< Vector3.Distance(p2.transform.position, myPosition)
|
|
? p1
|
|
: p2);
|
|
|
|
return closestPed;
|
|
}
|
|
|
|
public void UpdateAttackOnPed(Ped ped)
|
|
{
|
|
//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
|
|
{
|
|
if (diff.magnitude < 10f)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
} |