AI gets sent to a waypoint of choice when caught

This commit is contained in:
mushgunAX 2023-03-04 04:57:59 +08:00
parent d7d4e21005
commit dc54ae92a2
5 changed files with 97 additions and 17 deletions

View File

@ -3477,6 +3477,7 @@
Text: My name is Brandon. Text: My name is Brandon.
Font: 174412429 Font: 174412429
Color: {x: 1, y: 1, z: 1, w: 1} Color: {x: 1, y: 1, z: 1, w: 1}
Text Size: {x: 1, y: 1, z: 1}
IsActive: true IsActive: true
UI Component: UI Component:
Canvas ID: 199 Canvas ID: 199
@ -3518,6 +3519,7 @@
Text: My name is Brandon. Text: My name is Brandon.
Font: 176667660 Font: 176667660
Color: {x: 1, y: 1, z: 1, w: 1} Color: {x: 1, y: 1, z: 1, w: 1}
Text Size: {x: 1, y: 1, z: 1}
IsActive: true IsActive: true
UI Component: UI Component:
Canvas ID: 199 Canvas ID: 199
@ -3539,6 +3541,7 @@
Text: X2 Text: X2
Font: 174412429 Font: 174412429
Color: {x: 1, y: 1, z: 1, w: 1} Color: {x: 1, y: 1, z: 1, w: 1}
Text Size: {x: 1, y: 1, z: 1}
IsActive: true IsActive: true
UI Component: UI Component:
Canvas ID: 199 Canvas ID: 199
@ -5855,7 +5858,7 @@
Components: ~ Components: ~
Scripts: ~ Scripts: ~
- EID: 165 - EID: 165
Name: 1 Name: 0
IsActive: true IsActive: true
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
@ -5866,7 +5869,7 @@
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 164 - EID: 164
Name: 2 Name: 1
IsActive: true IsActive: true
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
@ -5877,7 +5880,7 @@
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 163 - EID: 163
Name: 3 Name: 2
IsActive: true IsActive: true
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
@ -5888,7 +5891,7 @@
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 162 - EID: 162
Name: 4 Name: 3
IsActive: true IsActive: true
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
@ -5899,7 +5902,7 @@
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 161 - EID: 161
Name: 5 Name: 4
IsActive: true IsActive: true
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
@ -5910,7 +5913,7 @@
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 160 - EID: 160
Name: 6 Name: 5
IsActive: true IsActive: true
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
@ -5921,7 +5924,7 @@
IsActive: true IsActive: true
Scripts: ~ Scripts: ~
- EID: 159 - EID: 159
Name: 7 Name: 6
IsActive: true IsActive: true
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
@ -5937,7 +5940,7 @@
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
Transform Component: Transform Component:
Translate: {x: 0.0576689839, y: 0, z: -2.61272359} Translate: {x: -3.45969725, y: 0, z: -2.61272359}
Rotate: {x: -0, y: 0, z: -0} Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 1, y: 1, z: 1} Scale: {x: 1, y: 1, z: 1}
IsActive: true IsActive: true
@ -5975,12 +5978,13 @@
- Type: Homeowner1 - Type: Homeowner1
Enabled: true Enabled: true
waypointsPool: 166 waypointsPool: 166
startWaypoint: 162
patrolSpeed: 3 patrolSpeed: 3
chaseSpeed: 6 chaseSpeed: 6
turningSpeed: 5 turningSpeed: 5
sightDistance: 8 sightDistance: 8
eyeOffset: [0, 1.64999998, 0] eyeOffset: [0, 1.64999998, 0]
distanceToCapture: 0.5 distanceToCapture: 0.800000012
captureTime: 0.5 captureTime: 0.5
footstepSFXIntervalMultiplier: 0.5 footstepSFXIntervalMultiplier: 0.5
- EID: 12 - EID: 12
@ -6110,7 +6114,7 @@
Components: Components:
Transform Component: Transform Component:
Translate: {x: 0, y: -300, z: 0} Translate: {x: 0, y: -300, z: 0}
Rotate: {x: -4.5, y: 2, z: -2.5} Rotate: {x: 3.25, y: 0.5, z: 4.5}
Scale: {x: 400, y: 100, z: 500} Scale: {x: 400, y: 100, z: 500}
IsActive: true IsActive: true
Renderable Component: Renderable Component:
@ -6183,6 +6187,7 @@
Text: Game Pause Text: Game Pause
Font: 176667660 Font: 176667660
Color: {x: 1, y: 1, z: 1, w: 1} Color: {x: 1, y: 1, z: 1, w: 1}
Text Size: {x: 1, y: 1, z: 1}
IsActive: false IsActive: false
UI Component: UI Component:
Canvas ID: 10 Canvas ID: 10

