SpatialTimeSimulation.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.AI;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System;
  7. [DefaultExecutionOrder(61)]
  8. [RequireComponent(typeof(InstantiatePrefab), typeof(WriteInCSV), typeof(ReadFromCSV))]
  9. public class SpatialTimeSimulation : MonoBehaviour
  10. {
  11. // Waypoints and Rotations
  12. public Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] timePosRotList;
  13. // Private Journey Settings
  14. private int currentStartPoint;
  15. private float startTime;
  16. // Global GameObjects
  17. private GameObject[][] humansGO;
  18. private NavMeshAgent[][] humansNMA;
  19. private Animator[][] humansA;
  20. // Playback variables
  21. private int[] maxIJ;
  22. private int indexChangeRate;
  23. [HideInInspector]
  24. public bool rewind, pause, play; //PlaybackController
  25. // Slider Settings
  26. public Slider slider;
  27. private float prevSliderValue;
  28. // Thief Settings
  29. //private Vector3 stealHere;
  30. // Spatial time visualization
  31. private List<Vector3>[][] positions;
  32. public float thickness = 0.1f;
  33. public Material color;
  34. private void Start()
  35. {
  36. // Get information from InstatiatePrefab
  37. humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  38. humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
  39. humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
  40. positions = new List<Vector3>[humansGO.Length][];
  41. //stealHere = gameObject.GetComponent<ControllingThief>().stealHere;
  42. // Read from CSV file and save time, position, rotation in matrix
  43. int index = gameObject.GetComponent<WriteInCSV>().index;
  44. string dir = Directory.GetCurrentDirectory();
  45. string reference = @"\Assets\Data_position\Walk" + index + ".csv";
  46. timePosRotList = gameObject.GetComponent<ReadFromCSV>().ReadFromCSVFile(dir + reference);
  47. // Add 0.5f + time to y
  48. // Disable components and add LineRenderer
  49. currentStartPoint = 0;
  50. for (int i = 0; i < humansGO.Length; ++i)
  51. {
  52. positions[i] = new List<Vector3>[humansGO[i].Length];
  53. for (int j = 0; j < humansGO[i].Length; ++j)
  54. {
  55. // A shortcut to use the position data immediately for the LineRenderer
  56. positions[i][j] = new List<Vector3>();
  57. foreach (var position in timePosRotList[i][j].Item2)
  58. {
  59. positions[i][j].Add(new Vector3(position.x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], position.z));
  60. currentStartPoint++;
  61. }
  62. currentStartPoint = 0;
  63. // Every component should be disabled except the humans game object, bc the LineRenderer operates there
  64. humansGO[i][j].SetActive(true);
  65. humansA[i][j].enabled = false;
  66. humansNMA[i][j].enabled = false;
  67. foreach(Transform child in humansGO[i][j].transform)
  68. {
  69. child.gameObject.SetActive(false);
  70. }
  71. // Add the LineRenderer to the gameobject
  72. LineRenderer drawPath = humansGO[i][j].AddComponent<LineRenderer>();
  73. drawPath.material = new Material(Shader.Find("Sprites/Default"));
  74. drawPath.startColor = color.color;
  75. drawPath.endColor = color.color;
  76. drawPath.startWidth = thickness;
  77. drawPath.endWidth = thickness;
  78. drawPath.positionCount = currentStartPoint + 1;
  79. drawPath.SetPosition(currentStartPoint, positions[i][j][currentStartPoint]);
  80. }
  81. }
  82. // Set the Slider settings
  83. if (slider == null)
  84. {
  85. Debug.Log("No Slider instance attached");
  86. return;
  87. }
  88. maxIJ = GetHumanWithLongestScreenTime();
  89. float maxTime = (float)Math.Round(timePosRotList[maxIJ[0]][maxIJ[1]].Item1[timePosRotList[maxIJ[0]][maxIJ[1]].Item1.Count - 1], 2);
  90. slider.minValue = 0;
  91. slider.maxValue = (maxTime / Time.deltaTime);
  92. slider.wholeNumbers = true;
  93. prevSliderValue = slider.value;
  94. currentStartPoint++;
  95. slider.value = currentStartPoint;
  96. startTime = Time.time;
  97. }
  98. private void FixedUpdate()
  99. {
  100. if (pause)
  101. indexChangeRate = 0;
  102. if (play)
  103. indexChangeRate = 1;
  104. if (rewind)
  105. indexChangeRate = -1;
  106. if (play || rewind)
  107. {
  108. if ((currentStartPoint + indexChangeRate) < timePosRotList[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
  109. {
  110. if (slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play
  111. {
  112. prevSliderValue = slider.value;
  113. currentStartPoint += indexChangeRate;
  114. slider.value = currentStartPoint;
  115. }
  116. else
  117. {
  118. prevSliderValue = slider.value;
  119. currentStartPoint = (int)slider.value;
  120. }
  121. SetTransform(currentStartPoint);
  122. }
  123. else if (slider.value - prevSliderValue != -1 || slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1)
  124. {
  125. currentStartPoint = (int)slider.value;
  126. }
  127. }
  128. else
  129. {
  130. currentStartPoint = (int)slider.value;
  131. SetTransform(currentStartPoint);
  132. }
  133. }
  134. private void SetTransform(int currentStartPoint)
  135. {
  136. for (int i = 0; i < humansGO.Length; ++i)
  137. {
  138. for (int j = 0; j < humansGO[i].Length; ++j)
  139. {
  140. LineRenderer drawPath = humansGO[i][j].GetComponent<LineRenderer>();
  141. // if so ArgumentOutOfRangeException
  142. if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
  143. {
  144. if((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
  145. {
  146. drawPath.positionCount = positions[i][j].Count;
  147. drawPath.SetPositions(positions[i][j].ToArray());
  148. }
  149. continue;
  150. }
  151. if (currentStartPoint < timePosRotList[i][j].Item2.Count)
  152. {
  153. // We need to specify the Length of the positionCount variable
  154. drawPath.positionCount = currentStartPoint + 1;
  155. // We copy the positions list and remove every element after the currentStartPoint from the copy
  156. List<Vector3> tmpLst = new List<Vector3>(positions[i][j]);
  157. tmpLst.RemoveRange(currentStartPoint + 1, positions[i][j].Count - currentStartPoint - 1);
  158. drawPath.SetPositions(tmpLst.ToArray());
  159. }
  160. }
  161. }
  162. }
  163. private int[] GetHumanWithLongestScreenTime()
  164. {
  165. int[] maxIJ = new int[2] { 0, 0 };
  166. float currentMaxTime = 0;
  167. for (int i = 0; i < humansGO.Length; ++i)
  168. {
  169. for (int j = 0; j < humansGO[i].Length; ++j)
  170. {
  171. if (currentMaxTime < timePosRotList[i][j].Item1.Count)
  172. {
  173. currentMaxTime = timePosRotList[i][j].Item1.Count;
  174. maxIJ[0] = i;
  175. maxIJ[1] = j;
  176. }
  177. }
  178. }
  179. return maxIJ;
  180. }
  181. }