WalkLerpPlayback.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 dir = Directory.GetCurrentDirectory();
  42. string reference = @"\Assets\Data_position\Walk" + index + ".csv";
  43. timePosRotList = gameObject.GetComponent<ReadFromCSV>().ReadFromCSVFile(dir + reference);
  44. // Set initial position and rotation
  45. currentStartPoint = 0;
  46. for (int i = 0; i < humansGO.Length; ++i)
  47. {
  48. for (int j = 0; j < humansGO[i].Length; ++j)
  49. {
  50. humansGO[i][j].transform.position = timePosRotList[0][0].Item2[0]; // First entry of Position
  51. humansGO[i][j].transform.rotation = timePosRotList[0][0].Item3[0]; // First entry of Rotation
  52. // Animation Idle
  53. humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
  54. humansNMA[i][j].enabled = false;
  55. }
  56. }
  57. // Set the Slider settings
  58. if (slider == null)
  59. {
  60. Debug.Log("No Slider instance attached");
  61. return;
  62. }
  63. maxIJ = GetHumanWithLongestScreenTime();
  64. float maxTime = (float) Math.Round(timePosRotList[maxIJ[0]][maxIJ[1]].Item1[timePosRotList[maxIJ[0]][maxIJ[1]].Item1.Count - 1], 2);
  65. slider.minValue = 0;
  66. slider.maxValue = (maxTime / Time.deltaTime);
  67. slider.wholeNumbers = true;
  68. prevSliderValue = slider.value;
  69. SetTransform(currentStartPoint);
  70. currentStartPoint++;
  71. slider.value = currentStartPoint;
  72. startTime = Time.time;
  73. }
  74. private void FixedUpdate()
  75. {
  76. if (pause)
  77. indexChangeRate = 0;
  78. if (play)
  79. indexChangeRate = 1;
  80. if (rewind)
  81. indexChangeRate = -1;
  82. if (play || rewind)
  83. {
  84. if ((currentStartPoint + indexChangeRate) < timePosRotList[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
  85. {
  86. if(slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play
  87. {
  88. prevSliderValue = slider.value;
  89. currentStartPoint += indexChangeRate;
  90. slider.value = currentStartPoint;
  91. }
  92. else
  93. {
  94. prevSliderValue = slider.value;
  95. currentStartPoint = (int) slider.value;
  96. }
  97. SetTransform(currentStartPoint);
  98. }
  99. else if(slider.value - prevSliderValue != -1|| slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1)
  100. {
  101. currentStartPoint = (int) slider.value;
  102. }
  103. }
  104. else
  105. {
  106. // upper else-Statement only for pause
  107. if (slider.value - prevSliderValue != 0)
  108. {
  109. //prevSliderValue = slider.value;
  110. currentStartPoint = (int) slider.value;
  111. }
  112. for (int i = 0; i < humansGO.Length; ++i)
  113. {
  114. for (int j = 0; j < humansGO[i].Length; ++j)
  115. {
  116. if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
  117. {
  118. if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
  119. {
  120. humansGO[i][j].transform.position = timePosRotList[i][j].Item2[timePosRotList[i][j].Item2.Count - 1];
  121. humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[timePosRotList[i][j].Item3.Count - 1];
  122. }
  123. humansA[i][j].SetBool(isWalking, false);
  124. continue;
  125. }
  126. if (currentStartPoint < timePosRotList[i][j].Item2.Count)
  127. {
  128. humansGO[i][j].transform.position = timePosRotList[i][j].Item2[currentStartPoint]; // First entry of Position
  129. humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[currentStartPoint]; // First entry of Rotation
  130. humansA[i][j].SetBool(isWalking, false);
  131. humansA[i][j].SetBool(isGrabbing, false);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. private void SetTransform(int currentStartPoint)
  138. {
  139. for (int i = 0; i < humansGO.Length; ++i)
  140. {
  141. for (int j = 0; j < humansGO[i].Length; ++j)
  142. {
  143. // if so ArgumentOutOfRangeException
  144. if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
  145. {
  146. if((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
  147. {
  148. humansGO[i][j].transform.position = timePosRotList[i][j].Item2[timePosRotList[i][j].Item2.Count - 1];
  149. humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[timePosRotList[i][j].Item3.Count - 1];
  150. }
  151. humansA[i][j].SetBool(isWalking, false);
  152. continue;
  153. }
  154. Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
  155. Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + indexChangeRate];
  156. Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
  157. Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + indexChangeRate];
  158. float distCovered = (Time.time - startTime) * timePosRotList[i][j].Item4[currentStartPoint];
  159. float fracJourney = 1f;
  160. float journeyLength = Vector3.Distance(startPos, endPos);
  161. if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
  162. // Animation Idle, if startPoint == endPoint and startRot == endRot
  163. //if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count)
  164. //{
  165. // humansA[i][j].SetBool("isWalking", false);
  166. //}
  167. //else if (journeyLength > 0)
  168. //{
  169. // humansA[i][j].SetBool("isWalking", true);
  170. //}
  171. //humansA[i][j].speed = 2*(journeyLength / 0.2f);
  172. humansA[i][j].SetBool(isWalking, timePosRotList[i][j].Item4[currentStartPoint] > 0.01f);
  173. if (Vector3.Distance(humansGO[i][j].transform.position, stealHere) <= 1.5f && timePosRotList[i][j].Item4[currentStartPoint] == 0.0f)
  174. humansA[i][j].SetBool(isGrabbing, true);
  175. else
  176. humansA[i][j].SetBool(isGrabbing, false);
  177. humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
  178. humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
  179. }
  180. }
  181. }
  182. private int[] GetHumanWithLongestScreenTime()
  183. {
  184. int[] maxIJ = new int[2] { 0, 0 };
  185. float currentMaxTime = 0;
  186. for (int i = 0; i < humansGO.Length; ++i)
  187. {
  188. for (int j = 0; j < humansGO[i].Length; ++j)
  189. {
  190. if(currentMaxTime < timePosRotList[i][j].Item1.Count)
  191. {
  192. currentMaxTime = timePosRotList[i][j].Item1.Count;
  193. maxIJ[0] = i;
  194. maxIJ[1] = j;
  195. }
  196. }
  197. }
  198. return maxIJ;
  199. }
  200. }