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 
00029 typedef std::vector<Entity*> EntityArray;
00030 
00032 
00042 class Entity : virtual public sigc::trackable
00043 {
00044 public: 
00045     typedef std::map<std::string, Atlas::Message::Element> AttrMap;
00046     
00047     explicit Entity(const std::string& id, TypeInfo* ty, View* vw);
00048     virtual ~Entity();
00049 
00050 // heirarchy interface    
00051     unsigned int numContained() const {
00052         return m_contents.size();
00053     }
00054     Entity* getContained(unsigned int index) const {
00055         return m_contents[index];
00056     }
00057 
00058     const Atlas::Message::Element& valueOfAttr(const std::string& attr) const;
00059         
00060     bool hasAttr(const std::string &p) const;
00061 
00062     typedef sigc::slot<void, const std::string&, const Atlas::Message::Element&> AttrChangedSlot;
00063 
00066     sigc::connection observe(const std::string& attr, const AttrChangedSlot& aslot);
00067 
00068 // accesors
00070     const std::string& getId() const
00071     {
00072         return m_id;
00073     }
00074     
00075     const std::string& getName() const
00076     {
00077         return m_name;
00078     }
00079         
00081     float getStamp() const
00082     {
00083         return m_stamp;
00084     }
00085 
00086     TypeInfo* getType() const
00087     {
00088         return m_type;
00089     }
00090     
00091     View* getView() const
00092     {
00093         return m_view;
00094     }
00095     
00097     Entity* getLocation() const
00098     {
00099         return m_location;
00100     }
00101         
00103     WFMath::Point<3> getPosition() const
00104     {
00105         return m_position;
00106     }
00107     
00108     inline const AttrMap& getAttributes() const {return m_attrs;}
00109     
00111     bool isMoving() const;
00112         
00118     WFMath::Point<3> getPredictedPos() const;
00119     
00124     WFMath::Vector<3> getPredictedVelocity() const;   
00125     
00127     WFMath::Point<3> getViewPosition() const;
00128 
00130     WFMath::Quaternion getViewOrientation() const;
00131         
00133         const WFMath::Vector< 3 > & getVelocity(void) const
00134         {
00135                 return m_velocity;
00136         }
00137         
00139         const WFMath::Quaternion & getOrientation(void) const
00140         {
00141                 return m_orientation;
00142         }
00143         
00145         const WFMath::AxisBox< 3 > & getBBox(void) const
00146         {
00147                 return m_bbox;
00148         }
00149 
00150     bool hasBBox() const
00151     {
00152         return m_hasBBox;
00153     }
00154     
00155     bool hasChild(const std::string& eid) const;
00156     
00158     bool isVisible() const;
00159 
00160 // coordinate transformations
00161     template<class C>
00162     C toLocationCoords(const C& c) const
00163     {
00164         return c.toParentCoords(getPredictedPos(), m_orientation);
00165     }
00166     
00167     template<class C>
00168     C fromLocationCoords(const C& c) const
00169     {
00170         return c.toLocalCoords(getPredictedPos(), m_orientation);
00171     }
00172     
00173     // A vector (e.g., the distance between two points, or
00174     // a velocity) gets rotated by a coordinate transformation,
00175     // but doesn't get shifted by the change in the position
00176     // of the origin, so we handle it separately. We also
00177     // need to copy the vector before rotating, because
00178     // Vector::rotate() rotates it in place.
00179     WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const
00180     {
00181         return WFMath::Vector<3>(v).rotate(m_orientation);
00182     }
00183     
00184     WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const
00185     {
00186         return WFMath::Vector<3>(v).rotate(m_orientation.inverse());
00187     }
00188         
00189 // Signals
00190     sigc::signal<void, Entity*> ChildAdded;
00191     sigc::signal<void, Entity*> ChildRemoved;
00192     
00194 
00198     sigc::signal<void, Entity*> LocationChanged;
00199 
00202     sigc::signal<void, const StringSet&> Changed;
00203 
00207     sigc::signal<void> Moved;
00208 
00211     sigc::signal<void, bool> Moving;
00212 
00224     sigc::signal< void, const Atlas::Objects::Root & > Say;
00225         
00230     sigc::signal<void, const std::string&> Emote;
00231     
00237     sigc::signal<void, const Atlas::Objects::Operation::RootOperation&> Acted;
00238     
00244     sigc::signal<void, const Atlas::Objects::Root&> Noise;
00245     
00246     sigc::signal<void, bool> VisibilityChanged;
00247     
00253     sigc::signal<void> BeingDeleted;
00254 protected:              
00258     virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp);
00259     
00263     virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk);
00264 
00265     virtual void onAttrChanged(const std::string& attr, const Atlas::Message::Element &v);
00266         
00267     virtual void onLocationChanged(Entity* oldLoc);
00268     
00271     virtual void onMoved();
00272     
00276     virtual void onVisibilityChanged(bool vis);
00277 
00282     virtual void onAction(const Atlas::Objects::Operation::RootOperation& act);
00283 
00288     virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op);
00289 
00294     virtual void onImaginary(const Atlas::Objects::Root& act);
00295 
00301     virtual void setMoving(bool moving);
00302     
00307     virtual void onChildAdded(Entity* child);
00308     
00313     virtual void onChildRemoved(Entity* child);
00314 
00315 private:
00316     friend class IGRouter;
00317     friend class View;
00318     friend class EntityRouter;
00319     
00322     void setLocationFromAtlas(const std::string& locId);
00323       
00328     void sight(const Atlas::Objects::Entity::RootEntity& gent);
00329     
00334     void setFromRoot(const Atlas::Objects::Root& obj, bool allowMotion);
00335     
00338     void setVisible(bool vis);
00339     
00340     void setAttr(const std::string &p, const Atlas::Message::Element &v);       
00341         
00346     bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v);
00347     
00348     void beginUpdate();
00349     void addToUpdate(const std::string& attr);
00350     void endUpdate();
00351     
00355     void setLocation(Entity* newLocation);
00356     
00359     void setContentsFromAtlas(const StringList& contents);
00360     
00365     void filterMoveAttrs(Atlas::Message::MapType& attrs) const;
00366 
00367     typedef std::map<std::string, Entity*> IdEntityMap;
00368     void buildEntityDictFromContents(IdEntityMap& dict);
00369     
00370     void addChild(Entity* e);
00371     void removeChild(Entity* e);
00372 
00373     void addToLocation();
00374     void removeFromLocation();
00375 
00378     void updateCalculatedVisibility(bool wasVisible);
00379         
00380     class DynamicState
00381     {
00382     public:
00383         WFMath::Point<3> position;
00384         WFMath::Vector<3> velocity;
00385     };
00386     
00387     void updatePredictedState(const WFMath::TimeStamp& t);
00388     
00389     void createAlarmExpired();
00390     
00391     AttrMap m_attrs;
00392     
00393     TypeInfo* m_type;
00394     
00395 // primary state, in native form
00396     Entity* m_location; 
00397     EntityArray m_contents;
00398     
00399     const std::string m_id;     
00400     std::string m_name;         
00401     float m_stamp;              
00402     std::string m_description;
00403     EntityRouter* m_router;
00404     bool m_visible;
00405     bool m_limbo;   
00406     
00407     WFMath::AxisBox<3> m_bbox;
00408     WFMath::Point<3> m_position;
00409     WFMath::Vector<3> m_velocity;
00410     WFMath::Quaternion m_orientation;    
00411     WFMath::Vector<3> m_acc;
00412     
00413     DynamicState m_predicted;
00414     
00415 // extra state and state tracking things
00419     int m_updateLevel;
00420 
00425     StringSet m_modifiedAttrs;
00426         
00427     typedef sigc::signal<void, const std::string&, const Atlas::Message::Element&> AttrChangedSignal;
00428         
00429     typedef std::map<std::string, AttrChangedSignal> ObserverMap;
00430     ObserverMap m_observers;
00431         
00432     View* m_view;   
00433     
00437     bool m_hasBBox;
00438     
00439     WFMath::TimeStamp m_lastMoveTime;
00440     bool m_moving; 
00441     
00442     bool m_recentlyCreated; 
00443 };
00444 
00445 } // of namespace
00446 
00447 #endif

Generated on Tue Feb 7 13:20:24 2006 for Eris by  doxygen 1.4.6