EventScript.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace ObjectScripts
  5. {
  6. public class EventScript : AbstractEventScript
  7. {
  8. ///<summary>
  9. /// GameObject to indicate event from far away
  10. ///</summary>
  11. GameObject EventFlag;
  12. ///<summary>
  13. ///
  14. ///</summary>
  15. GameObject EventFlagpole;
  16. ///<summary>
  17. /// Prefab to indicate ideal viewing point
  18. ///</summary>
  19. GameObject EventViewingPoint;
  20. ///<summary>
  21. /// is event flag visible
  22. ///</summary>
  23. bool EventFlagIsVisible = true;
  24. ///<summary>
  25. ///
  26. ///</summary>
  27. [SerializeField] string nameOfCamera;
  28. ///<summary>
  29. /// EventFlag prefab
  30. ///</summary>
  31. [SerializeField] GameObject Flag;
  32. ///<summary>
  33. /// time an event is shown earlier and longer than start- and endtimestamp
  34. ///</summary>
  35. [SerializeField] double InTimeOffset = 3;
  36. ///<summary>
  37. /// Array of Sprites indicating each Eventtype
  38. ///</summary>
  39. [SerializeField] Sprite[] EventImages;
  40. ///<summary>
  41. /// not used
  42. ///</summary>
  43. Sprite EventImage;
  44. ///<summary>
  45. /// not used
  46. ///</summary>
  47. SpriteRenderer EventSpriteRenderer;
  48. ///<summary>
  49. /// Position of the flage. Changes with the Player
  50. ///</summary>
  51. Vector3 flagPosition;
  52. ///<summary>
  53. /// event is in time. timestamp of event +-offset reached
  54. ///</summary>
  55. public bool inTime = true;
  56. // Start is called before the first frame update
  57. void Start()
  58. {
  59. //Create Flag and EventPoint
  60. EventFlag = Instantiate(Flag, Vector3.zero, new Quaternion());
  61. EventPoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  62. // set positions
  63. EventPoint.transform.SetParent(this.gameObject.transform);
  64. EventFlag.transform.SetParent(this.EventPoint.transform);
  65. EventFlag.transform.Find("EventImage").gameObject.GetComponent<SpriteRenderer>().sprite = EventImages[(int)Event.Type];
  66. EventPoint.name = "EventPoint";
  67. EventFlag.name = "EventFlag";
  68. this.gameObject.transform.position = Event.Pos;
  69. // if ideal viewing position is set. indicate ideal viewing postion
  70. flagPosition = this.EventFlag.transform.position;
  71. if (!Event.IdealViewingPos.Equals(Event.Pos))
  72. {
  73. EventViewingPoint = GameObject.CreatePrimitive(PrimitiveType.Cube);
  74. EventViewingPoint.transform.SetParent(this.EventPoint.transform);
  75. EventViewingPoint.transform.position = Event.IdealViewingPos;
  76. EventViewingPoint.name = "EventViewingPoint";
  77. }
  78. Position2D = new Vector2(EventPoint.transform.position.x, EventPoint.transform.position.z);
  79. // SetEventPointIsVisible(false);
  80. EventIsVisible = true;
  81. }
  82. void Update()
  83. {
  84. //If event in time then object will be visible
  85. if (EventInTime())
  86. {
  87. if (!inTime)
  88. {
  89. inTime = true;
  90. ChangeEventVisibilityTo(true);
  91. }
  92. }
  93. else
  94. {
  95. if (inTime)
  96. {
  97. inTime = false;
  98. ChangeEventVisibilityTo(false);
  99. }
  100. }
  101. HandleFlagBehavior();
  102. //DrawPole(this.EventFlagpole, this.EventPoint.transform.position, this.EventFlag.transform.position);
  103. }
  104. ///<summary>
  105. /// Handle Flag Positon. Flag will have the same y-coordinate as the player
  106. ///</summary>
  107. private void HandleFlagBehavior()
  108. {
  109. this.EventFlag.transform.LookAt(this.Camera.transform);
  110. flagPosition = this.EventFlag.transform.position;
  111. this.EventFlag.transform.position = new Vector3(flagPosition.x, this.Camera.transform.position.y, flagPosition.z);
  112. if (inTime)
  113. {
  114. if (this.EventFlag.transform.localPosition.y < 1 && this.EventFlag.transform.localPosition.y > -1)
  115. {
  116. this.EventFlag.GetComponent<SpriteRenderer>().enabled = false;
  117. }
  118. else
  119. {
  120. if (!this.EventFlag.GetComponent<SpriteRenderer>().enabled)
  121. {
  122. this.EventFlag.GetComponent<SpriteRenderer>().enabled = true;
  123. }
  124. if (this.EventFlag.transform.localPosition.y < -1)
  125. {
  126. this.EventFlag.GetComponent<SpriteRenderer>().flipY = true;
  127. }
  128. else
  129. {
  130. this.EventFlag.GetComponent<SpriteRenderer>().flipY = false;
  131. }
  132. }
  133. }
  134. }
  135. ///<summary>
  136. /// currenttimestamp in intervall [TimestampStart - InTimeOffset ,TimestampEnd + InTimeOffset ]
  137. ///</summary>
  138. private bool EventInTime()
  139. {
  140. return (this.EventHandler.CurrentTimestamp >= this.Event.TimestampStart - InTimeOffset
  141. && (this.EventHandler.CurrentTimestamp <= this.Event.TimestampEnd + InTimeOffset || ((this.Event.TimestampEnd == -1 && this.EventHandler.CurrentTimestamp <= this.Event.TimestampStart + InTimeOffset)))); // || this.Event.TimestampEnd == -1
  142. }
  143. ///<summary>
  144. /// set Event visible or invisible
  145. ///</summary>
  146. private void ChangeEventVisibilityTo(bool visible)
  147. {
  148. foreach (Renderer renderer in this.GetComponentsInChildren<Renderer>(visible))
  149. {
  150. renderer.enabled = visible;
  151. }
  152. this.EventIsVisible = visible;
  153. }
  154. ///<summary>
  155. ///
  156. ///</summary>
  157. private void DrawPole(GameObject poleObject, Vector3 eventPointposition, Vector3 eventFlagposition)
  158. {
  159. float distancesBetweenPointAndFlag = eventFlagposition.y - eventPointposition.y;
  160. Renderer poleObjectRenderer = poleObject.GetComponent<Renderer>();
  161. Vector3 poleObjectCurrentScale = poleObject.transform.localScale;
  162. Vector3 poleObjectSize = poleObjectRenderer.bounds.size;
  163. float poleObjectHeight = poleObjectSize.y / poleObjectCurrentScale.y;
  164. float amountPoleObjects = distancesBetweenPointAndFlag / poleObjectHeight;
  165. Vector3 polePos = poleObject.transform.position;
  166. poleObject.transform.localPosition = new Vector3(polePos.x, amountPoleObjects + 2 * poleObjectHeight, polePos.z);
  167. poleObject.transform.localScale = new Vector3(poleObjectCurrentScale.x, amountPoleObjects - 2 * poleObjectHeight, poleObjectCurrentScale.z);
  168. }
  169. ///<summary>
  170. ///
  171. ///</summary>
  172. private void FixedUpdate()
  173. {
  174. // SetEventPointIsVisible(CheckObjectVisible(EventPoint, Camera, ignoreNames));
  175. // EventFlagIsVisible = CheckObjectVisible(EventFlag, Camera, ignoreNames);
  176. }
  177. ///<summary>
  178. ///
  179. ///</summary>
  180. private bool CheckObjectVisible(GameObject obj, GameObject Camera, string ignoreNames)
  181. {
  182. RaycastHit raycastHit;
  183. if (obj.GetComponent<Renderer>().isVisible)
  184. {
  185. if (Physics.Raycast(Camera.transform.position, obj.transform.position - Camera.transform.position, out raycastHit))
  186. {
  187. string hitName = raycastHit.collider.gameObject.name;
  188. if (ignoreNames.Contains(hitName))
  189. {
  190. return true;
  191. }
  192. else
  193. {
  194. return false;
  195. }
  196. }
  197. else
  198. {
  199. return true;
  200. }
  201. }
  202. else
  203. {
  204. return false;
  205. }
  206. }
  207. }
  208. }