afxEA_Damage.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/afxDamage.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxEA_Damage
  32. class afxEA_Damage : public afxEffectWrapper
  33. {
  34. typedef afxEffectWrapper Parent;
  35. afxDamageData* damage_data;
  36. bool started;
  37. U8 repeat_cnt;
  38. U32 dot_delta_ms;
  39. U32 next_dot_time;
  40. Point3F impact_pos;
  41. SimObjectId impacted_obj_id;
  42. void do_runtime_substitutions();
  43. public:
  44. /*C*/ afxEA_Damage();
  45. /*C*/ ~afxEA_Damage();
  46. virtual bool isDone();
  47. virtual void ea_set_datablock(SimDataBlock*);
  48. virtual bool ea_start();
  49. virtual bool ea_update(F32 dt);
  50. virtual void ea_finish(bool was_stopped);
  51. };
  52. //~~~~~~~~~~~~~~~~~~~~//
  53. afxEA_Damage::afxEA_Damage()
  54. {
  55. damage_data = 0;
  56. started = false;
  57. repeat_cnt = 0;
  58. dot_delta_ms = 0;
  59. next_dot_time = 0;
  60. impact_pos.zero();
  61. impacted_obj_id = 0;
  62. }
  63. afxEA_Damage::~afxEA_Damage()
  64. {
  65. if (damage_data && damage_data->isTempClone())
  66. delete damage_data;
  67. damage_data = 0;
  68. }
  69. bool afxEA_Damage::isDone()
  70. {
  71. return (damage_data) ? (repeat_cnt >= damage_data->repeats) : true;
  72. }
  73. void afxEA_Damage::ea_set_datablock(SimDataBlock* db)
  74. {
  75. damage_data = dynamic_cast<afxDamageData*>(db);
  76. }
  77. bool afxEA_Damage::ea_start()
  78. {
  79. if (!damage_data)
  80. {
  81. Con::errorf("afxEA_Damage::ea_start() -- missing or incompatible datablock.");
  82. return false;
  83. }
  84. do_runtime_substitutions();
  85. if (damage_data->repeats > 1)
  86. {
  87. dot_delta_ms = mFull_lifetime /(damage_data->repeats - 1);
  88. next_dot_time = dot_delta_ms;
  89. }
  90. return true;
  91. }
  92. bool afxEA_Damage::ea_update(F32 dt)
  93. {
  94. if (!started)
  95. {
  96. started = true;
  97. afxConstraint* pos_cons = getPosConstraint();
  98. if (pos_cons)
  99. pos_cons->getPosition(impact_pos);
  100. afxConstraint* aim_cons = getAimConstraint();
  101. if (aim_cons && aim_cons->getSceneObject())
  102. impacted_obj_id = aim_cons->getSceneObject()->getId();
  103. if (mChoreographer)
  104. mChoreographer->inflictDamage(damage_data->label, damage_data->flavor, impacted_obj_id, damage_data->amount,
  105. repeat_cnt, damage_data->ad_amount, damage_data->radius, impact_pos,
  106. damage_data->impulse);
  107. repeat_cnt++;
  108. }
  109. else if (repeat_cnt < damage_data->repeats)
  110. {
  111. if (next_dot_time <= mLife_elapsed)
  112. {
  113. // CONSTRAINT REMAPPING <<
  114. afxConstraint* aim_cons = getAimConstraint();
  115. if (aim_cons && aim_cons->getSceneObject())
  116. impacted_obj_id = aim_cons->getSceneObject()->getId();
  117. // CONSTRAINT REMAPPING >>
  118. if (mChoreographer)
  119. mChoreographer->inflictDamage(damage_data->label, damage_data->flavor, impacted_obj_id, damage_data->amount,
  120. repeat_cnt, 0, 0, impact_pos, 0);
  121. next_dot_time += dot_delta_ms;
  122. repeat_cnt++;
  123. }
  124. }
  125. return true;
  126. }
  127. void afxEA_Damage::ea_finish(bool was_stopped)
  128. {
  129. if (started && (repeat_cnt < damage_data->repeats))
  130. {
  131. if (next_dot_time <= mLife_elapsed)
  132. {
  133. if (mChoreographer)
  134. mChoreographer->inflictDamage(damage_data->label, damage_data->flavor, impacted_obj_id, damage_data->amount,
  135. repeat_cnt, 0, 0, impact_pos, 0);
  136. }
  137. }
  138. started = false;
  139. }
  140. void afxEA_Damage::do_runtime_substitutions()
  141. {
  142. // only clone the datablock if there are substitutions
  143. if (damage_data->getSubstitutionCount() > 0)
  144. {
  145. // clone the datablock and perform substitutions
  146. afxDamageData* orig_db = damage_data;
  147. damage_data = new afxDamageData(*orig_db, true);
  148. orig_db->performSubstitutions(damage_data, mChoreographer, mGroup_index);
  149. }
  150. }
  151. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  152. class afxEA_DamageDesc : public afxEffectAdapterDesc, public afxEffectDefs
  153. {
  154. static afxEA_DamageDesc desc;
  155. public:
  156. virtual bool testEffectType(const SimDataBlock*) const;
  157. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const { return false; }
  158. virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
  159. virtual bool runsOnClient(const afxEffectWrapperData*) const { return false; }
  160. virtual afxEffectWrapper* create() const { return new afxEA_Damage; }
  161. };
  162. afxEA_DamageDesc afxEA_DamageDesc::desc;
  163. bool afxEA_DamageDesc::testEffectType(const SimDataBlock* db) const
  164. {
  165. return (typeid(afxDamageData) == typeid(*db));
  166. }
  167. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//