entity.h 9.7 KB

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