EventHandler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using CSVReader;
  5. namespace ObjectScripts
  6. {
  7. public class EventHandler : AbstractEventHandler
  8. {
  9. ///<summary>
  10. /// EventPrefab
  11. ///</summary>
  12. [SerializeField] GameObject EventPrefab;
  13. ///<summary>
  14. /// Manager Object
  15. ///</summary>
  16. [SerializeField] AbstractManager Manager;
  17. ///<summary>
  18. /// next Id to assign
  19. ///</summary>
  20. private uint nextID = 0;
  21. ///<summary>
  22. /// Event CSV Reader
  23. ///</summary>
  24. EventCSVReader EventCSV;
  25. ///<summary>
  26. /// EventBuffer
  27. ///</summary>
  28. EventObject[] Events;
  29. ///<summary>
  30. /// MapSensor for mapping local position to world position
  31. ///</summary>
  32. IMapSensor MapSensor;
  33. ///<summary>
  34. /// Control Coroutine
  35. ///</summary>
  36. bool running;
  37. ///<summary>
  38. /// number of known events
  39. ///</summary>
  40. float KnownCount = 0;
  41. void Start()
  42. {
  43. EventList = new List<AbstractEventScript>();
  44. // EventCSV = new EventCSVReader(filename);
  45. // EventObject[] events = EventCSV.ReadFile();
  46. // Handle(events);
  47. }
  48. ///<summary>
  49. /// Set up EventHandler
  50. ///</summary>
  51. public override void SetUp(EventCSVReader eventCSV, IMapSensor mapSensor)
  52. {
  53. EventCSV = eventCSV;
  54. MapSensor = mapSensor;
  55. running = true;
  56. StartCoroutine(UpdateWithProzessorUpdateRate());
  57. }
  58. ///<summary>
  59. /// Read events every 1/updaterate seconds
  60. ///</summary>
  61. IEnumerator UpdateWithProzessorUpdateRate()
  62. {
  63. while (running)
  64. {
  65. this.Events = this.EventCSV.ReadFile();
  66. if (Events.Length > KnownCount)
  67. {
  68. Events = MapEventsToWorldposition(Events);
  69. Handle(Events);
  70. }
  71. yield return new WaitForSecondsRealtime(this.Manager.UpdateRateInSeconds);
  72. }
  73. }
  74. ///<summary>
  75. /// Map Event local position to global position
  76. ///</summary>
  77. private EventObject[] MapEventsToWorldposition(EventObject[] events)
  78. {
  79. List<InputObject> eventPositions = new List<InputObject>();
  80. List<InputObject> eventViewingPositions = new List<InputObject>();
  81. foreach (EventObject e in events)
  82. {
  83. eventPositions.Add(new InputObject(0, 0, e.Pos.x, e.Pos.y, e.Pos.z, 0, 0, 0, 0, 0, 0, 0, 0, 0, e.ID));
  84. eventViewingPositions.Add(new InputObject(0, 0, e.IdealViewingPos.x, e.IdealViewingPos.y, e.IdealViewingPos.z, 0, 0, 0, 0, 0, 0, 0, 0, 0, e.ID));
  85. }
  86. eventPositions = MapSensor.MapToWorldPositions(eventPositions);
  87. eventViewingPositions = MapSensor.MapToWorldPositions(eventViewingPositions);
  88. int len = eventPositions.Count;
  89. for (int i = 0; i < len; i++)
  90. {
  91. InputObject eventPosition = eventPositions[i];
  92. InputObject eventViewingPosition = eventViewingPositions[i];
  93. events[i].Pos = new Vector3(eventPosition.Pos.x, eventPosition.Pos.y, eventPosition.Pos.z);
  94. events[i].setIdealViewingPosition(eventViewingPosition.Pos.x, eventViewingPosition.Pos.y, eventViewingPosition.Pos.z);
  95. }
  96. return events;
  97. }
  98. ///<summary>
  99. /// Handle new events
  100. ///</summary>
  101. public override void Handle(EventObject[] events)
  102. {
  103. if (events.Length > EventList.Count)
  104. for (int i = EventList.Count; i < events.Length; i++)
  105. {
  106. EventObject e = events[i];
  107. createEvent(e);
  108. }
  109. }
  110. ///<summary>
  111. /// Create new Events
  112. ///</summary>
  113. void createEvent(EventObject e)
  114. {
  115. GameObject eventGameObject = Instantiate(EventPrefab);
  116. eventGameObject.name = "Event" + e.TimestampStart + " " + e.Type;
  117. AbstractEventScript eventScript = eventGameObject.GetComponent<AbstractEventScript>();
  118. eventScript.Camera = this.Camera;
  119. eventScript.Event = e;
  120. eventScript.EventHandler = this;
  121. eventScript.ID = nextID;
  122. nextID++;
  123. EventList.Add(eventScript);
  124. KnownCount++;
  125. }
  126. // Update is called once per frame
  127. void Update()
  128. {
  129. this.CurrentTimestamp = this.Manager.GameTimestamp;
  130. }
  131. private void OnDestroy()
  132. {
  133. running = false;
  134. }
  135. }
  136. }