Event.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //***********************************************************
  2. // Filename: Event.cs
  3. // Author: Niclas Dobbertin
  4. // Last changes: Mittwoch, 8. August 2018
  5. // Content: This file contains the abstract Event class and all inheriting Events used
  6. //***********************************************************
  7. using System;
  8. using UnityEngine;
  9. /// <summary>
  10. /// This class defines an abstract Event from which all custom Events inherit
  11. /// </summary>
  12. /// <typeparam name="T">The type of Event, has to inherit from this abstract class</typeparam>
  13. public abstract class Event<T> where T : Event<T>
  14. {
  15. // description that this event type has fired, mostly used to debug
  16. public string description;
  17. // true when an unique event has already fired, to prevent loops
  18. private bool hasFired;
  19. // signature Event listeners have to match
  20. public delegate void EventListener(T info);
  21. // contains all subscribed listeners
  22. private static event EventListener Listeners;
  23. /// <summary>
  24. /// Adds a method with fitting signature as listener to Listeners
  25. /// </summary>
  26. /// <param name="listener">The method with fitting signature that should be called when the Event fires</param>
  27. public static void RegisterListener(EventListener listener)
  28. {
  29. Listeners += listener;
  30. }
  31. /// <summary>
  32. /// Removes a method with fitting signature as listener to Listeners
  33. /// </summary>
  34. /// <param name="listener">The method that should be removed as listener to that Event</param>
  35. public static void UnregisterListener(EventListener listener)
  36. {
  37. Listeners -= listener;
  38. }
  39. /// <summary>
  40. /// Calls all registered listeners with child-specific info
  41. /// </summary>
  42. public void FireEvent()
  43. {
  44. if (hasFired)
  45. {
  46. throw new Exception("This event has already fired, to prevent infinite loops you can't refire an event");
  47. }
  48. hasFired = true;
  49. if (Listeners != null)
  50. {
  51. Listeners(this as T);
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// This event is for debug purposes, unused verbosityLevel indicates importance of event
  57. /// </summary>
  58. public class DebugEvent : Event<DebugEvent>
  59. {
  60. public int verbosityLevel;
  61. }
  62. /// <summary>
  63. /// This event should fire when a new target gets instantiated
  64. /// </summary>
  65. public class TargetSpawnedEvent : Event<TargetSpawnedEvent>
  66. {
  67. // The id (number of targets thus far starting with zero) of this target
  68. public int targetId;
  69. // The transform object of this target
  70. public Transform targetTransform;
  71. }
  72. /// <summary>
  73. /// This event should fire when a target is locked in and thus reached/destroyed
  74. /// </summary>
  75. public class TargetReachedEvent : Event<TargetReachedEvent>
  76. {
  77. // The id (number of targets thus far starting with zero) of this target
  78. public int targetId;
  79. // The transform object of this target
  80. public Transform targetTransform;
  81. // The transform object of the player
  82. // TODO: check if transform should be replaced with Vector3 to avoid distortion through player movement
  83. public Transform playerTransform;
  84. // Realtime from target instantiation to lock in
  85. public float time;
  86. }
  87. /// <summary>
  88. /// This event should fire when the current experiment has finished
  89. /// </summary>
  90. public class ExperimentEndEvent : Event<ExperimentEndEvent>
  91. {
  92. // empty for now
  93. }
  94. /// <summary>
  95. /// This event should fire after a teleportation was executed
  96. /// </summary>
  97. public class TeleportEvent : Event<TeleportEvent>
  98. {
  99. // Realtime on initiating the teleport by holding a controller button
  100. public float startTeleportTime;
  101. // Realtime when the teleport is finished and the user is in the new position
  102. public float endTeleportTime;
  103. // Coordinates of the player when initiating the teleport
  104. public Vector3 startUserPosition;
  105. // Rotation of the player when initiating the teleport
  106. public Vector3 startUserRotation;
  107. // Coordinates of the player when the teleport is finished
  108. public Vector3 endUserPosition;
  109. // Rotation of the player when the teleport is finished
  110. public Vector3 endUserRotation;
  111. }