afxEA_MachineGun.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/afxProjectile.h"
  30. #include "afx/ce/afxMachineGun.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_MachineGun
  33. class afxEA_MachineGun : public afxEffectWrapper
  34. {
  35. typedef afxEffectWrapper Parent;
  36. afxMachineGunData* gun_data;
  37. ProjectileData* bullet_data;
  38. bool shooting;
  39. F32 start_time;
  40. F32 shot_gap;
  41. S32 shot_count;
  42. void launch_projectile();
  43. void do_runtime_substitutions();
  44. public:
  45. /*C*/ afxEA_MachineGun();
  46. /*D*/ ~afxEA_MachineGun();
  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_MachineGun::afxEA_MachineGun()
  54. {
  55. gun_data = 0;
  56. bullet_data = 0;
  57. shooting = false;
  58. start_time = 0.0f;
  59. shot_count = 0;
  60. shot_gap = 0.2f;
  61. }
  62. afxEA_MachineGun::~afxEA_MachineGun()
  63. {
  64. if (gun_data && gun_data->isTempClone())
  65. delete gun_data;
  66. gun_data = 0;
  67. }
  68. void afxEA_MachineGun::ea_set_datablock(SimDataBlock* db)
  69. {
  70. gun_data = dynamic_cast<afxMachineGunData*>(db);
  71. if (gun_data)
  72. bullet_data = gun_data->projectile_data;
  73. }
  74. bool afxEA_MachineGun::ea_start()
  75. {
  76. if (!gun_data)
  77. {
  78. Con::errorf("afxEA_MachineGun::ea_start() -- missing or incompatible datablock.");
  79. return false;
  80. }
  81. if (!bullet_data)
  82. {
  83. Con::errorf("afxEA_MachineGun::ea_start() -- missing or incompatible ProjectileData.");
  84. return false;
  85. }
  86. do_runtime_substitutions();
  87. if (gun_data->rounds_per_minute > 0)
  88. shot_gap = 60.0f/gun_data->rounds_per_minute;
  89. return true;
  90. }
  91. bool afxEA_MachineGun::ea_update(F32 dt)
  92. {
  93. if (!shooting)
  94. {
  95. start_time = mElapsed;
  96. shooting = true;
  97. }
  98. else
  99. {
  100. F32 next_shot = start_time + (shot_count+1)*shot_gap;
  101. while (next_shot < mElapsed)
  102. {
  103. if (mIn_scope)
  104. launch_projectile();
  105. next_shot += shot_gap;
  106. shot_count++;
  107. }
  108. }
  109. return true;
  110. }
  111. void afxEA_MachineGun::ea_finish(bool was_stopped)
  112. {
  113. }
  114. void afxEA_MachineGun::launch_projectile()
  115. {
  116. afxProjectile* projectile = new afxProjectile();
  117. ProjectileData* next_bullet = bullet_data;
  118. if (bullet_data->getSubstitutionCount() > 0)
  119. {
  120. next_bullet = new ProjectileData(*bullet_data, true);
  121. bullet_data->performSubstitutions(next_bullet, mChoreographer, mGroup_index);
  122. }
  123. projectile->onNewDataBlock(next_bullet, false);
  124. F32 muzzle_vel = next_bullet->muzzleVelocity;
  125. afxConstraint* pos_cons = getPosConstraint();
  126. ShapeBase* src_obj = (pos_cons) ? (dynamic_cast<ShapeBase*>(pos_cons->getSceneObject())) : 0;
  127. Point3F dir_vec = mUpdated_aim - mUpdated_pos;
  128. dir_vec.normalizeSafe();
  129. dir_vec *= muzzle_vel;
  130. projectile->init(mUpdated_pos, dir_vec, src_obj);
  131. if (!projectile->registerObject())
  132. {
  133. delete projectile;
  134. projectile = 0;
  135. Con::errorf("afxEA_MachineGun::launch_projectile() -- projectile failed to register.");
  136. }
  137. if (projectile)
  138. projectile->setDataField(StringTable->insert("afxOwner"), 0, mChoreographer->getIdString());
  139. }
  140. void afxEA_MachineGun::do_runtime_substitutions()
  141. {
  142. // only clone the datablock if there are substitutions
  143. if (gun_data->getSubstitutionCount() > 0)
  144. {
  145. // clone the datablock and perform substitutions
  146. afxMachineGunData* orig_db = gun_data;
  147. gun_data = new afxMachineGunData(*orig_db, true);
  148. orig_db->performSubstitutions(gun_data, mChoreographer, mGroup_index);
  149. }
  150. }
  151. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  152. class afxEA_MachineGunDesc : public afxEffectAdapterDesc, public afxEffectDefs
  153. {
  154. static afxEA_MachineGunDesc desc;
  155. public:
  156. virtual bool testEffectType(const SimDataBlock*) const;
  157. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  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_MachineGun; }
  161. };
  162. afxEA_MachineGunDesc afxEA_MachineGunDesc::desc;
  163. bool afxEA_MachineGunDesc::testEffectType(const SimDataBlock* db) const
  164. {
  165. return (typeid(afxMachineGunData) == typeid(*db));
  166. }
  167. bool afxEA_MachineGunDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  168. {
  169. return (timing.lifetime < 0);
  170. }
  171. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//