WalkLerpPlayback.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 WalkLerpPlayback : MonoBehaviour
  10. {
  11. // Waypoints and Rotations
  12. public Tuple<List<float>, List<Vector3>, List<Quaternion>, List<float>>[][] timePosRotList;
  13. // Animation
  14. private const string isWalking = "isWalking";
  15. private const string isGrabbing = "isGrabbing";
  16. // Private Journey Settings
  17. private int currentStartPoint;
  18. private float startTime;
  19. // Global GameObjects
  20. private GameObject[][] humansGO;
  21. private Animator[][] humansA;
  22. // Playback variables
  23. private int[] maxIJ;
  24. private int indexChangeRate;
  25. [HideInInspector]
  26. public bool rewind, pause, play; //PlaybackController
  27. // Slider Settings
  28. public Slider slider;
  29. private float prevSliderValue;
  30. // Thief Settings
  31. private Vector3 stealHere;
  32. private void Start()
  33. {
  34. // Get information from InstatiatePrefab
  35. humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  36. humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
  37. NavMeshAgent[][] humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
  38. stealHere = gameObject.GetComponent<ControllingThief>().stealHere;
  39. // Read from CSV file and save time, position, rotation in matrix
  40. int index = gameObject.GetComponent<WriteInCSV>().index;
  41. string folderName = gameObject.GetComponent<WriteInCSV>().folderName;
  42. string dir = Directory.GetCurrentDirectory();
  43. string reference = @"\Assets\Data_position\" + folderName + @"\Walk" + index + @".csv";
  44. timePosRotList = gameObject.GetComponent<ReadFromCSV>().ReadFromCSVFile(dir + reference);
  45. // Set initial position and rotation
  46. currentStartPoint = 0;
  47. for (int i = 0; i < humansGO.Length; ++i)
  48. {
  49. for (int j = 0; j < humansGO[i].Length; ++j)
  50. {
  51. humansGO[i][j].transform.position = timePosRotList[0][0].Item2[0]; // First entry of Position
  52. humansGO[i][j].transform.rotation = timePosRotList[0][0].Item3[0]; // First entry of Rotation
  53. // Animation Idle
  54. humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
  55. humansNMA[i][j].enabled = false;
  56. }
  57. }
  58. // Set the Slider settings
  59. if (slider == null)
  60. {
  61. Debug.Log("No Slider instance attached");
  62. return;
  63. }
  64. maxIJ = GetHumanWithLongestScreenTime();
  65. float maxTime = (float) Math.Round(timePosRotList[maxIJ[0]][maxIJ[1]].Item1[timePosRotList[maxIJ[0]][maxIJ[1]].Item1.Count - 1], 2);
  66. slider.minValue = 0;
  67. var tmpMaxValue = (maxTime / Time.deltaTime) + 1;
  68. if (slider.maxValue < tmpMaxValue)
  69. slider.maxValue = tmpMaxValue;
  70. slider.wholeNumbers = true;
  71. prevSliderValue = slider.value;
  72. SetTransform(currentStartPoint);
  73. currentStartPoint++;
  74. slider.value = currentStartPoint;
  75. startTime = Time.time;
  76. }
  77. private void FixedUpdate()
  78. {
  79. if (pause)
  80. indexChangeRate = 0;
  81. if (play)
  82. indexChangeRate = 1;
  83. if (rewind)
  84. indexChangeRate = -1;
  85. if (play || rewind)
  86. {
  87. if ((currentStartPoint + indexChangeRate) < timePosRotList[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
  88. {
  89. if(slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play
  90. {
  91. prevSliderValue = slider.value;
  92. currentStartPoint += indexChangeRate;
  93. slider.value = currentStartPoint;
  94. }
  95. else
  96. {
  97. prevSliderValue = slider.value;
  98. currentStartPoint = (int) slider.value;
  99. }
  100. SetTransform(currentStartPoint);
  101. }
  102. else if(slider.value - prevSliderValue != -1|| slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1)
  103. {
  104. currentStartPoint = (int) slider.value;
  105. }
  106. }
  107. else
  108. {
  109. // upper else-Statement only for pause
  110. if (slider.value - prevSliderValue != 0)
  111. {
  112. //prevSliderValue = slider.value;
  113. currentStartPoint = (int) slider.value;
  114. }
  115. for (int i = 0; i < humansGO.Length; ++i)
  116. {
  117. for (int j = 0; j < humansGO[i].Length; ++j)
  118. {
  119. if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
  120. {
  121. if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
  122. {
  123. humansGO[i][j].transform.position = timePosRotList[i][j].Item2[timePosRotList[i][j].Item2.Count - 1];
  124. humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[timePosRotList[i][j].Item3.Count - 1];
  125. }
  126. humansA[i][j].SetBool(isWalking, false);
  127. continue;
  128. }
  129. if (currentStartPoint < timePosRotList[i][j].Item2.Count)
  130. {
  131. humansGO[i][j].transform.position = timePosRotList[i][j].Item2[currentStartPoint]; // First entry of Position
  132. humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[currentStartPoint]; // First entry of Rotation
  133. humansA[i][j].SetBool(isWalking, false);
  134. humansA[i][j].SetBool(isGrabbing, false);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. private void SetTransform(int currentStartPoint)
  141. {
  142. for (int i = 0; i < humansGO.Length; ++i)
  143. {
  144. for (int j = 0; j < humansGO[i].Length; ++j)
  145. {
  146. // if so ArgumentOutOfRangeException
  147. if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
  148. {
  149. if((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
  150. {
  151. humansGO[i][j].transform.position = timePosRotList[i][j].Item2[timePosRotList[i][j].Item2.Count - 1];
  152. humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[timePosRotList[i][j].Item3.Count - 1];
  153. }
  154. humansA[i][j].SetBool(isWalking, false);
  155. continue;
  156. }
  157. Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
  158. Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + indexChangeRate];
  159. Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
  160. Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + indexChangeRate];
  161. float distCovered = (Time.time - startTime) * timePosRotList[i][j].Item4[currentStartPoint];
  162. float fracJourney = 1f;
  163. float journeyLength = Vector3.Distance(startPos, endPos);
  164. if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
  165. // Animation Idle, if startPoint == endPoint and startRot == endRot
  166. //if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count)
  167. //{
  168. // humansA[i][j].SetBool("isWalking", false);
  169. //}
  170. //else if (journeyLength > 0)
  171. //{
  172. // humansA[i][j].SetBool("isWalking", true);
  173. //}
  174. //humansA[i][j].speed = 2*(journeyLength / 0.2f);
  175. humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
  176. if (Vector3.Distance(humansGO[i][j].transform.position, stealHere) <= 1.5f && timePosRotList[i][j].Item4[currentStartPoint] == 0.0f)
  177. humansA[i][j].SetBool(isGrabbing, true);
  178. else
  179. humansA[i][j].SetBool(isGrabbing, false);
  180. humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
  181. humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
  182. }
  183. }
  184. }
  185. private int[] GetHumanWithLongestScreenTime()
  186. {
  187. int[] maxIJ = new int[2] { 0, 0 };
  188. float currentMaxTime = 0;
  189. for (int i = 0; i < humansGO.Length; ++i)
  190. {
  191. for (int j = 0; j < humansGO[i].Length; ++j)
  192. {
  193. if(currentMaxTime < timePosRotList[i][j].Item1.Count)
  194. {
  195. currentMaxTime = timePosRotList[i][j].Item1.Count;
  196. maxIJ[0] = i;
  197. maxIJ[1] = j;
  198. }
  199. }
  200. }
  201. return maxIJ;
  202. }
  203. }