escape state done

This commit is contained in:
in0finite 2021-09-19 22:33:51 +02:00
parent b673942f1e
commit 8adfc87913
4 changed files with 51 additions and 26 deletions

View file

@ -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;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 338dada8543a5844a874473543f40680
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -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()

View file

@ -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;
}