gamelibcomponents.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #ifdef COMPONENT_SYSTEM
  2. //Generic components from GameLib (script side of c++ classes)
  3. typedef int[] IEntityComponentSource;
  4. class IEntityComponentSource: BaseContainer
  5. {
  6. };
  7. //!Entity touch event types
  8. enum TouchEvent
  9. {
  10. ON_ENTER,
  11. ON_STAY,
  12. ON_EXIT
  13. };
  14. //!Builtin component types
  15. //TypeID MeshObjectTypeID;
  16. //TypeID HierarchyTypeID;
  17. //TypeID RigidBodyTypeID;
  18. //TypeID SphereGeometryTypeID;
  19. //TypeID BoxGeometryTypeID;
  20. class GenericComponent : Managed
  21. {
  22. /**
  23. * Gets current eventmask of the component.
  24. * \return Returns bitmask of events the component accepts
  25. */
  26. proto native int GetEventMask();
  27. /**
  28. * Sets eventmask. Component accepts only events which has set bits in eventmask.
  29. * Bits are or'ed with existing bitmask. See enf::EntityEvents.
  30. * When this method is called in the constructor of the component, it will not properly set the eventmask to the parent entity. You may consider OnComponentInsert event.
  31. * \param mask Mask of those bits, which will be set.
  32. * \return Returns bitmask of events the component accepts (result of mask|GetEventMask())
  33. */
  34. proto native int SetEventMask(IEntity owner, int mask);
  35. /**
  36. * Clears bitmask. Component accepts only events which has set bits in eventmask.
  37. * Only bits set in the mask are cleared. See enf::EntityEvents
  38. * \param mask Mask of those bits, which will be cleared.
  39. * \return returns these bits that were set before and now are cleared.
  40. */
  41. proto native int ClearEventMask(IEntity owner, int mask);
  42. /**
  43. * Activate component and calls EOnActivate().
  44. */
  45. proto native void Activate(IEntity owner);
  46. /**
  47. * Deactivate component and calls EOnDectivate().
  48. */
  49. proto native void Deactivate(IEntity owner);
  50. /**
  51. * Returns activity state.
  52. * \return Returns true, if component is active.
  53. */
  54. proto native bool IsActive();
  55. //! Constructor
  56. protected void GenericComponent(IEntityComponentSource src, IEntity ent);
  57. }
  58. class GenericComponentClass
  59. {
  60. bool DependsOn(typename otherClass, TypeID otherTypeID) {}
  61. }
  62. /**
  63. * Parent class for all components created in script.
  64. * Every ScriptComponent is being created in Entity's constructor and may receive following events
  65. * 1. OnComponentInsert is being called when component is created. This is last event Workbench sends in World Editor edit mode.
  66. * 2. EOnInit is being called after all components have been inserted and if the component registered event mask EV_INIT
  67. * 3. EOnActivate is being called if the entity was flagged as TFL_ACTIVE and if the component is active. The component is active by default.
  68. * 4. EOnXXXs are being called base on event mask component registered
  69. * 5. EOnDeactivate is being called when Deactivate is called or when the component is being to be removed. Component must be active to be deactivated.
  70. * 6. OnComponentRemove is being called after a component is removed.
  71. * 7. EOnDelete is being called after entity is going to be destroyed.
  72. */
  73. class ScriptComponent : GenericComponent
  74. {
  75. /*
  76. Event when owner entity is touched
  77. \param owner
  78. Touched entity
  79. \param extra
  80. Bitmask of touch types TODO
  81. */
  82. protected void EOnTouch(IEntity owner, int extra);
  83. /*!
  84. Event after component is initialized. At this point all entity's components have recieved OnComponentInsert.
  85. \param owner
  86. \param extra
  87. Number of entity
  88. */
  89. protected void EOnInit(IEntity owner, int extra);
  90. /*!
  91. Extra event of various functional extensions. ATM it's used
  92. by Trigger when some insider is leaving
  93. \param owner
  94. owner actor of event
  95. \param extra
  96. Extra value of event
  97. */
  98. protected void EOnExtra(IEntity owner, int extra);
  99. /*!
  100. Event when we are out of visibility
  101. \param owner
  102. \param extra
  103. Frame number
  104. */
  105. protected void EOnNotVisible(IEntity owner, int extra);
  106. /*!
  107. Event when we are visible
  108. \param owner
  109. \param extra
  110. Frame number
  111. */
  112. protected void EOnVisible(IEntity owner, int extra);
  113. /*!
  114. Event every frame
  115. \param owner
  116. \param timeSlice
  117. Time passed since last frame
  118. */
  119. protected void EOnFrame(IEntity owner, float timeSlice);
  120. /*!
  121. Even after physics update
  122. \param owner
  123. \param extra
  124. Frame number
  125. */
  126. protected void EOnPostFrame(IEntity owner, int extra);
  127. /*!
  128. Event from animation system
  129. \param owner
  130. \param extra
  131. extra data
  132. */
  133. protected void EOnAnimEvent(IEntity owner, AnimEvent extra);
  134. /*!
  135. Event from sound system
  136. \param owner
  137. \param extra
  138. extra data
  139. */
  140. protected void EOnSoundEvent(IEntity owner, SoundEvent extra);
  141. /*!
  142. Event after simulated by physics engine (once per frame)
  143. \param owner
  144. \param timeslice
  145. Time slice of simulation step
  146. */
  147. protected void EOnPostSimulate(IEntity owner, float timeslice);
  148. /*!
  149. Event before simulated by physics engine (called from sub-iterations!
  150. \param owner
  151. \param timeslice
  152. Time slice of simulation step
  153. */
  154. protected void EOnSimulate(IEntity owner, float timeslice);
  155. /*!
  156. Event when joint attached to RigidBody of this entity is broken
  157. \param owner
  158. owner Entity attached to the joint
  159. \param extra
  160. Not used ATM
  161. */
  162. protected void EOnJointBreak(IEntity owner, int extra);
  163. /*!
  164. Event when physics engine has moved with this Entity
  165. \param owner
  166. World Entity
  167. \param extra
  168. Not used ATM
  169. */
  170. protected void EOnPhysicsMove(IEntity owner, int extra);
  171. /*!
  172. Event when physics engine registered contact with owner RigidBody
  173. \param owner
  174. \param contact
  175. Structure describing the contact
  176. */
  177. protected void EOnContact(IEntity owner, Contact extra);
  178. /**
  179. * Event when component is activated.
  180. */
  181. protected void EOnActivate(IEntity owner);
  182. /**
  183. * Event when component is deactivated.
  184. */
  185. protected void EOnDeactivate(IEntity owner);
  186. /*!
  187. Event when a component is created and added to Entity.
  188. \param owner Entity into which component is added
  189. \param other Component which is being added into Entity
  190. */
  191. protected void OnComponentInsert(IEntity owner, ScriptComponent other);
  192. /*!
  193. Event when a component is being removed from Entity.
  194. \param owner Entity from which component is being removed
  195. \param other Component which is being removed from the Entity
  196. */
  197. protected void OnComponentRemove(IEntity owner, ScriptComponent other);
  198. /*!
  199. Called when Entity is being to be destroyed (deleted) or component to be deleted (see Game::DeleteScriptComponent).
  200. \param owner Entity which owns the component
  201. */
  202. protected void OnDelete(IEntity owner);
  203. }
  204. typedef int[] SoundHandle;
  205. class SignalInput
  206. {
  207. string m_name;
  208. float m_value;
  209. void SignalInput()
  210. {
  211. m_value = 0;
  212. }
  213. };
  214. class BaseSoundComponent : GenericComponent
  215. {
  216. /* Get list of 'events'. */
  217. proto native int GetEventNames(out array<string> events);
  218. /* Get list of 'signals. '*/
  219. proto native int GetSignalNames(out array<string> signals);
  220. /* Convert signal name to index. */
  221. proto native int GetSignalIndex(string name);
  222. /* Set signal value by 'name'. */
  223. proto native void SetSignalValueName(string signal, float value);
  224. /* Set signal value by 'index'. */
  225. proto native void SetSignalValue(int index, float value);
  226. /* Play 'event'. */
  227. proto native SoundHandle Play(string name);
  228. /* Play sounds based on triggers. */
  229. proto native SoundHandle Update();
  230. /* Terminate 'sound'. */
  231. proto native void Terminate(SoundHandle handle);
  232. /* Check if 'sound' is played. */
  233. proto native bool IsPlayed(SoundHandle handle);
  234. /* Validate handle. */
  235. proto native bool IsHandleValid(SoundHandle handle);
  236. /* Set sound position. */
  237. proto native void SetTransform(vector[] transf);
  238. /* Enable debug mode. */
  239. proto native void SetDebug(bool value);
  240. };
  241. #endif