DataObject.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using CSVReader;
  4. using Logger;
  5. using UnityEngine;
  6. namespace ObjectScripts
  7. {
  8. public class DataObject : MonoBehaviour
  9. {
  10. // Damit DataObject threadsafe ist, so wenig berechnungen wie möglich
  11. // Mann kann nicht Threadsafe auf Unity Objects außerhalb des Unity Main Threads zugreifen
  12. ///<summary>
  13. /// timestamp of last update
  14. ///</summary>
  15. public double Timestamp;
  16. ///<summary>
  17. /// Position to rotate towards
  18. ///</summary>
  19. public Vector3 Rot;
  20. ///<summary>
  21. /// Position too look at
  22. ///</summary>
  23. public Vector3 HeadRot;
  24. ///<summary>
  25. /// Top Color
  26. ///</summary>
  27. public uint TopColor;
  28. ///<summary>
  29. /// Bottom Color
  30. ///</summary>
  31. public uint BotColor;
  32. ///<summary>
  33. /// Entitiy Type
  34. ///</summary>
  35. public CSVReader.EntityType Type;
  36. ///<summary>
  37. /// ID
  38. ///</summary>
  39. public int ID { get; set; }
  40. ///<summary>
  41. /// ObjectHandler which handles the object
  42. ///</summary>
  43. public AbstractObjectHandler Handler { get; internal set; }
  44. ///<summary>
  45. /// The time since the last update occurred.
  46. /// Updated every timerRateInSeconds.
  47. ///</summary>
  48. public float TimeSinceLastUpdate { get; internal set; }
  49. ///<summary>
  50. /// Maximum Time the object can live without an update
  51. ///</summary>
  52. public float TimeToLive { get; internal set; }
  53. ///<summary>
  54. /// Rate for Updating TimeSinceLastUpdate
  55. ///</summary>
  56. public float TimerRateInSeconds { get; internal set; }
  57. /// <summary>
  58. /// Position which the GameObject travels to in UpdateRate Time
  59. /// </summary>
  60. public Vector3 TargetPos { get; internal set; }
  61. /// <summary>
  62. /// Position from which the GameObject started moving
  63. /// </summary>
  64. public Vector3 StartPos { get; internal set; }
  65. /// <summary>
  66. /// Direction which the GameObject will face
  67. /// </summary>
  68. public Quaternion TargetRotation { get; internal set; }
  69. /// <summary>
  70. /// Direction which the GameObject faces
  71. /// </summary>
  72. public Quaternion StartRotation { get; internal set; }
  73. /// <summary>
  74. /// Speed with which the GameObjects moves
  75. /// </summary>
  76. public double Velocity { get; internal set; }
  77. private Object LockObject;
  78. /// <summary>
  79. /// Control Coroutine
  80. /// </summary>
  81. public bool Alive;
  82. /// <summary>
  83. /// Reference to Sensor.
  84. /// Should be the Sensor "sensing" the "Object"
  85. /// </summary>
  86. public GameObject Sensor { get; set; }
  87. ///<summary>
  88. ///change Values that probably won't change anymore
  89. ///</summary>
  90. public void SetUp(int id, AbstractObjectHandler handler, CSVReader.EntityType type, float timeToLive, float gameVelocity)
  91. {
  92. LockObject = new Object();
  93. Alive = true;
  94. ID = id;
  95. Handler = handler;
  96. Type = type;
  97. TimeSinceLastUpdate = 0f;
  98. TimeToLive = timeToLive;
  99. StartCoroutine(UpdateTimer());
  100. }
  101. ///<summary>
  102. /// Increase TimeSinceLastUpdate (+= timerRateInSeconds )
  103. /// and start the Handler deleteprocess if the TimeSinceLastUpdate is greater than the TimeToLive
  104. ///</summary>
  105. private IEnumerator UpdateTimer()
  106. {
  107. float timeDiff, timeTemp = Time.unscaledTime;
  108. while (this.Alive)
  109. {
  110. //calculate the time since last iteration
  111. timeDiff = Time.unscaledTime - timeTemp;
  112. timeTemp += timeDiff;
  113. //and only add it when the simulation is running
  114. //otherwise all objects would be destroyed
  115. if (this.Handler.GameVelocity > 0)
  116. {
  117. //add the time to TimeSinceLastUpdate
  118. TimeSinceLastUpdate += timeDiff;
  119. //if we want that an object will be destroyed after e.g. 1 second
  120. //the timeSinceLastUpdate as to be greater than 1
  121. //if the GameVelocity is 1/2 we want to wait 2 seconds (1/(1/2))
  122. if (TimeSinceLastUpdate > TimeToLive / this.Handler.GameVelocity)
  123. {
  124. CityLogger.Log(string.Format("Destroy {0} after {1} seconds", this.ID, TimeSinceLastUpdate), LogLevel.DEBUG);
  125. //Start DeleteProcess
  126. Handler.DeleteObject(this);
  127. }
  128. //wait
  129. yield return new WaitForSeconds((float)this.Handler.UpdateRate);
  130. }
  131. else
  132. {
  133. //wait till GameVelocity > 0
  134. yield return null;
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// Sets TargetPosition
  140. /// </summary>
  141. /// <remarks>
  142. /// Also sets the StartPos to last TargetPosition
  143. /// </remarks>
  144. public void SetNewTargetPos(Vector3 newPos)
  145. {
  146. if (float.IsNaN(newPos.x) || float.IsNaN(newPos.y) || float.IsNaN(newPos.z))
  147. {
  148. return;
  149. }
  150. StartPos = TargetPos;
  151. TargetPos = newPos;
  152. }
  153. ///<summary>
  154. /// Maps input data
  155. ///</summary>
  156. public void MapInputObjekt(InputObject input, GameObject sensor)
  157. {
  158. //lock (LockObject)
  159. //{ //set TimeSinceLastUpdate to zero
  160. this.TimeSinceLastUpdate = 0f;
  161. this.Timestamp = input.Time;
  162. this.Rot = input.Rot;
  163. this.HeadRot = input.HeadRot;
  164. this.BotColor = input.BotColor;
  165. this.TopColor = input.TopColor;
  166. this.Type = input.Type;
  167. this.Sensor = sensor;
  168. this.SetNewTargetPos(input.Pos);
  169. //}
  170. }
  171. private void OnDestroy()
  172. {
  173. this.Alive = false;
  174. }
  175. }
  176. }