entity.h

Engine/source/T3D/entity.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef ENTITY_H
 25#define ENTITY_H
 26
 27#ifndef _GAMEBASE_H_
 28#include "T3D/gameBase/gameBase.h"
 29#endif
 30#ifndef _MOVEMANAGER_H_
 31#include "T3D/gameBase/moveManager.h"
 32#endif
 33#ifndef COMPONENT_H
 34#include "T3D/components/component.h"
 35#endif
 36#ifndef _CONTAINERQUERY_H_
 37#include "T3D/containerQuery.h"
 38#endif
 39
 40class Component;
 41
 42//**************************************************************************
 43// Entity
 44//**************************************************************************
 45class Entity : public GameBase
 46{
 47   typedef GameBase Parent;
 48   friend class Component;
 49
 50private:
 51   Point3F             mPos;
 52   RotationF           mRot;
 53
 54   Vector<Component*>         mComponents;
 55
 56   Vector<Component*>         mToLoadComponents;
 57
 58   bool                       mStartComponentUpdate;
 59
 60   ContainerQueryInfo containerInfo;
 61
 62   bool mInitialized;
 63
 64   Signal< void(Component*) > onComponentAdded;
 65   Signal< void(Component*) > onComponentRemoved;
 66
 67   Signal< void(MatrixF*) > onTransformSet;
 68
 69protected:
 70
 71   virtual void   processTick(const Move* move);
 72   virtual void   advanceTime(F32 dt);
 73   virtual void   interpolateTick(F32 delta);
 74
 75   void prepRenderImage(SceneRenderState *state);
 76
 77   virtual bool onAdd();
 78   virtual void onRemove();
 79
 80public:
 81   struct StateDelta
 82   {
 83      Move move;                    ///< Last move from server
 84      F32 dt;                       ///< Last interpolation time
 85      // Interpolation data
 86      Point3F pos;
 87      Point3F posVec;
 88      QuatF rot[2];
 89      // Warp data
 90      S32 warpTicks;                ///< Number of ticks to warp
 91      S32 warpCount;                ///< Current pos in warp
 92      Point3F warpOffset;
 93      QuatF warpRot[2];
 94   };
 95
 96   enum MaskBits
 97   {
 98      TransformMask = Parent::NextFreeMask << 0,
 99      BoundsMask = Parent::NextFreeMask << 1,
100      ComponentsMask = Parent::NextFreeMask << 2,
101      NoWarpMask = Parent::NextFreeMask << 3,
102      NextFreeMask = Parent::NextFreeMask << 4
103   };
104
105   StateDelta mDelta;
106   S32 mPredictionCount;            ///< Number of ticks to predict
107
108   Move lastMove;
109
110   //
111   Entity();
112   ~Entity();
113
114   static void    initPersistFields();
115   virtual void onPostAdd();
116
117   virtual void setTransform(const MatrixF &mat);
118   virtual void setRenderTransform(const MatrixF &mat);
119
120   void setTransform(Point3F position, RotationF rotation);
121
122   void setRenderTransform(Point3F position, RotationF rotation);
123
124   virtual MatrixF getTransform();
125   virtual Point3F getPosition() const { return mPos; }
126
127   //void setTransform(Point3F position, RotationF rot);
128
129   //void setRotation(RotationF rotation);
130
131   void setRotation(RotationF rotation) {
132      mRot = rotation;
133      setMaskBits(TransformMask);
134   };
135   RotationF getRotation() { return mRot; }
136
137   void setMountOffset(Point3F posOffset);
138   void setMountRotation(EulerF rotOffset);
139
140   //static bool _setEulerRotation( void *object, const char *index, const char *data );
141   static bool _setPosition(void *object, const char *index, const char *data);
142   static const char * _getPosition(void* obj, const char* data);
143
144   static bool _setRotation(void *object, const char *index, const char *data);
145   static const char * _getRotation(void* obj, const char* data);
146
147   virtual void getMountTransform(S32 index, const MatrixF &xfm, MatrixF *outMat);
148   virtual void getRenderMountTransform(F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat);
149
150   void setForwardVector(VectorF newForward, VectorF upVector = VectorF::Zero);
151
152   virtual void mountObject(SceneObject *obj, S32 node, const MatrixF &xfm = MatrixF::Identity);
153   void mountObject(SceneObject* objB, MatrixF txfm);
154   void onMount(SceneObject *obj, S32 node);
155   void onUnmount(SceneObject *obj, S32 node);
156
157   // NetObject
158   U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
159   void unpackUpdate(NetConnection *conn, BitStream *stream);
160
161   void setComponentsDirty();
162   void setComponentDirty(Component *comp, bool forceUpdate = false);
163
164   //Components
165   virtual bool deferAddingComponents() const { return true; }
166
167   template <class T>
168   T* getComponent();
169   template <class T>
170   Vector<T*> getComponents();
171
172   Component* getComponent(String componentType);
173
174   U32 getComponentCount() const
175   { 
176      return mComponents.size(); 
177   }
178
179   virtual void setObjectBox(Box3F objBox);
180
181   void resetWorldBox() { Parent::resetWorldBox(); }
182   void resetObjectBox() { Parent::resetObjectBox(); }
183   void resetRenderWorldBox() { Parent::resetRenderWorldBox(); }
184
185   //function redirects for collisions
186   bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
187   bool castRayRendered(const Point3F &start, const Point3F &end, RayInfo* info);
188   bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
189   virtual void buildConvex(const Box3F& box, Convex* convex);
190
191   Signal< void(SimObject*, String, String) > onDataSet;
192   virtual void setDataField(StringTableEntry slotName, const char *array, const char *value);
193   virtual void onStaticModified(const char* slotName, const char* newValue);
194
195   //void pushEvent(const char* eventName, Vector<const char*> eventParams);
196
197   void updateContainer();
198
199   ContainerQueryInfo getContainerInfo() { return containerInfo; }
200
201   //camera stuff
202   virtual void getCameraTransform(F32* pos, MatrixF* mat);
203   virtual void onCameraScopeQuery(NetConnection* connection, CameraScopeQuery* query);
204
205   //Heirarchy stuff
206   virtual void addObject(SimObject* object);
207   virtual void removeObject(SimObject* object);
208
209   virtual SimObject* findObjectByInternalName(StringTableEntry internalName, bool searchChildren);
210
211   //component stuff
212   bool addComponent(Component *comp);
213   bool removeComponent(Component *comp, bool deleteComponent);
214   void clearComponents(bool deleteComponents = true);
215   Component* getComponent(const U32 index) const;
216
217   void onInspect();
218   void onEndInspect();
219
220   virtual void write(Stream &stream, U32 tabStop, U32 flags);
221
222   // TamlChildren
223   virtual U32 getTamlChildCount(void) const
224   {
225      U32 componentCount = getComponentCount();
226      U32 childSize = (U32)size();
227      return componentCount + childSize;
228   }
229
230   virtual SimObject* getTamlChild(const U32 childIndex) const;
231
232   virtual void addTamlChild(SimObject* pSimObject)
233   {
234      // Sanity!
235      AssertFatal(pSimObject != NULL, "SimSet::addTamlChild() - Cannot add a NULL child object.");
236
237      addObject(pSimObject);
238   }
239
240   Box3F getObjectBox() { return mObjBox; }
241   MatrixF getWorldToObj() { return mWorldToObj; }
242   MatrixF getObjToWorld() { return mObjToWorld; }
243
244   DECLARE_CONOBJECT(Entity);
245
246};
247
248template <class T>
249T *Entity::getComponent()
250{
251   U32 componentCount = getComponentCount();
252   for (U32 i = 0; i < componentCount; i++)
253   {
254      T* t = dynamic_cast<T *>(mComponents[i]);
255
256      if (t) 
257      {
258         return t;
259      }
260   }
261   return NULL;
262}
263
264template <class T>
265Vector<T*> Entity::getComponents()
266{
267   Vector<T*> foundObjects;
268
269   T *curObj;
270   Component* comp;
271
272   // Loop through our child objects.
273   for (U32 i = 0; i < mComponents.size(); i++)
274   {
275      curObj = dynamic_cast<T*>(mComponents[i]);
276
277      // Add this child object if appropriate.
278      if (curObj)
279         foundObjects.push_back(curObj);
280   }
281
282   return foundObjects;
283}
284#endif //ENTITY_H
285