afxEA_ScriptEvent.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include <typeinfo>
  25. #include "afx/arcaneFX.h"
  26. #include "afx/afxEffectDefs.h"
  27. #include "afx/afxEffectWrapper.h"
  28. #include "afx/afxChoreographer.h"
  29. #include "afx/ce/afxScriptEvent.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxEA_ScriptEvent
  32. class afxEA_ScriptEvent : public afxEffectWrapper
  33. {
  34. typedef afxEffectWrapper Parent;
  35. afxScriptEventData* script_data;
  36. bool ran_script;
  37. void do_runtime_substitutions();
  38. public:
  39. /*C*/ afxEA_ScriptEvent();
  40. /*D*/ ~afxEA_ScriptEvent();
  41. virtual bool isDone() { return ran_script; }
  42. virtual void ea_set_datablock(SimDataBlock*);
  43. virtual bool ea_start();
  44. virtual bool ea_update(F32 dt);
  45. virtual void ea_finish(bool was_stopped);
  46. };
  47. //~~~~~~~~~~~~~~~~~~~~//
  48. afxEA_ScriptEvent::afxEA_ScriptEvent()
  49. {
  50. script_data = 0;
  51. ran_script = false;
  52. }
  53. afxEA_ScriptEvent::~afxEA_ScriptEvent()
  54. {
  55. if (script_data && script_data->isTempClone())
  56. delete script_data;
  57. script_data = 0;
  58. }
  59. void afxEA_ScriptEvent::ea_set_datablock(SimDataBlock* db)
  60. {
  61. script_data = dynamic_cast<afxScriptEventData*>(db);
  62. }
  63. bool afxEA_ScriptEvent::ea_start()
  64. {
  65. if (!script_data)
  66. {
  67. Con::errorf("afxEA_ScriptEvent::ea_start() -- missing or incompatible datablock.");
  68. return false;
  69. }
  70. do_runtime_substitutions();
  71. ran_script = (script_data->method_name == ST_NULLSTRING);
  72. return true;
  73. }
  74. bool afxEA_ScriptEvent::ea_update(F32 dt)
  75. {
  76. if (!ran_script && mChoreographer != NULL)
  77. {
  78. afxConstraint* pos_constraint = getPosConstraint();
  79. mChoreographer->executeScriptEvent(script_data->method_name, pos_constraint, mUpdated_xfm,
  80. script_data->script_data);
  81. ran_script = true;
  82. }
  83. return true;
  84. }
  85. void afxEA_ScriptEvent::ea_finish(bool was_stopped)
  86. {
  87. ran_script = false;
  88. }
  89. void afxEA_ScriptEvent::do_runtime_substitutions()
  90. {
  91. // only clone the datablock if there are substitutions
  92. if (script_data->getSubstitutionCount() > 0)
  93. {
  94. // clone the datablock and perform substitutions
  95. afxScriptEventData* orig_db = script_data;
  96. script_data = new afxScriptEventData(*orig_db, true);
  97. orig_db->performSubstitutions(script_data, mChoreographer, mGroup_index);
  98. }
  99. }
  100. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  101. class afxEA_ScriptEventDesc : public afxEffectAdapterDesc, public afxEffectDefs
  102. {
  103. static afxEA_ScriptEventDesc desc;
  104. public:
  105. virtual bool testEffectType(const SimDataBlock*) const;
  106. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const { return false; }
  107. virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
  108. virtual bool runsOnClient(const afxEffectWrapperData*) const { return false; }
  109. virtual bool isPositional(const afxEffectWrapperData*) const { return false; }
  110. virtual afxEffectWrapper* create() const { return new afxEA_ScriptEvent; }
  111. };
  112. afxEA_ScriptEventDesc afxEA_ScriptEventDesc::desc;
  113. bool afxEA_ScriptEventDesc::testEffectType(const SimDataBlock* db) const
  114. {
  115. return (typeid(afxScriptEventData) == typeid(*db));
  116. }
  117. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//