Eris 1.3.16
|
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 // Forward Declerations 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); 00063 virtual ~Entity(); 00064 00070 virtual void shutdown(); 00071 00072 // heirarchy interface 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 // accessors 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 Entity* getLocation() const; 00148 00153 WFMath::Point<3> getPosition() const; 00154 00162 const AttrMap getAttributes() const; 00163 00171 const AttrMap& getInstanceAttributes() const; 00172 00177 bool isMoving() const; 00178 00184 WFMath::Point<3> getPredictedPos() const; 00185 00190 WFMath::Vector<3> getPredictedVelocity() const; 00191 00193 WFMath::Point<3> getViewPosition() const; 00194 00196 WFMath::Quaternion getViewOrientation() const; 00197 00199 const WFMath::Vector< 3 > & getVelocity(void) const; 00200 00202 const WFMath::Quaternion & getOrientation(void) const; 00203 00205 const WFMath::AxisBox< 3 > & getBBox(void) const; 00206 00212 bool hasBBox() const; 00213 00218 const TaskArray& getTasks() const; 00219 00225 TypeInfoArray getUseOperations() const; 00226 00227 bool hasChild(const std::string& eid) const; 00228 00230 bool isVisible() const; 00231 00232 // coordinate transformations 00233 template<class C> 00234 C toLocationCoords(const C& c) const; 00235 00236 template<class C> 00237 C fromLocationCoords(const C& c) const; 00238 00239 // A vector (e.g., the distance between two points, or 00240 // a velocity) gets rotated by a coordinate transformation, 00241 // but doesn't get shifted by the change in the position 00242 // of the origin, so we handle it separately. We also 00243 // need to copy the vector before rotating, because 00244 // Vector::rotate() rotates it in place. 00245 WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const; 00246 00247 WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const; 00248 00249 // Signals 00250 sigc::signal<void, Entity*> ChildAdded; 00251 sigc::signal<void, Entity*> ChildRemoved; 00252 00254 00258 sigc::signal<void, Entity*> LocationChanged; 00259 00262 sigc::signal<void, const StringSet&> Changed; 00263 00267 sigc::signal<void> Moved; 00268 00271 sigc::signal<void, bool> Moving; 00272 00284 sigc::signal< void, const Atlas::Objects::Root & > Say; 00285 00290 sigc::signal<void, const std::string&> Emote; 00291 00297 sigc::signal<void, const Atlas::Objects::Operation::RootOperation&> Acted; 00298 00304 sigc::signal<void, const Atlas::Objects::Root&> Noise; 00305 00306 sigc::signal<void, bool> VisibilityChanged; 00307 00313 sigc::signal<void> BeingDeleted; 00314 00315 sigc::signal<void, Task*> TaskAdded; 00316 sigc::signal<void, Task*> TaskRemoved; 00317 protected: 00321 virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp); 00322 00326 virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk); 00327 00328 virtual void onAttrChanged(const std::string& attr, const Atlas::Message::Element &v); 00329 00330 virtual void onLocationChanged(Entity* oldLoc); 00331 00334 virtual void onMoved(); 00335 00339 virtual void onVisibilityChanged(bool vis); 00340 00345 virtual void onAction(const Atlas::Objects::Operation::RootOperation& act); 00346 00351 virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op); 00352 00357 virtual void onImaginary(const Atlas::Objects::Root& act); 00358 00364 virtual void setMoving(bool moving); 00365 00370 virtual void onChildAdded(Entity* child); 00371 00376 virtual void onChildRemoved(Entity* child); 00377 00382 virtual void onTaskAdded(Task* task); 00383 00384 friend class IGRouter; 00385 friend class View; 00386 friend class Task; 00387 00392 void sight(const Atlas::Objects::Entity::RootEntity& gent); 00393 00394 00401 void setFromRoot(const Atlas::Objects::Root& obj, bool allowMotion, bool includeTypeInfoAttributes = false); 00402 00405 void setVisible(bool vis); 00406 00407 void setAttr(const std::string &p, const Atlas::Message::Element &v); 00408 00413 bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v); 00414 00421 void typeInfo_AttributeChanges(const std::string& attributeName, const Atlas::Message::Element& element); 00422 00429 virtual void attrChangedFromTypeInfo(const std::string& attributeName, const Atlas::Message::Element& element); 00430 00431 00438 void fillAttributesFromType(Entity::AttrMap& attributes, TypeInfo* typeInfo) const; 00439 00440 void beginUpdate(); 00441 void addToUpdate(const std::string& attr); 00442 void endUpdate(); 00443 00446 void setLocationFromAtlas(const std::string& locId); 00447 00451 void setLocation(Entity* newLocation); 00452 00455 void setContentsFromAtlas(const StringList& contents); 00456 00461 void filterMoveAttrs(Atlas::Message::MapType& attrs) const; 00462 00463 typedef std::map<std::string, Entity*> IdEntityMap; 00464 void buildEntityDictFromContents(IdEntityMap& dict); 00465 00466 void addChild(Entity* e); 00467 void removeChild(Entity* e); 00468 00469 void addToLocation(); 00470 void removeFromLocation(); 00471 00472 void updateTasks(const Atlas::Message::Element& e); 00473 void removeTask(Task* t); 00474 00477 void updateCalculatedVisibility(bool wasVisible); 00478 00479 class DynamicState 00480 { 00481 public: 00482 WFMath::Point<3> position; 00483 WFMath::Vector<3> velocity; 00484 }; 00485 00486 void updatePredictedState(const WFMath::TimeStamp& t); 00487 00488 void createAlarmExpired(); 00489 00494 virtual TypeService* getTypeService() const = 0; 00495 00500 virtual void removeFromMovementPrediction() = 0; 00501 00506 virtual void addToMovementPredition() = 0; 00507 00512 virtual Entity* getEntity(const std::string& id) = 0; 00513 00514 00515 AttrMap m_attrs; 00516 00517 TypeInfo* m_type; 00518 00519 // primary state, in native form 00520 Entity* m_location; 00521 EntityArray m_contents; 00522 00523 const std::string m_id; 00524 std::string m_name; 00525 float m_stamp; 00526 std::string m_description; 00527 bool m_visible; 00528 bool m_limbo; 00529 00530 WFMath::AxisBox<3> m_bbox; 00531 WFMath::Point<3> m_position; 00532 WFMath::Vector<3> m_velocity; 00533 WFMath::Quaternion m_orientation; 00534 WFMath::Vector<3> m_acc; 00535 00536 DynamicState m_predicted; 00537 00538 // extra state and state tracking things 00542 int m_updateLevel; 00543 00548 StringSet m_modifiedAttrs; 00549 00550 typedef sigc::signal<void, const Atlas::Message::Element&> AttrChangedSignal; 00551 00552 typedef std::map<std::string, AttrChangedSignal> ObserverMap; 00553 ObserverMap m_observers; 00554 00558 bool m_hasBBox; 00559 00560 WFMath::TimeStamp m_lastMoveTime; 00561 bool m_moving; 00562 00563 bool m_recentlyCreated; 00564 00565 TaskArray m_tasks; 00566 00567 bool m_initialised; 00568 }; 00569 00570 inline unsigned int Entity::numContained() const { 00571 return m_contents.size(); 00572 } 00573 00574 inline Entity* Entity::getContained(unsigned int index) const { 00575 return m_contents[index]; 00576 } 00577 00578 inline const std::string& Entity::getId() const 00579 { 00580 return m_id; 00581 } 00582 00583 inline const std::string& Entity::getName() const 00584 { 00585 return m_name; 00586 } 00587 00588 inline float Entity::getStamp() const 00589 { 00590 return m_stamp; 00591 } 00592 00593 inline TypeInfo* Entity::getType() const 00594 { 00595 return m_type; 00596 } 00597 00599 inline Entity* Entity::getLocation() const 00600 { 00601 return m_location; 00602 } 00603 00605 inline WFMath::Point<3> Entity::getPosition() const 00606 { 00607 return m_position; 00608 } 00610 inline const WFMath::Vector< 3 > & Entity::getVelocity(void) const 00611 { 00612 return m_velocity; 00613 } 00614 00616 inline const WFMath::Quaternion & Entity::getOrientation(void) const 00617 { 00618 return m_orientation; 00619 } 00620 00622 inline const WFMath::AxisBox< 3 > & Entity::getBBox(void) const 00623 { 00624 return m_bbox; 00625 } 00626 00627 inline bool Entity::hasBBox() const 00628 { 00629 return m_hasBBox; 00630 } 00631 00632 inline const TaskArray& Entity::getTasks() const 00633 { 00634 return m_tasks; 00635 } 00636 00637 template<class C> 00638 inline C Entity::toLocationCoords(const C& c) const 00639 { 00640 return c.toParentCoords(getPredictedPos(), m_orientation); 00641 } 00642 00643 template<class C> 00644 inline C Entity::fromLocationCoords(const C& c) const 00645 { 00646 return c.toLocalCoords(getPredictedPos(), m_orientation); 00647 } 00648 00649 inline WFMath::Vector<3> Entity::toLocationCoords(const WFMath::Vector<3>& v) const 00650 { 00651 return WFMath::Vector<3>(v).rotate(m_orientation); 00652 } 00653 00654 inline WFMath::Vector<3> Entity::fromLocationCoords(const WFMath::Vector<3>& v) const 00655 { 00656 return WFMath::Vector<3>(v).rotate(m_orientation.inverse()); 00657 } 00658 00659 } // of namespace 00660 00661 #endif