Sfoglia il codice sorgente

Bug fixed; Documentation added

Furkan Karakocaoglu 2 anni fa
parent
commit
e0081dd89d

+ 25 - 18
testumgebung/CrowdModelling/Assets/Depictions_Years/Scripts/WanderingAI/WanderingAI.cs

@@ -16,31 +16,33 @@ public class WanderingAI : MonoBehaviour
     public float leaveTimer = 120f;
     public Vector3 leavePosition;
 
-    // Leave Settings
+    // Settings Leave Market
     private float offsetx = 0;
     private float offsetz = 0;
     private int inRow = 4; // incl 0 - 4
-
-    private float[] timer;
-    private float globalTimer;
     private bool leaveMarket = false;
+
+    // Settings Humans
     private Vector3[][] target;
     private int[][] humansPrio;
 
-    // Settings WP and Waiting time
+    // Settings Waypoints and Waiting Time
     private int maxWPCount;
     private List<List<int>> waitingTimer;
     private List<int> waitingList;
     private float[] currentWait;
     private int countFilling = 0;
 
+    // Time
+    private float[] timer;
+    private float globalTimer;
+
     private GameObject[][] humansGO;
     private NavMeshAgent[][] humansNMA;
     private Animator[][] humansA;
 
     private const string isWalking = "isWalking";
 
-    // Use this for initialization
     void Start()
     {
         // Get initialized Variables from InstantiatePrefab
@@ -48,19 +50,20 @@ public class WanderingAI : MonoBehaviour
         humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
         humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
         wanderTimer = gameObject.GetComponent<InstantiatePrefab>().wanderTimer;
-        //waitingTimer = gameObject.GetComponent<InstantiatePrefab>().waitingTimer;
         waypoints = gameObject.GetComponent<InstantiatePrefab>().waypointsArray;
         humansPrio = gameObject.GetComponent<InstantiatePrefab>().humanPriorities;
         // Set length of variables to humanGo.Length
         target = new Vector3[humansGO.Length][];
         timer = new float[humansGO.Length];
 
+        // Create copies
         waypointsList = new List<Transform>(waypoints);
-        maxWPCount = waypointsList.Count;
         waitingTimer = new List<List<int>>(gameObject.GetComponent<InstantiatePrefab>().waitingTimer);
         waitingList = new List<int>(waitingTimer[0]);
         currentWait = new float[humansGO.Length];
 
+        maxWPCount = waypointsList.Count;
+
         // Initialize size and content
         for (int i = 0; i < humansGO.Length; i++)
         {
@@ -73,46 +76,50 @@ public class WanderingAI : MonoBehaviour
 
     private void FixedUpdate()
     {
+        // Leave Market if time is up
         if(globalTimer >= leaveTimer)
             leaveMarket = true;
 
         for (int i = 0; i < humansGO.Length; ++i)
         {
             if (waypoints.Length == 0) return;
-
+            // Set a new destination if the waiting time is over
             if (timer[i] >= currentWait[i])
             {
                 for (int j = 0; j < humansGO[i].Length; ++j)
                 {
+                    // Set new destination iff the human reached his destination or (only in first iteration) target vector is zero
                     if (target[i][j] == Vector3.zero ||
                         humansNMA[i][j].velocity.magnitude <= 0.01f ||
                         humansNMA[i][j].remainingDistance <= humansNMA[i][j].stoppingDistance ||
                         humansNMA[i][j].pathStatus == NavMeshPathStatus.PathComplete ||
                         !humansNMA[i][j].hasPath)
                     {
+                        // Random index for waypointsList
                         if (maxWPCount <= 0) maxWPCount = waypointsList.Count;
                         int currentWPIndex = Random.Range(0, maxWPCount);
                         target[i][j] = CheckTarget(waypointsList[currentWPIndex].transform.position, targetScattering);
-
+                        // Random index for waitingTimer
                         int currentWaitingIndex = Random.Range(0, waitingTimer[currentWPIndex].Count);
                         currentWait[i] = waitingTimer[currentWPIndex][currentWaitingIndex];
                         RemoveIndexFromWaitingTimer(currentWPIndex, currentWaitingIndex);
-                        // fill waiting Timer, bc preparation for next iteration
-                        if (waitingTimer[i].Count <= 0)
+                        // Fill waiting timer with default, if it is empty, to get a new waiting time for the next iteration
+                        if (waitingTimer[currentWPIndex].Count <= 0)
                         {
-                            waitingTimer[i] = new List<int>(waitingList);
-                            countFilling++;
+                            waitingTimer[currentWPIndex] = new List<int>(waitingList);
+                            countFilling++; // to check if all waitingTimer entries are processed
                         }
 
+                        // Set destination and animation
                         if (target[i][0].x != float.PositiveInfinity)
                         {
                             if (humansNMA[i][j].isActiveAndEnabled)
                                 humansNMA[i][j].SetDestination(target[i][j]);
 
                             humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
-
                         }
-                        // remove from list to ensure that all WP are visited at the frequency indicated; if list empty fill list with initial WPs
+                        // Append the current WP and the waiting time from both lists to the end to ensure that all WPs and waiting timers are visited at the frequency indicated,
+                        // afterwords decrement the imaginated max length by 1
                         AppendIndexToEndWP(currentWPIndex);
                         AppendIndexToEndWT(currentWPIndex);
                         maxWPCount--;
@@ -120,18 +127,18 @@ public class WanderingAI : MonoBehaviour
                     }
                 }
             }
+            // Wait until the waiting time is over
             else
             {
                 for (int j = 0; j < humansGO[i].Length; ++j)
                 {
+                    // Set walking state and priority; increment timer
                     humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
                     if (humansNMA[i][j].remainingDistance <= humansNMA[i][j].stoppingDistance)
                         timer[i] += Time.deltaTime;
                     // if humans are not walking, then set priority to 0 so that the other humans dont disturb them
                     if (humansNMA[i][j].velocity.magnitude <= 0.01f)
-                    {
                         humansNMA[i][j].avoidancePriority = 0;
-                    }
                     else
                         humansNMA[i][j].avoidancePriority = humansPrio[i][j];
                 }