|
@@ -10,7 +10,6 @@ public class WanderingAI : MonoBehaviour
|
|
public Transform[] waypoints;
|
|
public Transform[] waypoints;
|
|
public List<Transform> waypointsList;
|
|
public List<Transform> waypointsList;
|
|
public float[] wanderTimer;
|
|
public float[] wanderTimer;
|
|
- public List<float>[] waitingTimer;
|
|
|
|
[Header("Scattering around the target")]
|
|
[Header("Scattering around the target")]
|
|
public float targetScattering = 5.0f; //Streuung
|
|
public float targetScattering = 5.0f; //Streuung
|
|
[Header("Leave Settings")]
|
|
[Header("Leave Settings")]
|
|
@@ -24,9 +23,17 @@ public class WanderingAI : MonoBehaviour
|
|
|
|
|
|
private float[] timer;
|
|
private float[] timer;
|
|
private float globalTimer;
|
|
private float globalTimer;
|
|
|
|
+ private bool leaveMarket = false;
|
|
private Vector3[][] target;
|
|
private Vector3[][] target;
|
|
private int[][] humansPrio;
|
|
private int[][] humansPrio;
|
|
|
|
|
|
|
|
+ // Settings WP and Waiting time
|
|
|
|
+ private int maxWPCount;
|
|
|
|
+ private List<List<int>> waitingTimer;
|
|
|
|
+ private List<int> waitingList;
|
|
|
|
+ private float[] currentWait;
|
|
|
|
+ private int countFilling = 0;
|
|
|
|
+
|
|
private GameObject[][] humansGO;
|
|
private GameObject[][] humansGO;
|
|
private NavMeshAgent[][] humansNMA;
|
|
private NavMeshAgent[][] humansNMA;
|
|
private Animator[][] humansA;
|
|
private Animator[][] humansA;
|
|
@@ -41,113 +48,96 @@ public class WanderingAI : MonoBehaviour
|
|
humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
|
|
humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
|
|
humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
|
|
humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
|
|
wanderTimer = gameObject.GetComponent<InstantiatePrefab>().wanderTimer;
|
|
wanderTimer = gameObject.GetComponent<InstantiatePrefab>().wanderTimer;
|
|
- waitingTimer = gameObject.GetComponent<InstantiatePrefab>().waitingTimer;
|
|
|
|
|
|
+ //waitingTimer = gameObject.GetComponent<InstantiatePrefab>().waitingTimer;
|
|
waypoints = gameObject.GetComponent<InstantiatePrefab>().waypointsArray;
|
|
waypoints = gameObject.GetComponent<InstantiatePrefab>().waypointsArray;
|
|
- waypointsList = new List<Transform>(waypoints);
|
|
|
|
humansPrio = gameObject.GetComponent<InstantiatePrefab>().humanPriorities;
|
|
humansPrio = gameObject.GetComponent<InstantiatePrefab>().humanPriorities;
|
|
// Set length of variables to humanGo.Length
|
|
// Set length of variables to humanGo.Length
|
|
target = new Vector3[humansGO.Length][];
|
|
target = new Vector3[humansGO.Length][];
|
|
timer = new float[humansGO.Length];
|
|
timer = new float[humansGO.Length];
|
|
|
|
|
|
|
|
+ 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];
|
|
|
|
+
|
|
// Initialize size and content
|
|
// Initialize size and content
|
|
- for(int i = 0; i < humansGO.Length; i++)
|
|
|
|
|
|
+ for (int i = 0; i < humansGO.Length; i++)
|
|
{
|
|
{
|
|
target[i] = new Vector3[humansGO[i].Length];
|
|
target[i] = new Vector3[humansGO[i].Length];
|
|
-
|
|
|
|
- for(int j = 0; j < humansGO[i].Length; j++)
|
|
|
|
- {
|
|
|
|
- timer[i] = wanderTimer[i];
|
|
|
|
- }
|
|
|
|
|
|
+ timer[i] = 0;
|
|
}
|
|
}
|
|
|
|
|
|
globalTimer = 0;
|
|
globalTimer = 0;
|
|
}
|
|
}
|
|
|
|
|
|
- //private void FixedUpdate()
|
|
|
|
- //{
|
|
|
|
- // for(int i = 0; i < humansGO.Length; ++i)
|
|
|
|
- // {
|
|
|
|
- // if (timer[i] >= wanderTimer[i])
|
|
|
|
- // {
|
|
|
|
- // if (waypoints.Length == 0) return;
|
|
|
|
-
|
|
|
|
- // int currentWP = Random.Range(0, waypointsList.Count);
|
|
|
|
- // target[i][0] = CheckTarget(waypointsList[currentWP].transform.position, targetScattering);
|
|
|
|
-
|
|
|
|
- // if(target[i][0].x != float.PositiveInfinity)
|
|
|
|
- // {
|
|
|
|
- // for (int j = 0; j < humansGO[i].Length; ++j)
|
|
|
|
- // {
|
|
|
|
- // target[i][j] = target[i][0];
|
|
|
|
-
|
|
|
|
- // if (humansNMA[i][j].isActiveAndEnabled)
|
|
|
|
- // humansNMA[i][j].SetDestination(target[i][j]);
|
|
|
|
-
|
|
|
|
- // humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
|
|
|
|
- // }
|
|
|
|
- // timer[i] = 0;
|
|
|
|
- // // remove from list to ensure that all WP are visited at the frequency indicated; if list empty fill list with initial WPs
|
|
|
|
- // ReduceWaypoints(currentWP);
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- // else
|
|
|
|
- // {
|
|
|
|
- // for (int j = 0; j < humansGO[i].Length; ++j)
|
|
|
|
- // {
|
|
|
|
- // humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
|
|
|
|
-
|
|
|
|
- // // 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];
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- // timer[i] += Time.deltaTime;
|
|
|
|
- // }
|
|
|
|
- //}
|
|
|
|
-
|
|
|
|
private void FixedUpdate()
|
|
private void FixedUpdate()
|
|
{
|
|
{
|
|
|
|
+ if(globalTimer >= leaveTimer)
|
|
|
|
+ leaveMarket = true;
|
|
|
|
+
|
|
for (int i = 0; i < humansGO.Length; ++i)
|
|
for (int i = 0; i < humansGO.Length; ++i)
|
|
{
|
|
{
|
|
if (waypoints.Length == 0) return;
|
|
if (waypoints.Length == 0) return;
|
|
-
|
|
|
|
- for(int j = 0; j < humansGO[i].Length; ++j)
|
|
|
|
|
|
+
|
|
|
|
+ if (timer[i] >= currentWait[i])
|
|
{
|
|
{
|
|
- 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)
|
|
|
|
|
|
+ for (int j = 0; j < humansGO[i].Length; ++j)
|
|
{
|
|
{
|
|
- int currentWP = Random.Range(0, waypointsList.Count);
|
|
|
|
- target[i][j] = CheckTarget(waypointsList[currentWP].transform.position, targetScattering);
|
|
|
|
-
|
|
|
|
- if (target[i][0].x != float.PositiveInfinity)
|
|
|
|
|
|
+ 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)
|
|
{
|
|
{
|
|
- if (humansNMA[i][j].isActiveAndEnabled)
|
|
|
|
- humansNMA[i][j].SetDestination(target[i][j]);
|
|
|
|
-
|
|
|
|
- humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
|
|
|
|
-
|
|
|
|
|
|
+ if (maxWPCount <= 0) maxWPCount = waypointsList.Count;
|
|
|
|
+ int currentWPIndex = Random.Range(0, maxWPCount);
|
|
|
|
+ target[i][j] = CheckTarget(waypointsList[currentWPIndex].transform.position, targetScattering);
|
|
|
|
+
|
|
|
|
+ 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)
|
|
|
|
+ {
|
|
|
|
+ waitingTimer[i] = new List<int>(waitingList);
|
|
|
|
+ countFilling++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+ AppendIndexToEndWP(currentWPIndex);
|
|
|
|
+ AppendIndexToEndWT(currentWPIndex);
|
|
|
|
+ maxWPCount--;
|
|
|
|
+ timer[i] = 0;
|
|
}
|
|
}
|
|
- // remove from list to ensure that all WP are visited at the frequency indicated; if list empty fill list with initial WPs
|
|
|
|
- ReduceWaypoints(currentWP);
|
|
|
|
}
|
|
}
|
|
- else
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ for (int j = 0; j < humansGO[i].Length; ++j)
|
|
{
|
|
{
|
|
humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
|
|
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 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)
|
|
if (humansNMA[i][j].velocity.magnitude <= 0.01f)
|
|
|
|
+ {
|
|
humansNMA[i][j].avoidancePriority = 0;
|
|
humansNMA[i][j].avoidancePriority = 0;
|
|
|
|
+ }
|
|
else
|
|
else
|
|
humansNMA[i][j].avoidancePriority = humansPrio[i][j];
|
|
humansNMA[i][j].avoidancePriority = humansPrio[i][j];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- timer[i] += Time.deltaTime;
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ globalTimer += Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
|
|
public static Vector3 CheckTarget(Vector3 target, float dist)
|
|
public static Vector3 CheckTarget(Vector3 target, float dist)
|
|
@@ -163,12 +153,21 @@ public class WanderingAI : MonoBehaviour
|
|
return navHit.position;
|
|
return navHit.position;
|
|
}
|
|
}
|
|
|
|
|
|
- public void ReduceWaypoints(int index)
|
|
|
|
|
|
+ public void AppendIndexToEndWP(int index)
|
|
{
|
|
{
|
|
|
|
+ waypointsList.Add(waypointsList[index]);
|
|
waypointsList.RemoveAt(index);
|
|
waypointsList.RemoveAt(index);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void AppendIndexToEndWT(int index)
|
|
|
|
+ {
|
|
|
|
+ waitingTimer.Add(waitingTimer[index]);
|
|
|
|
+ waitingTimer.RemoveAt(index);
|
|
|
|
+ }
|
|
|
|
|
|
- if(waypointsList.Count <= 0)
|
|
|
|
- waypointsList = new List<Transform>(waypoints);
|
|
|
|
|
|
+ public void RemoveIndexFromWaitingTimer(int i, int removeIndex)
|
|
|
|
+ {
|
|
|
|
+ waitingTimer[i].RemoveAt(removeIndex);
|
|
}
|
|
}
|
|
|
|
|
|
public void LeaveMarket(int i)
|
|
public void LeaveMarket(int i)
|