adding ai fixes in #268

Merged
glencelow merged 3 commits from PlayerController into main 2022-11-24 01:05:35 +08:00
5 changed files with 76 additions and 21 deletions
Showing only changes of commit d8b2addd1f - Show all commits

View File

@ -40,7 +40,15 @@ namespace SHADE_Scripting.AIBehaviour.BehaviourTree
//awake and update functions //awake and update functions
//the only segment in the entire AI that is dependent on the engine //the only segment in the entire AI that is dependent on the engine
private bool test = false;
protected override void awake() protected override void awake()
{
AwakeCall();
}
protected override void start()
{ {
_root = CreateTree(); _root = CreateTree();
_root.InitialiseNode(this); _root.InitialiseNode(this);
@ -51,7 +59,7 @@ namespace SHADE_Scripting.AIBehaviour.BehaviourTree
_root?.Evaluate(); _root?.Evaluate();
Tick(); Tick();
} }
protected abstract void AwakeCall();
protected abstract void Initialise(); protected abstract void Initialise();
protected abstract void Tick(); protected abstract void Tick();
} }

View File

@ -28,9 +28,8 @@ public partial class Homeowner1 : BehaviourTree
private BehaviourTreeEvents _events { get; set; } private BehaviourTreeEvents _events { get; set; }
public override BehaviourTreeEvents events { get => _events; } public override BehaviourTreeEvents events { get => _events; }
[SerializeField]
[Tooltip("The player the AI should chase and attempt to capture")] [Tooltip("The player the AI should chase and attempt to capture")]
private GameObject player; public GameObject player;
//PATROL FIELDS/////////////////////////////////////////////////////////////// //PATROL FIELDS///////////////////////////////////////////////////////////////
@ -80,6 +79,27 @@ public partial class Homeowner1 : BehaviourTree
//AI tree //AI tree
public partial class Homeowner1 : BehaviourTree public partial class Homeowner1 : BehaviourTree
{ {
Transform _thisTransform = null;
RigidBody _thisRigidbody = null;
GameObject _playerObject;
private LeafPatrol leafPatrol;
protected override void AwakeCall()
{
_thisTransform = GetComponent<Transform>();
if (!_thisTransform)
Debug.LogError("EMPTY TRANSFORM");
_thisRigidbody = GetComponent<RigidBody>();
if (!_thisRigidbody)
Debug.LogError("EMPTY RIGIDBODY");
if (!player)
Debug.Log("PLAYER MISSING!");
//_playerObject = GameObject.Find("Player").GetValueOrDefault();
}
//Called at the start //Called at the start
protected override void Initialise() protected override void Initialise()
{ {
@ -93,6 +113,7 @@ public partial class Homeowner1 : BehaviourTree
events.Tick(); events.Tick();
float velocity = GetComponent<RigidBody>().LinearVelocity.GetMagnitude(); float velocity = GetComponent<RigidBody>().LinearVelocity.GetMagnitude();
leafPatrol.waypoints = waypoints;
footstepTimeRemaining -= velocity * Time.DeltaTimeF; footstepTimeRemaining -= velocity * Time.DeltaTimeF;
if (footstepTimeRemaining < 0.0f) if (footstepTimeRemaining < 0.0f)
@ -107,19 +128,20 @@ public partial class Homeowner1 : BehaviourTree
//The tree is called from the root every tick //The tree is called from the root every tick
protected override BehaviourTreeNode CreateTree() protected override BehaviourTreeNode CreateTree()
{ {
leafPatrol = new LeafPatrol("Patrol", _thisTransform, waypoints, patrolSpeed, turningSpeed, _thisRigidbody);
//Start from the root, structure it like this to make it look like a tree //Start from the root, structure it like this to make it look like a tree
BehaviourTreeNode root = new BehaviourTreeSelector("Root", new List<BehaviourTreeNode> BehaviourTreeNode root = new BehaviourTreeSelector("Root", new List<BehaviourTreeNode>
{ {
new BehaviourTreeSequence("Alerted", new List<BehaviourTreeNode> /* new BehaviourTreeSequence("Alerted", new List<BehaviourTreeNode>
{ {
new LeafSearch("SearchFOV", player, GetComponent<Transform>(), eyeOffset, sightDistance), new LeafSearch("SearchFOV", _thisTransform, eyeOffset, sightDistance),
new BehaviourTreeSequence("CatchPlayer", new List<BehaviourTreeNode> new BehaviourTreeSequence("CatchPlayer", new List<BehaviourTreeNode>
{ {
new LeafChase("Chasing", GetComponent<Transform>(), GetComponent<RigidBody>(), chaseSpeed, turningSpeed, distanceToCapture, captureTime), new LeafChase("Chasing", _thisTransform, _thisRigidbody, chaseSpeed, turningSpeed, distanceToCapture, captureTime),
new LeafAttack("Attacking", GameObject.Find("Player").GetValueOrDefault()) new LeafAttack("Attacking")
}) })
}), }),*/
new LeafPatrol("Patrol", GetComponent<Transform>(), waypoints, patrolSpeed, turningSpeed, GetComponent<RigidBody>()) leafPatrol
}); });
return root; return root;

View File

@ -28,13 +28,22 @@ public partial class LeafAttack : BehaviourTreeNode
//FUNCTIONS //FUNCTIONS
public partial class LeafAttack : BehaviourTreeNode public partial class LeafAttack : BehaviourTreeNode
{ {
public LeafAttack(string name, GameObject p) : base (name) public LeafAttack(string name) : base (name)
{ {
player = p; //player = p;
} }
public override BehaviourTreeNodeStatus Evaluate() public override BehaviourTreeNodeStatus Evaluate()
{ {
{
if (!player)
{
player = GameObject.Find("Player").GetValueOrDefault();
Debug.Log("HERE2");
if (!player) { return BehaviourTreeNodeStatus.FAILURE; }
}
}
//Debug.LogWarning("LeafAttack"); //Debug.LogWarning("LeafAttack");
//Fail if no target in blackboard? //Fail if no target in blackboard?

View File

@ -22,10 +22,10 @@ public partial class LeafPatrol : BehaviourTreeNode
{ {
//Waypoints and movement //Waypoints and movement
private Transform transform; private Transform transform;
private List<Vector3> waypoints; public List<Vector3> waypoints;
private RigidBody rb; private RigidBody rb;
private float patrolSpeed; private float patrolSpeed = 1.0f;
private float turningSpeed; private float turningSpeed = 5.0f;
private float retreatTimer = 0.0f; private float retreatTimer = 0.0f;
private int currentWaypointIndex = 0; private int currentWaypointIndex = 0;
private bool retreatState = false; private bool retreatState = false;
@ -79,12 +79,19 @@ public partial class LeafPatrol : BehaviourTreeNode
ClearNodeData("isWaiting"); ClearNodeData("isWaiting");
return; return;
} }
Vector3 remainingDistance = Vector3.Zero;
Debug.Log($"{waypoints.Count}");
if (currentWaypointIndex > 0)
{
Vector3 targetPosition = waypoints[currentWaypointIndex]; Vector3 targetPosition = waypoints[currentWaypointIndex];
//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; remainingDistance = targetPosition - transform.GlobalPosition;
remainingDistance.y = 0.0f; remainingDistance.y = 0.0f;
}
//Reached waypoint, cycle //Reached waypoint, cycle
if (remainingDistance.GetSqrMagnitude() < 0.1f) if (remainingDistance.GetSqrMagnitude() < 0.1f)

View File

@ -31,9 +31,10 @@ public partial class LeafSearch : BehaviourTreeNode
//FUNCTIONS HERE //FUNCTIONS HERE
public partial class LeafSearch : BehaviourTreeNode public partial class LeafSearch : BehaviourTreeNode
{ {
public LeafSearch(string name, GameObject p, Transform t, Vector3 eo, float sDist) : base(name) public LeafSearch(string name, Transform t, Vector3 eo, float sDist) : base(name)
{ {
player = p; Debug.Log($"===============================PLAYER: {t}");
// player = p;
transform = t; transform = t;
eyeOffset = eo; eyeOffset = eo;
sightDistance = sDist; sightDistance = sDist;
@ -41,6 +42,14 @@ public partial class LeafSearch : BehaviourTreeNode
public override BehaviourTreeNodeStatus Evaluate() public override BehaviourTreeNodeStatus Evaluate()
{ {
{
if (!player)
{
player = GameObject.Find("Player").GetValueOrDefault();
if (!player) { Debug.Log("HERE1"); return BehaviourTreeNodeStatus.FAILURE; }
}
}
//Debug.LogWarning("LeafSearch"); //Debug.LogWarning("LeafSearch");
onEnter(BehaviourTreeNodeStatus.RUNNING); onEnter(BehaviourTreeNodeStatus.RUNNING);