entity.h 8.4 KB

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