afxMachineGun.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "afx/arcaneFX.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/stream/bitStream.h"
  27. #include "lighting/lightInfo.h"
  28. #include "T3D/projectile.h"
  29. #include "afx/ce/afxMachineGun.h"
  30. #include "console/typeValidators.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxMachineGunData
  33. IMPLEMENT_CO_DATABLOCK_V1(afxMachineGunData);
  34. ConsoleDocClass( afxMachineGunData,
  35. "@brief A datablock that specifies a Machine Gun effect.\n\n"
  36. "Machine Gun is a simple but useful effect for rapidly shooting standard Torque Projectile objects. For performance "
  37. "reasons, keep in mind that each bullet is a separate Projectile object, which is not a very lightweight object."
  38. "\n\n"
  39. "@ingroup afxEffects\n"
  40. "@ingroup AFX\n"
  41. "@ingroup Datablocks\n"
  42. );
  43. afxMachineGunData::afxMachineGunData()
  44. {
  45. projectile_data = 0;
  46. rounds_per_minute = 60;
  47. }
  48. afxMachineGunData::afxMachineGunData(const afxMachineGunData& other, bool temp_clone) : GameBaseData(other, temp_clone)
  49. {
  50. projectile_data = other.projectile_data;
  51. rounds_per_minute = other.rounds_per_minute;
  52. }
  53. #define myOffset(field) Offset(field, afxMachineGunData)
  54. void afxMachineGunData::initPersistFields()
  55. {
  56. docsURL;
  57. addField("projectile", TYPEID<ProjectileData>(), myOffset(projectile_data),
  58. "A ProjectileData datablock describing the projectile to be launched.");
  59. addFieldV("roundsPerMinute", TypeRangedS32, myOffset(rounds_per_minute), &CommonValidators::PositiveInt,
  60. "Specifies the number of projectiles fired over a minute of time. A value of 1200 "
  61. "will create 20 projectiles per second.\n"
  62. "Sample values for real machine guns:\n"
  63. " AK-47 = 600, M16 = 750-900, UZI = 600");
  64. Parent::initPersistFields();
  65. // disallow some field substitutions
  66. disableFieldSubstitutions("projectile");
  67. }
  68. bool afxMachineGunData::onAdd()
  69. {
  70. if (Parent::onAdd() == false)
  71. return false;
  72. if (projectile_data)
  73. {
  74. if (getId() >= DataBlockObjectIdFirst && getId() <= DataBlockObjectIdLast)
  75. {
  76. SimObjectId pid = projectile_data->getId();
  77. if (pid < DataBlockObjectIdFirst || pid > DataBlockObjectIdLast)
  78. {
  79. Con::errorf(ConsoleLogEntry::General,"afxMachineGunData: bad ProjectileData datablock.");
  80. return false;
  81. }
  82. }
  83. }
  84. return true;
  85. }
  86. void afxMachineGunData::packData(BitStream* stream)
  87. {
  88. Parent::packData(stream);
  89. if (stream->writeFlag(projectile_data))
  90. stream->writeRangedU32(projectile_data->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast);
  91. stream->write(rounds_per_minute);
  92. }
  93. void afxMachineGunData::unpackData(BitStream* stream)
  94. {
  95. Parent::unpackData(stream);
  96. if (stream->readFlag())
  97. {
  98. SimObjectId id = stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast);
  99. Sim::findObject(id, projectile_data);
  100. }
  101. stream->read(&rounds_per_minute);
  102. }
  103. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//