entity.h 8.5 KB

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