From d8b2addd1f31f7af7227a817cf88b21aafe596d7 Mon Sep 17 00:00:00 2001 From: Glence Date: Wed, 23 Nov 2022 22:44:15 +0800 Subject: [PATCH] added the fix for AI --- .../BehaviourTree/Core/BehaviourTree.cs | 10 ++++- .../AIBehaviour/Implemented/Homeowner1.cs | 38 +++++++++++++++---- .../Implemented/LeafNodes/LeafAttack.cs | 13 ++++++- .../Implemented/LeafNodes/LeafPatrol.cs | 23 +++++++---- .../Implemented/LeafNodes/LeafSearch.cs | 13 ++++++- 5 files changed, 76 insertions(+), 21 deletions(-) diff --git a/Assets/Scripts/AIBehaviour/BehaviourTree/Core/BehaviourTree.cs b/Assets/Scripts/AIBehaviour/BehaviourTree/Core/BehaviourTree.cs index 404e3df8..094f7e93 100644 --- a/Assets/Scripts/AIBehaviour/BehaviourTree/Core/BehaviourTree.cs +++ b/Assets/Scripts/AIBehaviour/BehaviourTree/Core/BehaviourTree.cs @@ -40,7 +40,15 @@ namespace SHADE_Scripting.AIBehaviour.BehaviourTree //awake and update functions //the only segment in the entire AI that is dependent on the engine + + private bool test = false; + protected override void awake() + { + AwakeCall(); + } + + protected override void start() { _root = CreateTree(); _root.InitialiseNode(this); @@ -51,7 +59,7 @@ namespace SHADE_Scripting.AIBehaviour.BehaviourTree _root?.Evaluate(); Tick(); } - + protected abstract void AwakeCall(); protected abstract void Initialise(); protected abstract void Tick(); } diff --git a/Assets/Scripts/AIBehaviour/Implemented/Homeowner1.cs b/Assets/Scripts/AIBehaviour/Implemented/Homeowner1.cs index 1742bbe8..b556ad38 100644 --- a/Assets/Scripts/AIBehaviour/Implemented/Homeowner1.cs +++ b/Assets/Scripts/AIBehaviour/Implemented/Homeowner1.cs @@ -28,9 +28,8 @@ public partial class Homeowner1 : BehaviourTree private BehaviourTreeEvents _events { get; set; } public override BehaviourTreeEvents events { get => _events; } - [SerializeField] [Tooltip("The player the AI should chase and attempt to capture")] - private GameObject player; + public GameObject player; //PATROL FIELDS/////////////////////////////////////////////////////////////// @@ -80,6 +79,27 @@ public partial class Homeowner1 : BehaviourTree //AI tree public partial class Homeowner1 : BehaviourTree { + Transform _thisTransform = null; + RigidBody _thisRigidbody = null; + GameObject _playerObject; + private LeafPatrol leafPatrol; + + protected override void AwakeCall() + { + _thisTransform = GetComponent(); + if (!_thisTransform) + Debug.LogError("EMPTY TRANSFORM"); + + _thisRigidbody = GetComponent(); + if (!_thisRigidbody) + Debug.LogError("EMPTY RIGIDBODY"); + + if (!player) + Debug.Log("PLAYER MISSING!"); + + //_playerObject = GameObject.Find("Player").GetValueOrDefault(); + } + //Called at the start protected override void Initialise() { @@ -93,6 +113,7 @@ public partial class Homeowner1 : BehaviourTree events.Tick(); float velocity = GetComponent().LinearVelocity.GetMagnitude(); + leafPatrol.waypoints = waypoints; footstepTimeRemaining -= velocity * Time.DeltaTimeF; if (footstepTimeRemaining < 0.0f) @@ -107,19 +128,20 @@ public partial class Homeowner1 : BehaviourTree //The tree is called from the root every tick 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 BehaviourTreeNode root = new BehaviourTreeSelector("Root", new List { - new BehaviourTreeSequence("Alerted", new List +/* new BehaviourTreeSequence("Alerted", new List { - new LeafSearch("SearchFOV", player, GetComponent(), eyeOffset, sightDistance), + new LeafSearch("SearchFOV", _thisTransform, eyeOffset, sightDistance), new BehaviourTreeSequence("CatchPlayer", new List { - new LeafChase("Chasing", GetComponent(), GetComponent(), chaseSpeed, turningSpeed, distanceToCapture, captureTime), - new LeafAttack("Attacking", GameObject.Find("Player").GetValueOrDefault()) + new LeafChase("Chasing", _thisTransform, _thisRigidbody, chaseSpeed, turningSpeed, distanceToCapture, captureTime), + new LeafAttack("Attacking") }) - }), - new LeafPatrol("Patrol", GetComponent(), waypoints, patrolSpeed, turningSpeed, GetComponent()) + }),*/ + leafPatrol }); return root; diff --git a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs index e64b63ad..2a08e764 100644 --- a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs +++ b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs @@ -28,13 +28,22 @@ public partial class LeafAttack : BehaviourTreeNode //FUNCTIONS 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() { + { + if (!player) + { + player = GameObject.Find("Player").GetValueOrDefault(); + Debug.Log("HERE2"); + if (!player) { return BehaviourTreeNodeStatus.FAILURE; } + } + } + //Debug.LogWarning("LeafAttack"); //Fail if no target in blackboard? diff --git a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs index a85faafc..1e68a7f2 100644 --- a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs +++ b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs @@ -22,10 +22,10 @@ public partial class LeafPatrol : BehaviourTreeNode { //Waypoints and movement private Transform transform; - private List waypoints; + public List waypoints; private RigidBody rb; - private float patrolSpeed; - private float turningSpeed; + private float patrolSpeed = 1.0f; + private float turningSpeed = 5.0f; private float retreatTimer = 0.0f; private int currentWaypointIndex = 0; private bool retreatState = false; @@ -79,12 +79,19 @@ public partial class LeafPatrol : BehaviourTreeNode ClearNodeData("isWaiting"); return; } - Vector3 targetPosition = waypoints[currentWaypointIndex]; - //Reach waypoint by X and Z being near enough - //Do not consider Y of waypoints yet - Vector3 remainingDistance = targetPosition - transform.GlobalPosition; - remainingDistance.y = 0.0f; + Vector3 remainingDistance = Vector3.Zero; + Debug.Log($"{waypoints.Count}"); + if (currentWaypointIndex > 0) + { + + Vector3 targetPosition = waypoints[currentWaypointIndex]; + + //Reach waypoint by X and Z being near enough + //Do not consider Y of waypoints yet + remainingDistance = targetPosition - transform.GlobalPosition; + remainingDistance.y = 0.0f; + } //Reached waypoint, cycle if (remainingDistance.GetSqrMagnitude() < 0.1f) diff --git a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafSearch.cs b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafSearch.cs index a82c9462..01c7bd66 100644 --- a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafSearch.cs +++ b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafSearch.cs @@ -31,9 +31,10 @@ public partial class LeafSearch : BehaviourTreeNode //FUNCTIONS HERE 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; eyeOffset = eo; sightDistance = sDist; @@ -41,6 +42,14 @@ public partial class LeafSearch : BehaviourTreeNode public override BehaviourTreeNodeStatus Evaluate() { + { + if (!player) + { + player = GameObject.Find("Player").GetValueOrDefault(); + if (!player) { Debug.Log("HERE1"); return BehaviourTreeNodeStatus.FAILURE; } + } + } + //Debug.LogWarning("LeafSearch"); onEnter(BehaviourTreeNodeStatus.RUNNING);