staticShape.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "core/dnet.h"
  24. #include "core/stream/bitStream.h"
  25. #include "app/game.h"
  26. #include "math/mMath.h"
  27. #include "console/simBase.h"
  28. #include "console/console.h"
  29. #include "console/consoleTypes.h"
  30. #include "console/engineAPI.h"
  31. #include "T3D/gameBase/moveManager.h"
  32. #include "ts/tsShapeInstance.h"
  33. #include "T3D/staticShape.h"
  34. #include "math/mathIO.h"
  35. #include "sim/netConnection.h"
  36. #include "scene/sceneObjectLightingPlugin.h"
  37. extern void wireCube(F32 size,Point3F pos);
  38. static const U32 sgAllowedDynamicTypes = 0xffffff;
  39. //----------------------------------------------------------------------------
  40. IMPLEMENT_CO_DATABLOCK_V1(StaticShapeData);
  41. ConsoleDocClass( StaticShapeData,
  42. "@brief The most basic ShapeBaseData derrived shape datablock available in Torque 3D.\n\n"
  43. "When it comes to placing 3D objects in the scene, you effectively have two options:\n\n"
  44. "1. TSStatic objects\n\n"
  45. "2. ShapeBase derived objects\n\n"
  46. "Since ShapeBase and ShapeBaseData are not meant to be instantiated in script, you "
  47. "will use one of its child classes instead. Several game related objects are derived "
  48. "from ShapeBase: Player, Vehicle, Item, and so on.\n\n"
  49. "When you need a 3D object with datablock capabilities, you will use an object derived "
  50. "from ShapeBase. When you need an object with extremely low overhead, and with no other "
  51. "purpose than to be a 3D object in the scene, you will use TSStatic.\n\n"
  52. "The most basic child of ShapeBase is StaticShape. It does not introduce any of the "
  53. "additional functionality you see in Player, Item, Vehicle or the other game play "
  54. "heavy classes. At its core, it is comparable to a TSStatic, but with a datbalock. Having "
  55. "a datablock provides a location for common variables as well as having access to "
  56. "various ShapeBaseData, GameBaseData and SimDataBlock callbacks.\n\n"
  57. "@tsexample\n"
  58. "// Create a StaticShape using a datablock\n"
  59. "datablock StaticShapeData(BasicShapeData)\n"
  60. "{\n"
  61. " shapeFile = \"art/shapes/items/kit/healthkit.dts\";\n"
  62. " testVar = \"Simple string, not a stock variable\";\n"
  63. "};\n\n"
  64. "new StaticShape()\n"
  65. "{\n"
  66. " dataBlock = \"BasicShapeData\";\n"
  67. " position = \"0.0 0.0 0.0\";\n"
  68. " rotation = \"1 0 0 0\";\n"
  69. " scale = \"1 1 1\";\n"
  70. "};\n"
  71. "@endtsexample\n\n"
  72. "@see StaticShape\n"
  73. "@see ShapeBaseData\n"
  74. "@see TSStatic\n\n"
  75. "@ingroup gameObjects\n"
  76. "@ingroup Datablocks");
  77. StaticShapeData::StaticShapeData()
  78. {
  79. dynamicTypeField = 0;
  80. shadowEnable = true;
  81. noIndividualDamage = false;
  82. }
  83. void StaticShapeData::initPersistFields()
  84. {
  85. addField("noIndividualDamage", TypeBool, Offset(noIndividualDamage, StaticShapeData), "Deprecated\n\n @internal");
  86. addField("dynamicType", TypeS32, Offset(dynamicTypeField, StaticShapeData),
  87. "@brief An integer value which, if speficied, is added to the value retured by getType().\n\n"
  88. "This allows you to extend the type mask for a StaticShape that uses this datablock. Type masks "
  89. "are used for container queries, etc.");
  90. Parent::initPersistFields();
  91. }
  92. void StaticShapeData::packData(BitStream* stream)
  93. {
  94. Parent::packData(stream);
  95. stream->writeFlag(noIndividualDamage);
  96. stream->write(dynamicTypeField);
  97. }
  98. void StaticShapeData::unpackData(BitStream* stream)
  99. {
  100. Parent::unpackData(stream);
  101. noIndividualDamage = stream->readFlag();
  102. stream->read(&dynamicTypeField);
  103. }
  104. //----------------------------------------------------------------------------
  105. IMPLEMENT_CO_NETOBJECT_V1(StaticShape);
  106. ConsoleDocClass( StaticShape,
  107. "@brief The most basic 3D shape with a datablock available in Torque 3D.\n\n"
  108. "When it comes to placing 3D objects in the scene, you technically have two options:\n\n"
  109. "1. TSStatic objects\n\n"
  110. "2. ShapeBase derived objects\n\n"
  111. "Since ShapeBase and ShapeBaseData are not meant to be instantiated in script, you "
  112. "will use one of its child classes instead. Several game related objects are derived "
  113. "from ShapeBase: Player, Vehicle, Item, and so on.\n\n"
  114. "When you need a 3D object with datablock capabilities, you will use an object derived "
  115. "from ShapeBase. When you need an object with extremely low overhead, and with no other "
  116. "purpose than to be a 3D object in the scene, you will use TSStatic.\n\n"
  117. "The most basic child of ShapeBase is StaticShape. It does not introduce any of the "
  118. "additional functionality you see in Player, Item, Vehicle or the other game play "
  119. "heavy classes. At its core, it is comparable to a TSStatic, but with a datbalock. Having "
  120. "a datablock provides a location for common variables as well as having access to "
  121. "various ShapeBaseData, GameBaseData and SimDataBlock callbacks.\n\n"
  122. "@tsexample\n"
  123. "// Create a StaticShape using a datablock\n"
  124. "datablock StaticShapeData(BasicShapeData)\n"
  125. "{\n"
  126. " shapeFile = \"art/shapes/items/kit/healthkit.dts\";\n"
  127. " testVar = \"Simple string, not a stock variable\";\n"
  128. "};\n\n"
  129. "new StaticShape()\n"
  130. "{\n"
  131. " dataBlock = \"BasicShapeData\";\n"
  132. " position = \"0.0 0.0 0.0\";\n"
  133. " rotation = \"1 0 0 0\";\n"
  134. " scale = \"1 1 1\";\n"
  135. "};\n"
  136. "@endtsexample\n\n"
  137. "@see StaticShapeData\n"
  138. "@see ShapeBase\n"
  139. "@see TSStatic\n\n"
  140. "@ingroup gameObjects\n");
  141. StaticShape::StaticShape()
  142. {
  143. mTypeMask |= StaticShapeObjectType | StaticObjectType;
  144. mDataBlock = 0;
  145. }
  146. StaticShape::~StaticShape()
  147. {
  148. }
  149. //----------------------------------------------------------------------------
  150. void StaticShape::initPersistFields()
  151. {
  152. Parent::initPersistFields();
  153. }
  154. bool StaticShape::onAdd()
  155. {
  156. if(!Parent::onAdd() || !mDataBlock)
  157. return false;
  158. // We need to modify our type mask based on what our datablock says...
  159. mTypeMask |= (mDataBlock->dynamicTypeField & sgAllowedDynamicTypes);
  160. addToScene();
  161. if (isServerObject())
  162. scriptOnAdd();
  163. return true;
  164. }
  165. bool StaticShape::onNewDataBlock(GameBaseData* dptr, bool reload)
  166. {
  167. mDataBlock = dynamic_cast<StaticShapeData*>(dptr);
  168. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  169. return false;
  170. scriptOnNewDataBlock();
  171. return true;
  172. }
  173. void StaticShape::onRemove()
  174. {
  175. scriptOnRemove();
  176. removeFromScene();
  177. Parent::onRemove();
  178. }
  179. //----------------------------------------------------------------------------
  180. void StaticShape::processTick(const Move* move)
  181. {
  182. Parent::processTick(move);
  183. // Image Triggers
  184. if (move && mDamageState == Enabled) {
  185. setImageTriggerState(0,move->trigger[0]);
  186. setImageTriggerState(1,move->trigger[1]);
  187. }
  188. if (isMounted()) {
  189. MatrixF mat;
  190. mMount.object->getMountTransform( mMount.node, mMount.xfm, &mat );
  191. Parent::setTransform(mat);
  192. Parent::setRenderTransform(mat);
  193. }
  194. }
  195. void StaticShape::interpolateTick(F32 delta)
  196. {
  197. if (isMounted()) {
  198. MatrixF mat;
  199. mMount.object->getRenderMountTransform( delta, mMount.node, mMount.xfm, &mat );
  200. Parent::setRenderTransform(mat);
  201. }
  202. }
  203. void StaticShape::setTransform(const MatrixF& mat)
  204. {
  205. Parent::setTransform(mat);
  206. setMaskBits(PositionMask);
  207. }
  208. void StaticShape::onUnmount(ShapeBase*,S32)
  209. {
  210. // Make sure the client get's the final server pos.
  211. setMaskBits(PositionMask);
  212. }
  213. //----------------------------------------------------------------------------
  214. U32 StaticShape::packUpdate(NetConnection *connection, U32 mask, BitStream *bstream)
  215. {
  216. U32 retMask = Parent::packUpdate(connection,mask,bstream);
  217. if (bstream->writeFlag(mask & PositionMask | ExtendedInfoMask))
  218. {
  219. // Write the transform (do _not_ use writeAffineTransform. Since this is a static
  220. // object, the transform must be RIGHT THE *&)*$&^ ON or it will goof up the
  221. // synchronization between the client and the server.
  222. mathWrite(*bstream,mObjToWorld);
  223. mathWrite(*bstream, mObjScale);
  224. }
  225. // powered?
  226. bstream->writeFlag(mPowered);
  227. if (mLightPlugin)
  228. {
  229. retMask |= mLightPlugin->packUpdate(this, ExtendedInfoMask, connection, mask, bstream);
  230. }
  231. return retMask;
  232. }
  233. void StaticShape::unpackUpdate(NetConnection *connection, BitStream *bstream)
  234. {
  235. Parent::unpackUpdate(connection,bstream);
  236. if (bstream->readFlag())
  237. {
  238. MatrixF mat;
  239. mathRead(*bstream,&mat);
  240. Parent::setTransform(mat);
  241. Parent::setRenderTransform(mat);
  242. VectorF scale;
  243. mathRead(*bstream, &scale);
  244. setScale(scale);
  245. }
  246. // powered?
  247. mPowered = bstream->readFlag();
  248. if (mLightPlugin)
  249. {
  250. mLightPlugin->unpackUpdate(this, connection, bstream);
  251. }
  252. }
  253. //----------------------------------------------------------------------------
  254. // This appears to be legacy T2 stuff
  255. // Marked internal, as this is flagged to be deleted
  256. // [8/1/2010 mperry]
  257. DefineConsoleMethod( StaticShape, setPoweredState, void, (bool isPowered), , "(bool isPowered)"
  258. "@internal")
  259. {
  260. if(!object->isServerObject())
  261. return;
  262. object->setPowered(isPowered);
  263. }
  264. DefineConsoleMethod( StaticShape, getPoweredState, bool, (), , "@internal")
  265. {
  266. if(!object->isServerObject())
  267. return(false);
  268. return(object->isPowered());
  269. }