entity.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #ifndef ENTITY_H
  23. #define ENTITY_H
  24. #ifndef _GAMEBASE_H_
  25. #include "T3D/gameBase/gameBase.h"
  26. #endif
  27. #ifndef _MOVEMANAGER_H_
  28. #include "T3D/gameBase/moveManager.h"
  29. #endif
  30. #ifndef COMPONENT_H
  31. #include "T3D/components/component.h"
  32. #endif
  33. #ifndef _CONTAINERQUERY_H_
  34. #include "T3D/containerQuery.h"
  35. #endif
  36. #ifndef _ASSET_PTR_H_
  37. #include "assets/assetPtr.h"
  38. #endif
  39. #ifndef GAME_OBJECT_ASSET_H
  40. #include "T3D/assets/GameObjectAsset.h"
  41. #endif
  42. class Component;
  43. //**************************************************************************
  44. // Entity
  45. //**************************************************************************
  46. class Entity : public GameBase
  47. {
  48. typedef GameBase Parent;
  49. friend class Component;
  50. private:
  51. Point3F mPos;
  52. RotationF mRot;
  53. Vector<Component*> mComponents;
  54. //Bit of helper data to let us track and manage the adding, removal and updating of networked components
  55. struct NetworkedComponent
  56. {
  57. U32 componentIndex;
  58. enum UpdateState
  59. {
  60. None,
  61. Adding,
  62. Removing,
  63. Updating
  64. };
  65. UpdateState updateState;
  66. U32 updateMaskBits;
  67. };
  68. Vector<NetworkedComponent> mNetworkedComponents;
  69. U32 mComponentNetMask;
  70. bool mStartComponentUpdate;
  71. StringTableEntry mGameObjectAssetId;
  72. AssetPtr<GameObjectAsset> mGameObjectAsset;
  73. ContainerQueryInfo containerInfo;
  74. bool mInitialized;
  75. String mTags;
  76. Signal< void(Component*) > onComponentAdded;
  77. Signal< void(Component*) > onComponentRemoved;
  78. S32 mLifetimeMS;
  79. protected:
  80. virtual void processTick(const Move* move);
  81. virtual void advanceTime(F32 dt);
  82. virtual void interpolateTick(F32 delta);
  83. void prepRenderImage(SceneRenderState *state);
  84. virtual bool onAdd();
  85. virtual void onRemove();
  86. public:
  87. struct StateDelta
  88. {
  89. Move move; ///< Last move from server
  90. F32 dt; ///< Last interpolation time
  91. // Interpolation data
  92. Point3F pos;
  93. Point3F posVec;
  94. QuatF rot[2];
  95. // Warp data
  96. S32 warpTicks; ///< Number of ticks to warp
  97. S32 warpCount; ///< Current pos in warp
  98. Point3F warpOffset;
  99. QuatF warpRot[2];
  100. };
  101. enum MaskBits
  102. {
  103. TransformMask = Parent::NextFreeMask << 0,
  104. BoundsMask = Parent::NextFreeMask << 1,
  105. ComponentsUpdateMask = Parent::NextFreeMask << 2,
  106. AddComponentsMask = Parent::NextFreeMask << 3,
  107. RemoveComponentsMask = Parent::NextFreeMask << 4,
  108. NoWarpMask = Parent::NextFreeMask << 5,
  109. NamespaceMask = Parent::NextFreeMask << 6,
  110. NextFreeMask = Parent::NextFreeMask << 7
  111. };
  112. StateDelta mDelta;
  113. S32 mPredictionCount; ///< Number of ticks to predict
  114. Move lastMove;
  115. S32 mStartTimeMS;
  116. //
  117. Entity();
  118. ~Entity();
  119. static void initPersistFields();
  120. virtual void onPostAdd();
  121. virtual void setTransform(const MatrixF &mat);
  122. virtual void setRenderTransform(const MatrixF &mat);
  123. void setTransform(const Point3F& position, const RotationF& rotation);
  124. void setRenderTransform(const Point3F& position, const RotationF& rotation);
  125. virtual MatrixF getTransform();
  126. virtual Point3F getPosition() const { return mPos; }
  127. void setRotation(const RotationF& rotation) {
  128. mRot = rotation;
  129. setMaskBits(TransformMask);
  130. };
  131. RotationF getRotation() { return mRot; }
  132. static bool _setGameObject(void *object, const char *index, const char *data);
  133. void setMountOffset(const Point3F& posOffset);
  134. void setMountRotation(const EulerF& rotOffset);
  135. //static bool _setEulerRotation( void *object, const char *index, const char *data );
  136. static bool _setPosition(void *object, const char *index, const char *data);
  137. static const char * _getPosition(void* obj, const char* data);
  138. static bool _setRotation(void *object, const char *index, const char *data);
  139. static const char * _getRotation(void* obj, const char* data);
  140. virtual void getMountTransform(S32 index, const MatrixF &xfm, MatrixF *outMat);
  141. virtual void getRenderMountTransform(F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat);
  142. virtual void mountObject(SceneObject *obj, S32 node, const MatrixF &xfm = MatrixF::Identity);
  143. void mountObject(SceneObject* objB, const MatrixF& txfm);
  144. void onMount(SceneObject *obj, S32 node);
  145. void onUnmount(SceneObject *obj, S32 node);
  146. /// Sets the client controlling this object
  147. /// @param client Client that is now controlling this object
  148. virtual void setControllingClient(GameConnection *client);
  149. //
  150. //Networking
  151. //
  152. // NetObject
  153. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  154. void unpackUpdate(NetConnection *conn, BitStream *stream);
  155. void setComponentsDirty();
  156. void setComponentDirty(Component *comp, bool forceUpdate = false);
  157. void setComponentNetMask(Component* comp, U32 mask);
  158. //Components
  159. virtual bool deferAddingComponents() const { return true; }
  160. void notifyComponents(String signalFunction, String argA, String argB, String argC, String argD, String argE);
  161. template <class T>
  162. T* getComponent();
  163. template <class T>
  164. Vector<T*> getComponents();
  165. Component* getComponent(String componentType);
  166. U32 getComponentCount() const
  167. {
  168. return mComponents.size();
  169. }
  170. virtual void setObjectBox(const Box3F& objBox);
  171. void resetWorldBox() { Parent::resetWorldBox(); }
  172. void resetObjectBox() { Parent::resetObjectBox(); }
  173. void resetRenderWorldBox() { Parent::resetRenderWorldBox(); }
  174. //function redirects for collisions
  175. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  176. bool castRayRendered(const Point3F &start, const Point3F &end, RayInfo* info);
  177. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
  178. virtual void buildConvex(const Box3F& box, Convex* convex);
  179. Signal< void(SimObject*, String, String) > onDataSet;
  180. virtual void setDataField(StringTableEntry slotName, const char *array, const char *value);
  181. virtual void onStaticModified(const char* slotName, const char* newValue);
  182. //void pushEvent(const char* eventName, Vector<const char*> eventParams);
  183. void updateContainer();
  184. ContainerQueryInfo getContainerInfo() { return containerInfo; }
  185. //camera stuff
  186. virtual void getCameraTransform(F32* pos, MatrixF* mat);
  187. virtual void onCameraScopeQuery(NetConnection* connection, CameraScopeQuery* query);
  188. //Heirarchy stuff
  189. virtual void addObject(SimObject* object);
  190. virtual void removeObject(SimObject* object);
  191. virtual SimObject* findObjectByInternalName(StringTableEntry internalName, bool searchChildren);
  192. //component stuff
  193. bool addComponent(Component *comp);
  194. bool removeComponent(Component *comp, bool deleteComponent);
  195. void clearComponents(bool deleteComponents = true);
  196. Component* getComponent(const U32 index) const;
  197. void onInspect();
  198. void onEndInspect();
  199. virtual void write(Stream &stream, U32 tabStop, U32 flags);
  200. // TamlChildren
  201. virtual U32 getTamlChildCount(void) const
  202. {
  203. U32 componentCount = getComponentCount();
  204. U32 childSize = (U32)size();
  205. return componentCount + childSize;
  206. }
  207. virtual SimObject* getTamlChild(const U32 childIndex) const;
  208. virtual void addTamlChild(SimObject* pSimObject)
  209. {
  210. // Sanity!
  211. AssertFatal(pSimObject != NULL, "SimSet::addTamlChild() - Cannot add a NULL child object.");
  212. addObject(pSimObject);
  213. }
  214. Box3F getObjectBox() { return mObjBox; }
  215. MatrixF getWorldToObj() { return mWorldToObj; }
  216. MatrixF getObjToWorld() { return mObjToWorld; }
  217. DECLARE_CONOBJECT(Entity);
  218. };
  219. template <class T>
  220. T *Entity::getComponent()
  221. {
  222. U32 componentCount = getComponentCount();
  223. for (U32 i = 0; i < componentCount; i++)
  224. {
  225. T* t = dynamic_cast<T *>(mComponents[i]);
  226. if (t)
  227. {
  228. return t;
  229. }
  230. }
  231. return NULL;
  232. }
  233. template <class T>
  234. Vector<T*> Entity::getComponents()
  235. {
  236. Vector<T*> foundObjects;
  237. T *curObj;
  238. // Loop through our child objects.
  239. for (U32 i = 0; i < mComponents.size(); i++)
  240. {
  241. curObj = dynamic_cast<T*>(mComponents[i]);
  242. // Add this child object if appropriate.
  243. if (curObj)
  244. foundObjects.push_back(curObj);
  245. }
  246. return foundObjects;
  247. }
  248. #endif //ENTITY_H