EntityTrigger.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreRoot.h>
  17. class Entity;
  18. /**
  19. * An entity trigger.
  20. */
  21. class EntityTrigger{
  22. public:
  23. /**
  24. * Constructor.
  25. *
  26. * @param[in] name The trigger name.
  27. */
  28. EntityTrigger(const Ogre::String& name);
  29. /**
  30. * Destructor.
  31. */
  32. virtual ~EntityTrigger();
  33. /**
  34. * Updates the trigger state with debug information.
  35. */
  36. void UpdateDebug();
  37. /**
  38. * Retrieves the trigger name.
  39. *
  40. * @return The trigger name.
  41. */
  42. const Ogre::String& GetName() const;
  43. /**
  44. * Enables or disables the trigger.
  45. *
  46. * When disabled, all the activators are removed.
  47. *
  48. * @param[in] enabled True to enable the trigger, false to disable it.
  49. */
  50. void SetEnabled(const bool enabled);
  51. /**
  52. * Checks if the trigger is enabled.
  53. *
  54. * @return True if it's enabled, false otherwise.
  55. */
  56. bool IsEnabled() const;
  57. /**
  58. * Sets the vertices of the line that acts as the trigger.
  59. *
  60. * @param[in] point1 One of the vertices of the line.
  61. * @param[in] point2 One of the vertices of the line.
  62. */
  63. void SetPoints(const Ogre::Vector3& point1, const Ogre::Vector3& point2);
  64. /**
  65. * Retrieves the first vertex of the line that acts as trigger.
  66. *
  67. * @return The first vertex.
  68. */
  69. const Ogre::Vector3& GetPoint1() const;
  70. /**
  71. * Retrieves the second vertex of the line that acts as trigger.
  72. *
  73. * @return The second vertex.
  74. */
  75. const Ogre::Vector3& GetPoint2() const;
  76. /**
  77. * Check if the line has been approached.
  78. *
  79. * @return True if the playable entity solid radius has collided with the line and has not
  80. * existed yet, false otherwise.
  81. */
  82. const bool CheckApproached();
  83. /**
  84. * Sets if the the playable entity has approached the line.
  85. *
  86. * Must be set to true when the playable entity's solid radius touches the line while
  87. * manually moving, and to false when the playable entity is not near the line.
  88. *
  89. * @param[in] entered If the line has been approached.
  90. */
  91. void SetApproached(const bool approached);
  92. /**
  93. * Check if the line has been crossed.
  94. *
  95. * @return True if the playable entity's center has collided with the line and has not
  96. * existed yet (exit as in the line being outside the entity solid radius), false otherwise.
  97. */
  98. const bool CheckCrossed();
  99. /**
  100. * Sets if the the playable entity has crossed the line.
  101. *
  102. * Must be set to true when the playable entity's center touches the line while
  103. * manually moving, and to false when the playable entity is not near the line.
  104. *
  105. * @param[in] crossed If the line has been approached.
  106. */
  107. void SetCrossed(const bool crossed);
  108. /**
  109. * Checks if the on_near_once event has been fired.
  110. *
  111. * @return True if the on_near_once event has been fired and the entity has not yet exited
  112. * the line (exit as in the line being outside the entity solid radius), and false if the
  113. * playable entity has approached the line but the event has not been triggered yet.
  114. */
  115. const bool CheckNearSingleEventTriggered();
  116. /**
  117. * Sets if the on_near_once have been triggered.
  118. *
  119. * Must be set to true when it's run, and to false when the playable entity is not near the
  120. * line.
  121. *
  122. * @param[in] triggered If the on_near_once has been triggered.
  123. */
  124. void SetNearSingleEventTriggered(const bool triggered);
  125. /**
  126. * Checks if the cooldown that prevents caling the on_near event multiple times has ended.
  127. *
  128. * After the on_near script is run, a cooltime of a number of frames is set before running
  129. * it again. This is because most of the time this trigger checks for a key, and if its
  130. * pressed, is run multiple times.
  131. *
  132. * @return The number of frames remaining in the cooldown. If it's 0, the on_near event can
  133. * be called again. If not, {@see DecreaseNearEventCooldown} must be called.
  134. */
  135. const int GetNearEventCooldown();
  136. /**
  137. * Decreases the cooldown time required between calls to on_near event by one.
  138. *
  139. * After the on_near script is run, a cooltime of a number of frames is set before running
  140. * it again. This is because most of the time this trigger checks for a key, and if its
  141. * pressed, is run multiple times.
  142. */
  143. void DecreaseNearEventCooldown();
  144. /**
  145. * Resets the cooldown time required between calls to on_near event.
  146. *
  147. * After the on_near script is run, a cooltime of a number of frames is set before running
  148. * it again. This is because most of the time this trigger checks for a key, and if its
  149. * pressed, is run multiple times.
  150. *
  151. * Must be called after firing the on_near event. After calling this, the cooldown will be
  152. * reset, and all frames must be waited before calling it again.
  153. */
  154. void ResetNearEventCooldown();
  155. /**
  156. * Sets the cooldown time required between calls to on_near event to 0.
  157. *
  158. * After the on_near script is run, a cooltime of a number of frames is set before running
  159. * it again. This is because most of the time this trigger checks for a key, and if its
  160. * pressed, is run multiple times.
  161. *
  162. * Must be called whent the playable entity gets away from the line
  163. */
  164. void ClearNearEventCooldown();
  165. /**
  166. * Clears the proximity statuses and event triggering statuses.
  167. *
  168. * Can be called once the playable entity leaves the line.
  169. */
  170. void Clear();
  171. protected:
  172. /**
  173. * The name of the trigger.
  174. */
  175. Ogre::String name_;
  176. /**
  177. * Indicates if the trigger is enabled or disabled.
  178. */
  179. bool enabled_;
  180. /**
  181. * One of the vertices of the trigger line.
  182. */
  183. Ogre::Vector3 point_1_;
  184. /**
  185. * One of the vertices of the trigger line.
  186. */
  187. Ogre::Vector3 point_2_;
  188. private:
  189. /**
  190. * Frames to cooldown after triggering on_near event
  191. *
  192. * After the on_near script is run, a cooltime of a number of frames is set before running
  193. * it again. This is because most of the time this trigger checks for a key, and if its
  194. * pressed, is run multiple times.
  195. */
  196. static int NEAR_EVENT_COOLDOWN_FRAMES;
  197. /**
  198. * Indicates if the playable entity has approached the line.
  199. */
  200. bool approached_;
  201. /**
  202. * Indicates if the playable entity has crossed the line.
  203. */
  204. bool crossed_;
  205. /**
  206. * Indicates if the near_once event has been triggered.
  207. */
  208. bool near_single_event_triggered_;
  209. /**
  210. * Remaining frames to cooldown after triggering on_near event
  211. *
  212. * After the on_near script is run, a cooltime of a number of frames is set before running
  213. * it again. This is because most of the time this trigger checks for a key, and if its
  214. * pressed, is run multiple times.
  215. */
  216. int near_event_cooldown_;
  217. };