Entity.h

00001 #ifndef ERIS_ENTITY_H
00002 #define ERIS_ENTITY_H
00003 
00004 #include <Eris/Types.h>
00005 #include <Eris/Router.h>
00006 
00007 #include <Atlas/Message/Element.h>
00008 #include <Atlas/Objects/ObjectsFwd.h>
00009 
00010 #include <wfmath/point.h>
00011 #include <wfmath/vector.h>
00012 #include <wfmath/axisbox.h>
00013 #include <wfmath/quaternion.h>
00014 #include <wfmath/timestamp.h>
00015 
00016 #include <sigc++/trackable.h>
00017 #include <sigc++/slot.h>
00018 #include <sigc++/signal.h>
00019 #include <sigc++/connection.h>
00020 
00021 namespace Eris {
00022 
00023 // Forward Declerations 
00024 class Entity;
00025 class TypeInfo;
00026 class View;
00027 class EntityRouter;
00028 class Task;
00029 
00030 typedef std::vector<Entity*> EntityArray;
00031 typedef std::vector<Task*> TaskArray;
00032 
00034 
00044 class Entity : virtual public sigc::trackable
00045 {
00046 public: 
00047     typedef std::map<std::string, Atlas::Message::Element> AttrMap;
00048     
00049     explicit Entity(const std::string& id, TypeInfo* ty, View* vw);
00050     virtual ~Entity();
00051 
00052     virtual void shutdown();
00053 
00054 // heirarchy interface    
00055     unsigned int numContained() const {
00056         return m_contents.size();
00057     }
00058     Entity* getContained(unsigned int index) const {
00059         return m_contents[index];
00060     }
00061 
00062     const Atlas::Message::Element& valueOfAttr(const std::string& attr) const;
00063         
00064     bool hasAttr(const std::string &p) const;
00065 
00066     typedef sigc::slot<void, const std::string&, const Atlas::Message::Element&> AttrChangedSlot;
00067 
00070     sigc::connection observe(const std::string& attr, const AttrChangedSlot& aslot);
00071 
00072 // accesors
00074     const std::string& getId() const
00075     {
00076         return m_id;
00077     }
00078     
00079     const std::string& getName() const
00080     {
00081         return m_name;
00082     }
00083         
00085     float getStamp() const
00086     {
00087         return m_stamp;
00088     }
00089 
00090     TypeInfo* getType() const
00091     {
00092         return m_type;
00093     }
00094     
00095     View* getView() const
00096     {
00097         return m_view;
00098     }
00099     
00101     Entity* getLocation() const
00102     {
00103         return m_location;
00104     }
00105         
00107     WFMath::Point<3> getPosition() const
00108     {
00109         return m_position;
00110     }
00111     
00112     inline const AttrMap& getAttributes() const {return m_attrs;}
00113     
00115     bool isMoving() const;
00116         
00122     WFMath::Point<3> getPredictedPos() const;
00123     
00128     WFMath::Vector<3> getPredictedVelocity() const;   
00129     
00131     WFMath::Point<3> getViewPosition() const;
00132 
00134     WFMath::Quaternion getViewOrientation() const;
00135         
00137         const WFMath::Vector< 3 > & getVelocity(void) const
00138         {
00139                 return m_velocity;
00140         }
00141         
00143         const WFMath::Quaternion & getOrientation(void) const
00144         {
00145                 return m_orientation;
00146         }
00147         
00149         const WFMath::AxisBox< 3 > & getBBox(void) const
00150         {
00151                 return m_bbox;
00152         }
00153 
00154     bool hasBBox() const
00155     {
00156         return m_hasBBox;
00157     }
00158     
00159     const TaskArray& getTasks() const
00160     { return m_tasks; }
00161     
00167     TypeInfoArray getUseOperations() const;
00168     
00169     bool hasChild(const std::string& eid) const;
00170     
00172     bool isVisible() const;
00173 
00174 // coordinate transformations
00175     template<class C>
00176     C toLocationCoords(const C& c) const
00177     {
00178         return c.toParentCoords(getPredictedPos(), m_orientation);
00179     }
00180     
00181     template<class C>
00182     C fromLocationCoords(const C& c) const
00183     {
00184         return c.toLocalCoords(getPredictedPos(), m_orientation);
00185     }
00186     
00187     // A vector (e.g., the distance between two points, or
00188     // a velocity) gets rotated by a coordinate transformation,
00189     // but doesn't get shifted by the change in the position
00190     // of the origin, so we handle it separately. We also
00191     // need to copy the vector before rotating, because
00192     // Vector::rotate() rotates it in place.
00193     WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const
00194     {
00195         return WFMath::Vector<3>(v).rotate(m_orientation);
00196     }
00197     
00198     WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const
00199     {
00200         return WFMath::Vector<3>(v).rotate(m_orientation.inverse());
00201     }
00202         
00203 // Signals
00204     sigc::signal<void, Entity*> ChildAdded;
00205     sigc::signal<void, Entity*> ChildRemoved;
00206     
00208 
00212     sigc::signal<void, Entity*> LocationChanged;
00213 
00216     sigc::signal<void, const StringSet&> Changed;
00217 
00221     sigc::signal<void> Moved;
00222 
00225     sigc::signal<void, bool> Moving;
00226 
00238     sigc::signal< void, const Atlas::Objects::Root & > Say;
00239         
00244     sigc::signal<void, const std::string&> Emote;
00245     
00251     sigc::signal<void, const Atlas::Objects::Operation::RootOperation&> Acted;
00252     
00258     sigc::signal<void, const Atlas::Objects::Root&> Noise;
00259     
00260     sigc::signal<void, bool> VisibilityChanged;
00261     
00267     sigc::signal<void> BeingDeleted;
00268     
00269     sigc::signal<void, Task*> TaskAdded;
00270     sigc::signal<void, Task*> TaskRemoved;
00271 protected:              
00275     virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp);
00276     
00280     virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk);
00281 
00282     virtual void onAttrChanged(const std::string& attr, const Atlas::Message::Element &v);
00283         
00284     virtual void onLocationChanged(Entity* oldLoc);
00285     
00288     virtual void onMoved();
00289     
00293     virtual void onVisibilityChanged(bool vis);
00294 
00299     virtual void onAction(const Atlas::Objects::Operation::RootOperation& act);
00300 
00305     virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op);
00306 
00311     virtual void onImaginary(const Atlas::Objects::Root& act);
00312 
00318     virtual void setMoving(bool moving);
00319     
00324     virtual void onChildAdded(Entity* child);
00325     
00330     virtual void onChildRemoved(Entity* child);
00331 
00332 private:
00333     friend class IGRouter;
00334     friend class View;
00335     friend class EntityRouter;
00336     friend class Task;
00337     
00340     void setLocationFromAtlas(const std::string& locId);
00341       
00346     void sight(const Atlas::Objects::Entity::RootEntity& gent);
00347     
00352     void setFromRoot(const Atlas::Objects::Root& obj, bool allowMotion);
00353     
00356     void setVisible(bool vis);
00357     
00358     void setAttr(const std::string &p, const Atlas::Message::Element &v);       
00359         
00364     bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v);
00365     
00366     void beginUpdate();
00367     void addToUpdate(const std::string& attr);
00368     void endUpdate();
00369     
00373     void setLocation(Entity* newLocation);
00374     
00377     void setContentsFromAtlas(const StringList& contents);
00378     
00383     void filterMoveAttrs(Atlas::Message::MapType& attrs) const;
00384 
00385     typedef std::map<std::string, Entity*> IdEntityMap;
00386     void buildEntityDictFromContents(IdEntityMap& dict);
00387     
00388     void addChild(Entity* e);
00389     void removeChild(Entity* e);
00390 
00391     void addToLocation();
00392     void removeFromLocation();
00393 
00394     void updateTasks(const Atlas::Message::Element& e);
00395     void removeTask(Task* t);
00396 
00399     void updateCalculatedVisibility(bool wasVisible);
00400         
00401     class DynamicState
00402     {
00403     public:
00404         WFMath::Point<3> position;
00405         WFMath::Vector<3> velocity;
00406     };
00407     
00408     void updatePredictedState(const WFMath::TimeStamp& t);
00409     
00410     void createAlarmExpired();
00411     
00412     AttrMap m_attrs;
00413     
00414     TypeInfo* m_type;
00415     
00416 // primary state, in native form
00417     Entity* m_location; 
00418     EntityArray m_contents;
00419     
00420     const std::string m_id;     
00421     std::string m_name;         
00422     float m_stamp;              
00423     std::string m_description;
00424     EntityRouter* m_router;
00425     bool m_visible;
00426     bool m_limbo;   
00427     
00428     WFMath::AxisBox<3> m_bbox;
00429     WFMath::Point<3> m_position;
00430     WFMath::Vector<3> m_velocity;
00431     WFMath::Quaternion m_orientation;    
00432     WFMath::Vector<3> m_acc;
00433     
00434     DynamicState m_predicted;
00435     
00436 // extra state and state tracking things
00440     int m_updateLevel;
00441 
00446     StringSet m_modifiedAttrs;
00447         
00448     typedef sigc::signal<void, const std::string&, const Atlas::Message::Element&> AttrChangedSignal;
00449         
00450     typedef std::map<std::string, AttrChangedSignal> ObserverMap;
00451     ObserverMap m_observers;
00452         
00453     View* m_view;   
00454     
00458     bool m_hasBBox;
00459     
00460     WFMath::TimeStamp m_lastMoveTime;
00461     bool m_moving; 
00462     
00463     bool m_recentlyCreated; 
00464     
00465     TaskArray m_tasks;
00466 
00467     bool m_initialised;
00468 };
00469 
00470 } // of namespace
00471 
00472 #endif

Generated on Sun Aug 19 18:51:26 2007 for Eris by  doxygen 1.5.2