00001 #ifndef ERIS_ENTITY_H
00002 #define ERIS_ENTITY_H
00003
00004 #include <Eris/Types.h>
00005
00006 #include <Atlas/Objects/ObjectsFwd.h>
00007
00008 #include <wfmath/point.h>
00009 #include <wfmath/vector.h>
00010 #include <wfmath/axisbox.h>
00011 #include <wfmath/quaternion.h>
00012 #include <wfmath/timestamp.h>
00013
00014 #include <sigc++/trackable.h>
00015 #include <sigc++/slot.h>
00016 #include <sigc++/signal.h>
00017 #include <sigc++/connection.h>
00018
00019 #include <map>
00020 #include <vector>
00021
00022 namespace Atlas {
00023 namespace Message {
00024 class Element;
00025 typedef std::map<std::string, Element> MapType;
00026 }
00027 }
00028
00029 namespace Eris {
00030
00031
00032 class Entity;
00033 class TypeInfo;
00034 class View;
00035 class EntityRouter;
00036 class Task;
00037
00038 typedef std::vector<Entity*> EntityArray;
00039 typedef std::vector<Task*> TaskArray;
00040 typedef std::vector<TypeInfoPtr> TypeInfoArray;
00041
00057 class Entity : virtual public sigc::trackable
00058 {
00059 public:
00060 typedef std::map<std::string, Atlas::Message::Element> AttrMap;
00061
00062 explicit Entity(const std::string& id, TypeInfo* ty, View* vw);
00063 virtual ~Entity();
00064
00070 virtual void shutdown();
00071
00072
00078 unsigned int numContained() const;
00079
00085 Entity* getContained(unsigned int index) const;
00086
00094 const Atlas::Message::Element& valueOfAttr(const std::string& attr) const;
00095
00101 bool hasAttr(const std::string &p) const;
00102
00106 typedef sigc::slot<void, const Atlas::Message::Element&> AttrChangedSlot;
00107
00115 sigc::connection observe(const std::string& attr, const AttrChangedSlot& aslot);
00116
00117
00122 const std::string& getId() const;
00123
00129 const std::string& getName() const;
00130
00135 float getStamp() const;
00136
00141 TypeInfo* getType() const;
00142
00147 View* getView() const;
00148
00153 Entity* getLocation() const;
00154
00159 WFMath::Point<3> getPosition() const;
00160
00168 const AttrMap getAttributes() const;
00169
00177 const AttrMap& getInstanceAttributes() const;
00178
00183 bool isMoving() const;
00184
00190 WFMath::Point<3> getPredictedPos() const;
00191
00196 WFMath::Vector<3> getPredictedVelocity() const;
00197
00199 WFMath::Point<3> getViewPosition() const;
00200
00202 WFMath::Quaternion getViewOrientation() const;
00203
00205 const WFMath::Vector< 3 > & getVelocity(void) const;
00206
00208 const WFMath::Quaternion & getOrientation(void) const;
00209
00211 const WFMath::AxisBox< 3 > & getBBox(void) const;
00212
00218 bool hasBBox() const;
00219
00224 const TaskArray& getTasks() const;
00225
00231 TypeInfoArray getUseOperations() const;
00232
00233 bool hasChild(const std::string& eid) const;
00234
00236 bool isVisible() const;
00237
00238
00239 template<class C>
00240 C toLocationCoords(const C& c) const;
00241
00242 template<class C>
00243 C fromLocationCoords(const C& c) const;
00244
00245
00246
00247
00248
00249
00250
00251 WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const;
00252
00253 WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const;
00254
00255
00256 sigc::signal<void, Entity*> ChildAdded;
00257 sigc::signal<void, Entity*> ChildRemoved;
00258
00260
00264 sigc::signal<void, Entity*> LocationChanged;
00265
00268 sigc::signal<void, const StringSet&> Changed;
00269
00273 sigc::signal<void> Moved;
00274
00277 sigc::signal<void, bool> Moving;
00278
00290 sigc::signal< void, const Atlas::Objects::Root & > Say;
00291
00296 sigc::signal<void, const std::string&> Emote;
00297
00303 sigc::signal<void, const Atlas::Objects::Operation::RootOperation&> Acted;
00304
00310 sigc::signal<void, const Atlas::Objects::Root&> Noise;
00311
00312 sigc::signal<void, bool> VisibilityChanged;
00313
00319 sigc::signal<void> BeingDeleted;
00320
00321 sigc::signal<void, Task*> TaskAdded;
00322 sigc::signal<void, Task*> TaskRemoved;
00323 protected:
00327 virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp);
00328
00332 virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk);
00333
00334 virtual void onAttrChanged(const std::string& attr, const Atlas::Message::Element &v);
00335
00336 virtual void onLocationChanged(Entity* oldLoc);
00337
00340 virtual void onMoved();
00341
00345 virtual void onVisibilityChanged(bool vis);
00346
00351 virtual void onAction(const Atlas::Objects::Operation::RootOperation& act);
00352
00357 virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op);
00358
00363 virtual void onImaginary(const Atlas::Objects::Root& act);
00364
00370 virtual void setMoving(bool moving);
00371
00376 virtual void onChildAdded(Entity* child);
00377
00382 virtual void onChildRemoved(Entity* child);
00383
00384 friend class IGRouter;
00385 friend class View;
00386 friend class EntityRouter;
00387 friend class Task;
00388
00391 void setLocationFromAtlas(const std::string& locId);
00392
00397 void sight(const Atlas::Objects::Entity::RootEntity& gent);
00398
00399
00406 void setFromRoot(const Atlas::Objects::Root& obj, bool allowMotion, bool includeTypeInfoAttributes = false);
00407
00410 void setVisible(bool vis);
00411
00412 void setAttr(const std::string &p, const Atlas::Message::Element &v);
00413
00418 bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v);
00419
00426 void typeInfo_AttributeChanges(const std::string& attributeName, const Atlas::Message::Element& element);
00427
00434 virtual void attrChangedFromTypeInfo(const std::string& attributeName, const Atlas::Message::Element& element);
00435
00436
00443 void fillAttributesFromType(Entity::AttrMap& attributes, TypeInfo* typeInfo) const;
00444
00445 void beginUpdate();
00446 void addToUpdate(const std::string& attr);
00447 void endUpdate();
00448
00452 void setLocation(Entity* newLocation);
00453
00456 void setContentsFromAtlas(const StringList& contents);
00457
00462 void filterMoveAttrs(Atlas::Message::MapType& attrs) const;
00463
00464 typedef std::map<std::string, Entity*> IdEntityMap;
00465 void buildEntityDictFromContents(IdEntityMap& dict);
00466
00467 void addChild(Entity* e);
00468 void removeChild(Entity* e);
00469
00470 void addToLocation();
00471 void removeFromLocation();
00472
00473 void updateTasks(const Atlas::Message::Element& e);
00474 void removeTask(Task* t);
00475
00478 void updateCalculatedVisibility(bool wasVisible);
00479
00480 class DynamicState
00481 {
00482 public:
00483 WFMath::Point<3> position;
00484 WFMath::Vector<3> velocity;
00485 };
00486
00487 void updatePredictedState(const WFMath::TimeStamp& t);
00488
00489 void createAlarmExpired();
00490
00491 AttrMap m_attrs;
00492
00493 TypeInfo* m_type;
00494
00495
00496 Entity* m_location;
00497 EntityArray m_contents;
00498
00499 const std::string m_id;
00500 std::string m_name;
00501 float m_stamp;
00502 std::string m_description;
00503 EntityRouter* m_router;
00504 bool m_visible;
00505 bool m_limbo;
00506
00507 WFMath::AxisBox<3> m_bbox;
00508 WFMath::Point<3> m_position;
00509 WFMath::Vector<3> m_velocity;
00510 WFMath::Quaternion m_orientation;
00511 WFMath::Vector<3> m_acc;
00512
00513 DynamicState m_predicted;
00514
00515
00519 int m_updateLevel;
00520
00525 StringSet m_modifiedAttrs;
00526
00527 typedef sigc::signal<void, const Atlas::Message::Element&> AttrChangedSignal;
00528
00529 typedef std::map<std::string, AttrChangedSignal> ObserverMap;
00530 ObserverMap m_observers;
00531
00532 View* m_view;
00533
00537 bool m_hasBBox;
00538
00539 WFMath::TimeStamp m_lastMoveTime;
00540 bool m_moving;
00541
00542 bool m_recentlyCreated;
00543
00544 TaskArray m_tasks;
00545
00546 bool m_initialised;
00547 };
00548
00549 inline unsigned int Entity::numContained() const {
00550 return m_contents.size();
00551 }
00552
00553 inline Entity* Entity::getContained(unsigned int index) const {
00554 return m_contents[index];
00555 }
00556
00557 inline const std::string& Entity::getId() const
00558 {
00559 return m_id;
00560 }
00561
00562 inline const std::string& Entity::getName() const
00563 {
00564 return m_name;
00565 }
00566
00567 inline float Entity::getStamp() const
00568 {
00569 return m_stamp;
00570 }
00571
00572 inline TypeInfo* Entity::getType() const
00573 {
00574 return m_type;
00575 }
00576
00577 inline View* Entity::getView() const
00578 {
00579 return m_view;
00580 }
00581
00583 inline Entity* Entity::getLocation() const
00584 {
00585 return m_location;
00586 }
00587
00589 inline WFMath::Point<3> Entity::getPosition() const
00590 {
00591 return m_position;
00592 }
00594 inline const WFMath::Vector< 3 > & Entity::getVelocity(void) const
00595 {
00596 return m_velocity;
00597 }
00598
00600 inline const WFMath::Quaternion & Entity::getOrientation(void) const
00601 {
00602 return m_orientation;
00603 }
00604
00606 inline const WFMath::AxisBox< 3 > & Entity::getBBox(void) const
00607 {
00608 return m_bbox;
00609 }
00610
00611 inline bool Entity::hasBBox() const
00612 {
00613 return m_hasBBox;
00614 }
00615
00616 inline const TaskArray& Entity::getTasks() const
00617 {
00618 return m_tasks;
00619 }
00620
00621 template<class C>
00622 inline C Entity::toLocationCoords(const C& c) const
00623 {
00624 return c.toParentCoords(getPredictedPos(), m_orientation);
00625 }
00626
00627 template<class C>
00628 inline C Entity::fromLocationCoords(const C& c) const
00629 {
00630 return c.toLocalCoords(getPredictedPos(), m_orientation);
00631 }
00632
00633 inline WFMath::Vector<3> Entity::toLocationCoords(const WFMath::Vector<3>& v) const
00634 {
00635 return WFMath::Vector<3>(v).rotate(m_orientation);
00636 }
00637
00638 inline WFMath::Vector<3> Entity::fromLocationCoords(const WFMath::Vector<3>& v) const
00639 {
00640 return WFMath::Vector<3>(v).rotate(m_orientation.inverse());
00641 }
00642
00643 }
00644
00645 #endif