diff --git a/Assets/Scripts/Behaviours/PedAI/EscapeState.cs b/Assets/Scripts/Behaviours/PedAI/EscapeState.cs new file mode 100644 index 00000000..f899f083 --- /dev/null +++ b/Assets/Scripts/Behaviours/PedAI/EscapeState.cs @@ -0,0 +1,28 @@ +using UnityEngine; + +namespace SanAndreasUnity.Behaviours.Peds.AI +{ + public class EscapeState : BaseState + { + private readonly PathMovementData _pathMovementData = new PathMovementData(); + + + public override void UpdateState() + { + // TODO: exit vehicle + + if (PedAI.ArrivedAtDestinationNode(_pathMovementData, _ped.transform)) + PedAI.OnArrivedToDestinationNode(_pathMovementData); + + if (!_pathMovementData.destinationNode.HasValue) + { + PedAI.FindClosestWalkableNode(_pathMovementData, _ped.transform.position); + return; + } + + this.MyPed.IsSprintOn = true; + this.MyPed.Movement = (_pathMovementData.moveDestination - this.MyPed.transform.position).normalized; + this.MyPed.Heading = this.MyPed.Movement; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Behaviours/PedAI/EscapeState.cs.meta b/Assets/Scripts/Behaviours/PedAI/EscapeState.cs.meta new file mode 100644 index 00000000..c94bc9d4 --- /dev/null +++ b/Assets/Scripts/Behaviours/PedAI/EscapeState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 338dada8543a5844a874473543f40680 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Behaviours/PedAI/PedAI.cs b/Assets/Scripts/Behaviours/PedAI/PedAI.cs index 87eb1b79..e726c830 100644 --- a/Assets/Scripts/Behaviours/PedAI/PedAI.cs +++ b/Assets/Scripts/Behaviours/PedAI/PedAI.cs @@ -290,22 +290,19 @@ namespace SanAndreasUnity.Behaviours.Peds.AI return targetNode.Position + offset.ToVector3XZ(); } - void UpdateEscaping() + public static void FindClosestWalkableNode(PathMovementData pathMovementData, Vector3 position) { - // TODO: exit vehicle - - if (this.ArrivedAtDestinationNode()) - this.OnArrivedToDestinationNode(); - - if (!this.HasTargetNode) - { - this.FindNextNodeDelayed(); + if (Time.time - pathMovementData.timeWhenAttemptedToFindClosestNode < 2f) // don't attempt to find it every frame return; - } - this.MyPed.IsSprintOn = true; - this.MyPed.Movement = (_moveDestination - this.MyPed.transform.position).normalized; - this.MyPed.Heading = this.MyPed.Movement; + pathMovementData.timeWhenAttemptedToFindClosestNode = Time.time; + + var closestPathNodeToWalk = PedAI.GetClosestPathNodeToWalk(position); + if (null == closestPathNodeToWalk) + return; + + pathMovementData.destinationNode = closestPathNodeToWalk; + pathMovementData.moveDestination = PedAI.GetMoveDestinationBasedOnTargetNode(closestPathNodeToWalk.Value); } void UpdateFollowing() diff --git a/Assets/Scripts/Behaviours/PedAI/WalkAroundState.cs b/Assets/Scripts/Behaviours/PedAI/WalkAroundState.cs index 2989d1d7..fd166daa 100644 --- a/Assets/Scripts/Behaviours/PedAI/WalkAroundState.cs +++ b/Assets/Scripts/Behaviours/PedAI/WalkAroundState.cs @@ -9,12 +9,12 @@ namespace SanAndreasUnity.Behaviours.Peds.AI public PathNode? currentNode; public PathNode? destinationNode; public Vector3 moveDestination; + public float timeWhenAttemptedToFindClosestNode = 0f; } public class WalkAroundState : BaseState { private readonly PathMovementData _pathMovementData = new PathMovementData(); - private float _timeWhenAttemptedToFindClosestNode = 0f; public override void UpdateState() @@ -42,18 +42,7 @@ namespace SanAndreasUnity.Behaviours.Peds.AI if (!_pathMovementData.destinationNode.HasValue) { - if (Time.time - _timeWhenAttemptedToFindClosestNode > 2f) // don't attempt to find it every frame - { - _timeWhenAttemptedToFindClosestNode = Time.time; - - var closestPathNodeToWalk = PedAI.GetClosestPathNodeToWalk(_ped.transform.position); - if (null == closestPathNodeToWalk) - return; - - _pathMovementData.destinationNode = closestPathNodeToWalk; - _pathMovementData.moveDestination = PedAI.GetMoveDestinationBasedOnTargetNode(closestPathNodeToWalk.Value); - } - + PedAI.FindClosestWalkableNode(_pathMovementData, _ped.transform.position); return; }