entity.h 8.9 KB

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