View File

@ -37,6 +37,9 @@ public partial class Homeowner1 : BehaviourTree
private List<GameObject> waypoints; private List<GameObject> waypoints;
[SerializeField]
private GameObject startWaypoint;
[SerializeField] [SerializeField]
[Tooltip("The AI will patrol at this speed")] [Tooltip("The AI will patrol at this speed")]
private float patrolSpeed; private float patrolSpeed;
@ -151,6 +154,8 @@ public partial class Homeowner1 : BehaviourTree
SetData("distanceToCapture", distanceToCapture); SetData("distanceToCapture", distanceToCapture);
if (GetData("baseCaptureTime") == null || (float)GetData("baseCaptureTime") != captureTime) if (GetData("baseCaptureTime") == null || (float)GetData("baseCaptureTime") != captureTime)
SetData("baseCaptureTime", captureTime); SetData("baseCaptureTime", captureTime);
if (GetData("startWaypoint") == null || (GameObject)GetData("startWaypoint") != startWaypoint)
SetData("startWaypoint", startWaypoint);
events.Tick(); events.Tick();

View File

@ -12,6 +12,7 @@
using SHADE; using SHADE;
using SHADE_Scripting.AIBehaviour.BehaviourTree; using SHADE_Scripting.AIBehaviour.BehaviourTree;
using SHADE_Scripting.Audio;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -98,6 +99,35 @@ public partial class LeafAttack : BehaviourTreeNode
if (player.GetScript<PlayerController>().stateMachine && !player.GetScript<PlayerController>().stateMachine.IsState(typeof(PlayerCaughtState))) if (player.GetScript<PlayerController>().stateMachine && !player.GetScript<PlayerController>().stateMachine.IsState(typeof(PlayerCaughtState)))
player.GetScript<PlayerController>().stateMachine.SetState(typeof(PlayerCaughtState)); player.GetScript<PlayerController>().stateMachine.SetState(typeof(PlayerCaughtState));
//Teleport AI back to home waypoint
int homeIndex = 0;
GameObject? startWaypoint = null;
List<GameObject>? waypoints = (List<GameObject>)GetNodeData("waypoints");
if (GetNodeData("startWaypoint") != null)
startWaypoint = (GameObject)GetNodeData("startWaypoint");
if (startWaypoint != null)
{
for (int i = 0; i < waypoints.Count; ++i)
{
if (startWaypoint == waypoints[i])
{
homeIndex = i;
}
}
}
SetNodeData("currentWaypointIndex", homeIndex);
SetNodeData("playerLastSightedWaypointIndex", homeIndex);
SetNodeData("isAlert", false);
Audio.SetParameterWithLabel("PlayerDetection", "Undetected");
AudioHandler.audioClipHandlers["SFXHumming"].Play();
Transform? transform = (Transform)GetNodeData("transform");
if (waypoints != null && transform != null)
{
transform.GlobalPosition = waypoints[homeIndex].GetComponent<Transform>().GlobalPosition;
}
ClearNodeData("target");
status = BehaviourTreeNodeStatus.SUCCESS; status = BehaviourTreeNodeStatus.SUCCESS;
onExit(BehaviourTreeNodeStatus.SUCCESS); onExit(BehaviourTreeNodeStatus.SUCCESS);
return status; return status;

View File

