SpatialTimeSimulation.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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(40)]
  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. // Playback variables
  19. private int[] maxIJ;
  20. private int indexChangeRate;
  21. [HideInInspector]
  22. public bool rewind, pause, play; //PlaybackController
  23. // Slider Settings
  24. public Slider slider;
  25. private float prevSliderValue;
  26. // Thief Settings
  27. private Vector3 stealHere;
  28. // Testing
  29. public float thickness = 0.1f;
  30. private void Start()
  31. {
  32. // Get information from InstatiatePrefab
  33. humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  34. NavMeshAgent[][] humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
  35. stealHere = gameObject.GetComponent<ControllingThief>().stealHere;
  36. // Read from CSV file and save time, position, rotation in matrix
  37. int index = gameObject.GetComponent<WriteInCSV>().index;
  38. string dir = Directory.GetCurrentDirectory();
  39. string reference = @"\Assets\Data_position\Walk" + index + ".csv";
  40. timePosRotList = gameObject.GetComponent<ReadFromCSV>().ReadFromCSVFile(dir + reference);
  41. // Set initial position and rotation
  42. currentStartPoint = 0;
  43. for (int i = 0; i < humansGO.Length; ++i)
  44. {
  45. for (int j = 0; j < humansGO[i].Length; ++j)
  46. {
  47. humansGO[i][j].SetActive(false);
  48. humansNMA[i][j].enabled = false;
  49. }
  50. }
  51. // Set the Slider settings
  52. if (slider == null)
  53. {
  54. Debug.Log("No Slider instance attached");
  55. return;
  56. }
  57. maxIJ = GetHumanWithLongestScreenTime();
  58. float maxTime = (float)Math.Round(timePosRotList[maxIJ[0]][maxIJ[1]].Item1[timePosRotList[maxIJ[0]][maxIJ[1]].Item1.Count - 1], 2);
  59. slider.minValue = 0;
  60. slider.maxValue = (maxTime / Time.deltaTime);
  61. slider.wholeNumbers = true;
  62. prevSliderValue = slider.value;
  63. //SetTransform(currentStartPoint);
  64. //currentStartPoint++;
  65. slider.value = currentStartPoint;
  66. startTime = Time.time;
  67. int count = 0;
  68. // Create all cubes for every human and disable/enable them with time.
  69. GameObject[][] cubes = new GameObject[humansGO.Length][];
  70. for (int i = 0; i < cubes.Length; ++i)
  71. {
  72. cubes[i] = new GameObject[humansGO[i].Length];
  73. for (int j = 0; j < cubes[i].Length; ++j)
  74. {
  75. currentStartPoint = 0;
  76. while (currentStartPoint < timePosRotList[i][j].Item2.Count)
  77. {
  78. //if(currentStartPoint == 0 || currentStartPoint >= timePosRotList[i][j].Item2.Count - 1)
  79. //{
  80. // cubes[i][j] = GameObject.CreatePrimitive(PrimitiveType.Cube);
  81. // cubes[i][j].transform.position = new Vector3(timePosRotList[i][j].Item2[currentStartPoint].x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], timePosRotList[i][j].Item2[currentStartPoint].z);
  82. // cubes[i][j].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
  83. // cubes[i][j].GetComponent<MeshRenderer>().material.color = Color.green;
  84. // count++;
  85. //}
  86. //else if(currentStartPoint < timePosRotList[i][j].Item2.Count - 1)
  87. //{
  88. // if (Vector3.Distance(timePosRotList[i][j].Item2[currentStartPoint - 1], timePosRotList[i][j].Item2[currentStartPoint]) <= 0.2f)
  89. // {
  90. // cubes[i][j] = GameObject.CreatePrimitive(PrimitiveType.Cube);
  91. // cubes[i][j].transform.position = new Vector3(timePosRotList[i][j].Item2[currentStartPoint].x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], timePosRotList[i][j].Item2[currentStartPoint].z);
  92. // cubes[i][j].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
  93. // cubes[i][j].GetComponent<MeshRenderer>().material.color = Color.green;
  94. // count++;
  95. // }
  96. //}
  97. //currentStartPoint++;
  98. GameObject newPath = new GameObject();
  99. LineRenderer drawPath = newPath.AddComponent<LineRenderer>();
  100. drawPath.material = new Material(Shader.Find("Sprites/Default"));
  101. drawPath.startColor = Color.green;
  102. drawPath.endColor = Color.green;
  103. drawPath.startWidth = thickness;
  104. drawPath.endWidth = thickness;
  105. drawPath.positionCount = timePosRotList[i][j].Item2.Count;
  106. List<Vector3> newList = new List<Vector3>();
  107. foreach(var vec in timePosRotList[i][j].Item2)
  108. {
  109. var z = new Vector3(vec.x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], vec.z);
  110. newList.Add(z);
  111. currentStartPoint++;
  112. }
  113. drawPath.SetPositions(newList.ToArray());
  114. }
  115. }
  116. }
  117. Debug.Log("Count: " + count);
  118. }
  119. //private void FixedUpdate()
  120. //{
  121. // if (pause)
  122. // indexChangeRate = 0;
  123. // if (play)
  124. // indexChangeRate = 1;
  125. // if (rewind)
  126. // indexChangeRate = -1;
  127. // if (play || rewind)
  128. // {
  129. // if ((currentStartPoint + indexChangeRate) < timePosRotList[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
  130. // {
  131. // if (slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play
  132. // {
  133. // prevSliderValue = slider.value;
  134. // currentStartPoint += indexChangeRate;
  135. // slider.value = currentStartPoint;
  136. // }
  137. // else
  138. // {
  139. // prevSliderValue = slider.value;
  140. // currentStartPoint = (int)slider.value;
  141. // }
  142. // SetTransform(currentStartPoint);
  143. // }
  144. // else if (slider.value - prevSliderValue != -1 || slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1)
  145. // {
  146. // currentStartPoint = (int)slider.value;
  147. // }
  148. // }
  149. // else
  150. // {
  151. // // upper else-Statement only for pause
  152. // if (slider.value - prevSliderValue != 0)
  153. // {
  154. // //prevSliderValue = slider.value;
  155. // currentStartPoint = (int)slider.value;
  156. // }
  157. // for (int i = 0; i < humansGO.Length; ++i)
  158. // {
  159. // for (int j = 0; j < humansGO[i].Length; ++j)
  160. // {
  161. // if (currentStartPoint < timePosRotList[i][j].Item2.Count)
  162. // {
  163. // humansGO[i][j].transform.position = timePosRotList[i][j].Item2[currentStartPoint]; // First entry of Position
  164. // humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[currentStartPoint]; // First entry of Rotation
  165. // }
  166. // }
  167. // }
  168. // }
  169. //}
  170. //private void SetTransform(int currentStartPoint)
  171. //{
  172. // for (int i = 0; i < humansGO.Length; ++i)
  173. // {
  174. // for (int j = 0; j < humansGO[i].Length; ++j)
  175. // {
  176. // // if so ArgumentOutOfRangeException
  177. // if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
  178. // {
  179. // continue;
  180. // }
  181. // Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
  182. // Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + indexChangeRate];
  183. // Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
  184. // Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + indexChangeRate];
  185. // float distCovered = (Time.time - startTime) * timePosRotList[i][j].Item4[currentStartPoint];
  186. // float fracJourney = 1f;
  187. // float journeyLength = Vector3.Distance(startPos, endPos);
  188. // if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
  189. // // Animation Idle, if startPoint == endPoint and startRot == endRot
  190. // //if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count)
  191. // //{
  192. // // humansA[i][j].SetBool("isWalking", false);
  193. // //}
  194. // //else if (journeyLength > 0)
  195. // //{
  196. // // humansA[i][j].SetBool("isWalking", true);
  197. // //}
  198. // //humansA[i][j].speed = 2*(journeyLength / 0.2f);
  199. // humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
  200. // humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
  201. // }
  202. // }
  203. //}
  204. private int[] GetHumanWithLongestScreenTime()
  205. {
  206. int[] maxIJ = new int[2] { 0, 0 };
  207. float currentMaxTime = 0;
  208. for (int i = 0; i < humansGO.Length; ++i)
  209. {
  210. for (int j = 0; j < humansGO[i].Length; ++j)
  211. {
  212. if (currentMaxTime < timePosRotList[i][j].Item1.Count)
  213. {
  214. currentMaxTime = timePosRotList[i][j].Item1.Count;
  215. maxIJ[0] = i;
  216. maxIJ[1] = j;
  217. }
  218. }
  219. }
  220. return maxIJ;
  221. }
  222. }