scriptObjects.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/simBase.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/scriptObjects.h"
  26. #include "console/engineAPI.h"
  27. //-----------------------------------------------------------------------------
  28. // ScriptObject
  29. //-----------------------------------------------------------------------------
  30. IMPLEMENT_CONOBJECT(ScriptObject);
  31. ConsoleDocClass( ScriptObject,
  32. "@brief A script-level OOP object which allows binding of a class, superClass and arguments along with declaration of methods.\n\n"
  33. "ScriptObjects are extrodinarily powerful objects that allow defining of any type of data required. They can optionally have\n"
  34. "a class and a superclass defined for added control of multiple ScriptObjects through a simple class definition.\n\n"
  35. "@tsexample\n"
  36. "new ScriptObject(Game)\n"
  37. "{\n"
  38. " class = \"DeathMatchGame\";\n"
  39. " superClass = GameCore;\n"
  40. " genre = \"Action FPS\"; // Note the new, non-Torque variable\n"
  41. "};\n"
  42. "@endtsexample\n"
  43. "@see SimObject\n"
  44. "@ingroup Console\n"
  45. "@ingroup Scripting"
  46. );
  47. IMPLEMENT_CALLBACK( ScriptObject, onAdd, void, ( SimObjectId ID ), ( ID ),
  48. "Called when this ScriptObject is added to the system.\n"
  49. "@param ID Unique object ID assigned when created (%this in script).\n"
  50. );
  51. IMPLEMENT_CALLBACK( ScriptObject, onRemove, void, ( SimObjectId ID ), ( ID ),
  52. "Called when this ScriptObject is removed from the system.\n"
  53. "@param ID Unique object ID assigned when created (%this in script).\n"
  54. );
  55. ScriptObject::ScriptObject()
  56. {
  57. }
  58. bool ScriptObject::onAdd()
  59. {
  60. if (!Parent::onAdd())
  61. return false;
  62. // Call onAdd in script!
  63. onAdd_callback(getId());
  64. return true;
  65. }
  66. void ScriptObject::onRemove()
  67. {
  68. // We call this on this objects namespace so we unlink them after. - jdd
  69. //
  70. // Call onRemove in script!
  71. onRemove_callback(getId());
  72. Parent::onRemove();
  73. }
  74. //-----------------------------------------------------------------------------
  75. // ScriptTickObject
  76. //-----------------------------------------------------------------------------
  77. IMPLEMENT_CONOBJECT(ScriptTickObject);
  78. ConsoleDocClass( ScriptTickObject,
  79. "@brief A ScriptObject that responds to tick and frame events.\n\n"
  80. "ScriptTickObject is a ScriptObject that adds callbacks for tick and frame events. Use "
  81. "setProcessTicks() to enable or disable the onInterpolateTick() and onProcessTick() callbacks. "
  82. "The callOnAdvanceTime property determines if the onAdvanceTime() callback is called.\n\n"
  83. "@see ScriptObject\n"
  84. "@ingroup Console\n"
  85. "@ingroup Scripting"
  86. );
  87. IMPLEMENT_CALLBACK( ScriptTickObject, onInterpolateTick, void, ( F32 delta ), ( delta ),
  88. "This is called every frame, but only if the object is set to process ticks.\n"
  89. "@param delta The time delta for this frame.\n"
  90. );
  91. IMPLEMENT_CALLBACK( ScriptTickObject, onProcessTick, void, (), (),
  92. "Called once every 32ms if this object is set to process ticks.\n"
  93. );
  94. IMPLEMENT_CALLBACK( ScriptTickObject, onAdvanceTime, void, ( F32 timeDelta ), ( timeDelta ),
  95. "This is called every frame regardless if the object is set to process ticks, but only "
  96. "if the callOnAdvanceTime property is set to true.\n"
  97. "@param timeDelta The time delta for this frame.\n"
  98. "@see callOnAdvanceTime\n"
  99. );
  100. ScriptTickObject::ScriptTickObject()
  101. {
  102. mCallOnAdvanceTime = false;
  103. }
  104. void ScriptTickObject::initPersistFields()
  105. {
  106. addField("callOnAdvanceTime", TypeBool, Offset(mCallOnAdvanceTime, ScriptTickObject), "Call the onAdvaceTime() callback.");
  107. Parent::initPersistFields();
  108. }
  109. bool ScriptTickObject::onAdd()
  110. {
  111. if (!Parent::onAdd())
  112. return false;
  113. return true;
  114. }
  115. void ScriptTickObject::onRemove()
  116. {
  117. Parent::onRemove();
  118. }
  119. void ScriptTickObject::interpolateTick( F32 delta )
  120. {
  121. onInterpolateTick_callback(delta);
  122. }
  123. void ScriptTickObject::processTick()
  124. {
  125. onProcessTick_callback();
  126. }
  127. void ScriptTickObject::advanceTime( F32 timeDelta )
  128. {
  129. if(mCallOnAdvanceTime)
  130. {
  131. onAdvanceTime_callback(timeDelta);
  132. }
  133. }
  134. DefineEngineMethod( ScriptTickObject, setProcessTicks, void, ( bool tick ),,
  135. "@brief Sets this object as either tick processing or not.\n\n"
  136. "@param tick This object's onInterpolateTick() and onProcessTick() callbacks are called if set to true.\n\n")
  137. {
  138. object->setProcessTicks(tick);
  139. }
  140. DefineEngineMethod( ScriptTickObject, isProcessingTicks, bool, ( ),,
  141. "@brief Is this object wanting to receive tick notifications.\n\n"
  142. "If this object is set to receive tick notifications then its onInterpolateTick() and "
  143. "onProcessTick() callbacks are called.\n"
  144. "@return True if object wants tick notifications\n\n" )
  145. {
  146. return object->isProcessingTicks();
  147. }
  148. //-----------------------------------------------------------------------------
  149. // ScriptGroup
  150. //-----------------------------------------------------------------------------
  151. IMPLEMENT_CONOBJECT(ScriptGroup);
  152. ConsoleDocClass( ScriptGroup,
  153. "@brief Essentially a SimGroup, but with onAdd and onRemove script callbacks.\n\n"
  154. "@tsexample\n"
  155. "// First container, SimGroup containing a ScriptGroup\n"
  156. "new SimGroup(Scenes)\n"
  157. "{\n"
  158. " // Subcontainer, ScriptGroup containing variables\n"
  159. " // related to a cut scene and a starting WayPoint\n"
  160. " new ScriptGroup(WelcomeScene)\n"
  161. " {\n"
  162. " class = \"Scene\";\n"
  163. " pathName = \"Pathx\";\n"
  164. " description = \"A small orc village set in the Hardesty mountains. This town and its surroundings will be used to illustrate some the Torque Game Engine\'s features.\";\n"
  165. " pathTime = \"0\";\n"
  166. " title = \"Welcome to Orc Town\";\n\n"
  167. " new WayPoint(start)\n"
  168. " {\n"
  169. " position = \"163.873 -103.82 208.354\";\n"
  170. " rotation = \"0.136165 -0.0544916 0.989186 44.0527\";\n"
  171. " scale = \"1 1 1\";\n"
  172. " dataBlock = \"WayPointMarker\";\n"
  173. " team = \"0\";\n"
  174. " };\n"
  175. " };\n"
  176. "};\n"
  177. "@endtsexample\n\n"
  178. "@see SimGroup\n"
  179. "@ingroup Console\n"
  180. "@ingroup Scripting"
  181. );
  182. ScriptGroup::ScriptGroup()
  183. {
  184. }
  185. IMPLEMENT_CALLBACK( ScriptGroup, onAdd, void, ( SimObjectId ID ), ( ID ),
  186. "Called when this ScriptGroup is added to the system.\n"
  187. "@param ID Unique object ID assigned when created (%this in script).\n"
  188. );
  189. IMPLEMENT_CALLBACK( ScriptGroup, onRemove, void, ( SimObjectId ID ), ( ID ),
  190. "Called when this ScriptObject is removed from the system.\n"
  191. "@param ID Unique object ID assigned when created (%this in script).\n"
  192. );
  193. bool ScriptGroup::onAdd()
  194. {
  195. if (!Parent::onAdd())
  196. return false;
  197. // Call onAdd in script!
  198. onAdd_callback(getId());
  199. return true;
  200. }
  201. void ScriptGroup::onRemove()
  202. {
  203. // Call onRemove in script!
  204. onRemove_callback(getId());
  205. Parent::onRemove();
  206. }