@ -60,7 +60,7 @@ public partial class LeafChase : BehaviourTreeNode
} }
} }
//Debug.Log("Player is nearest " + nearestWaypointIndex); //Debug.Log("Player is nearest " + nearestWaypointIndex);
//Debug.Log("I'm at " + (int)GetNodeData("currentWaypointIndex")); //Debug.Log("AI going to " + (int)GetNodeData("currentWaypointIndex"));
SetNodeData("playerLastSightedWaypointIndex", nearestWaypointIndex); SetNodeData("playerLastSightedWaypointIndex", nearestWaypointIndex);
} }
@ -78,6 +78,7 @@ public partial class LeafChase : BehaviourTreeNode
GetNodeData("distanceToCapture") == null || GetNodeData("distanceToCapture") == null ||
GetNodeData("baseCaptureTime") == null) GetNodeData("baseCaptureTime") == null)
{ {
//Debug.Log("Chase Failure: null values");
status = BehaviourTreeNodeStatus.FAILURE; status = BehaviourTreeNodeStatus.FAILURE;
onExit(BehaviourTreeNodeStatus.FAILURE); onExit(BehaviourTreeNodeStatus.FAILURE);
return status; return status;

View File

@ -29,7 +29,7 @@ public partial class LeafPatrol : BehaviourTreeNode
private float chaseSpeed; private float chaseSpeed;
private float turningSpeed; private float turningSpeed;
private float retreatTimer = 0.0f; private float retreatTimer = 0.0f;
private int currentWaypointIndex = 0; private int currentWaypointIndex = -1;
private bool retreatState = false; private bool retreatState = false;
private bool goingForwards = true; private bool goingForwards = true;
@ -47,7 +47,7 @@ public partial class LeafPatrol : BehaviourTreeNode
//node, and hence we do not need to inherit its constructors //node, and hence we do not need to inherit its constructors
public LeafPatrol(string name) : base(name) public LeafPatrol(string name) : base(name)
{ {
currentWaypointIndex = 0; currentWaypointIndex = -1;
} }
//When it comes to evaluating, //When it comes to evaluating,
@ -57,6 +57,42 @@ public partial class LeafPatrol : BehaviourTreeNode
//Debug.LogWarning("LeafPatrol"); //Debug.LogWarning("LeafPatrol");
onEnter(BehaviourTreeNodeStatus.RUNNING); onEnter(BehaviourTreeNodeStatus.RUNNING);
//Initialise home waypoint
waypoints = (List<GameObject>)GetNodeData("waypoints");
if (currentWaypointIndex == -1)
{
if (waypoints != null)
{
//Debug.Log("Waypoints not null");
if (GetNodeData("startWaypoint") != null)
{
//Debug.Log("Getting start waypoint");
GameObject startWaypoint = (GameObject)(GetNodeData("startWaypoint"));
for (int i = 0; i < waypoints.Count; ++i)
{
if (startWaypoint == waypoints[i])
{
//Debug.Log("Leaf Patrol Constructor: Start Waypoint Index: " + i.ToString());
currentWaypointIndex = i;
SetNodeData("currentWaypointIndex", i);
}
}
}
else
{
status = BehaviourTreeNodeStatus.FAILURE;
onExit(BehaviourTreeNodeStatus.FAILURE);
return status;
}
}
else
{
status = BehaviourTreeNodeStatus.FAILURE;
onExit(BehaviourTreeNodeStatus.FAILURE);
return status;
}
}
//Get data //Get data
if (GetNodeData("transform") == null || if (GetNodeData("transform") == null ||
GetNodeData("patrolSpeed") == null || GetNodeData("patrolSpeed") == null ||
@ -104,12 +140,13 @@ public partial class LeafPatrol : BehaviourTreeNode
ClearNodeData("isWaiting"); ClearNodeData("isWaiting");
return; return;
} }
waypoints = (List<GameObject>)GetNodeData("waypoints"); if (GetNodeData("currentWaypointIndex") != null)
if (waypoints == null)
{ {
return; //2023 Mar 4, 0400, this is needed when the AI teleports back to the first spot after catching
currentWaypointIndex = (int)GetNodeData("currentWaypointIndex");
} }
Vector3 targetPosition = waypoints[currentWaypointIndex].GetComponent<Transform>().GlobalPosition; //Modulo operator to prevent out of range exceptions
Vector3 targetPosition = waypoints[(currentWaypointIndex % waypoints.Count)].GetComponent<Transform>().GlobalPosition;
//Reach waypoint by X and Z being near enough //Reach waypoint by X and Z being near enough
//Do not consider Y of waypoints yet //Do not consider Y of waypoints yet
Vector3 remainingDistance = targetPosition - transform.GlobalPosition; Vector3 remainingDistance = targetPosition - transform.GlobalPosition;
@ -195,6 +232,8 @@ public partial class LeafPatrol : BehaviourTreeNode
//Debug.Log("AI is at " + transform.GlobalPosition.x.ToString() + " " + transform.GlobalPosition.y.ToString() + " " + transform.GlobalPosition.z.ToString()); //Debug.Log("AI is at " + transform.GlobalPosition.x.ToString() + " " + transform.GlobalPosition.y.ToString() + " " + transform.GlobalPosition.z.ToString());
Vector3 normalisedDifference = targetPosition - transform.GlobalPosition; Vector3 normalisedDifference = targetPosition - transform.GlobalPosition;
normalisedDifference.y = 0.0f; //Do not move vertically normalisedDifference.y = 0.0f; //Do not move vertically
//Debug.Log("Leaf Patrol Current Waypoint Index: " + currentWaypointIndex.ToString());
//Debug.Log("True Difference x " + normalisedDifference.x.ToString() + " z " + normalisedDifference.z.ToString());
normalisedDifference /= normalisedDifference.GetMagnitude(); normalisedDifference /= normalisedDifference.GetMagnitude();
//Debug.Log("Normalised Difference x " + normalisedDifference.x.ToString() + " z " + normalisedDifference.z.ToString()); //Debug.Log("Normalised Difference x " + normalisedDifference.x.ToString() + " z " + normalisedDifference.z.ToString());