eris  1.4.0
A WorldForge client library.
Entity.h
1 #ifndef ERIS_ENTITY_H
2 #define ERIS_ENTITY_H
3 
4 #include "Types.h"
5 
6 #include <Atlas/Objects/ObjectsFwd.h>
7 
8 #include <wfmath/point.h>
9 #include <wfmath/vector.h>
10 #include <wfmath/axisbox.h>
11 #include <wfmath/quaternion.h>
12 #include <wfmath/timestamp.h>
13 
14 #include <sigc++/trackable.h>
15 #include <sigc++/slot.h>
16 #include <sigc++/signal.h>
17 #include <sigc++/connection.h>
18 
19 #include <map>
20 #include <vector>
21 #include <unordered_map>
22 #include <boost/optional.hpp>
23 
24 namespace Atlas {
25  namespace Message {
26  class Element;
27  typedef std::map<std::string, Element> MapType;
28  }
29 }
30 
31 namespace Eris {
32 
33 // Forward Declarations
34 class Entity;
35 class TypeInfo;
36 class View;
37 class EntityRouter;
38 class Task;
39 
40 typedef std::vector<Entity*> EntityArray;
41 
55 class Entity : virtual public sigc::trackable
56 {
57  friend class EntityRouter;
58 public:
59  typedef std::map<std::string, Atlas::Message::Element> PropertyMap;
60 
61  explicit Entity(std::string id, TypeInfo* ty);
62  virtual ~Entity();
63 
64 // hierarchy interface
70  size_t numContained() const;
71 
77  Entity* getContained(size_t index) const;
78 
84  Entity* getTopEntity();
85 
93  bool isAncestorTo(Eris::Entity& entity) const;
94 
102  const Atlas::Message::Element& valueOfProperty(const std::string& name) const;
103 
109  bool hasProperty(const std::string &p) const;
110 
111 
118  const Atlas::Message::Element* ptrOfProperty(const std::string& name) const;
119 
123  typedef sigc::slot<void, const Atlas::Message::Element&> PropertyChangedSlot;
124 
133  sigc::connection observe(const std::string& propertyName, const PropertyChangedSlot& aslot, bool evaluateNow);
134 
135 // accessors
140  const std::string& getId() const;
141 
147  const std::string& getName() const;
148 
153  double getStamp() const;
154 
159  TypeInfo* getType() const;
160 
165  Entity* getLocation() const;
166 
171  const WFMath::Point<3>& getPosition() const;
172 
184  PropertyMap getProperties() const;
185 
197  const PropertyMap& getInstanceProperties() const;
198 
203  bool isMoving() const;
204 
210  const WFMath::Point<3>& getPredictedPos() const;
211 
217  const WFMath::Vector<3>& getPredictedVelocity() const;
218 
223  const WFMath::Quaternion& getPredictedOrientation() const;
224 
226  const WFMath::Vector<3> & getVelocity() const;
227 
229  const WFMath::Vector<3> & getAngularVelocity() const;
230 
232  const WFMath::Quaternion & getOrientation() const;
233 
235  const WFMath::AxisBox<3> & getBBox() const;
236 
242  bool hasBBox() const;
243 
248  const std::map<std::string, std::unique_ptr<Task>>& getTasks() const;
249 
250  bool hasChild(const std::string& eid) const;
251 
253  bool isVisible() const;
254 
267  static boost::optional<std::string> extractEntityId(const Atlas::Message::Element& element);
268 
269 // coordinate transformations
270  template<class C>
271  C toLocationCoords(const C& c) const;
272 
273  template<class C>
274  C fromLocationCoords(const C& c) const;
275 
276 
277  const std::vector<Entity*>& getContent() const {
278  return m_contents;
279  }
280 
281  // A vector (e.g., the distance between two points, or
282  // a velocity) gets rotated by a coordinate transformation,
283  // but doesn't get shifted by the change in the position
284  // of the origin, so we handle it separately. We also
285  // need to copy the vector before rotating, because
286  // Vector::rotate() rotates it in place.
287  WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const;
288 
289  WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const;
290 
291 // Signals
292  sigc::signal<void, Entity*> ChildAdded;
293  sigc::signal<void, Entity*> ChildRemoved;
294 
296 
300  sigc::signal<void, Entity*> LocationChanged;
301 
304  sigc::signal<void, const std::set<std::string>&> Changed;
305 
307  sigc::signal<void> Moved;
308 
310  sigc::signal<void, bool> Moving;
311 
327  sigc::signal< void, const Atlas::Objects::Root & > Say;
328 
333  sigc::signal<void, const std::string&> Emote;
334 
340  sigc::signal<void, const Atlas::Objects::Operation::RootOperation&, const TypeInfo&> Acted;
341 
345  sigc::signal<void, const Atlas::Objects::Operation::Hit&, const TypeInfo&> Hit;
346 
352  sigc::signal<void, const Atlas::Objects::Root&, const TypeInfo&> Noise;
353 
358  sigc::signal<void, bool> VisibilityChanged;
359 
365  sigc::signal<void> BeingDeleted;
366 
370  sigc::signal<void, const std::string&, Task*> TaskAdded;
374  sigc::signal<void, const std::string&, Task*> TaskRemoved;
375 protected:
379  virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp);
380 
386  void shutdown();
387 
391  virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk);
392 
393  virtual void onPropertyChanged(const std::string& propertyName, const Atlas::Message::Element &v);
394 
395  virtual void onLocationChanged(Entity* oldLoc);
396 
399  virtual void onMoved(const WFMath::TimeStamp& timeStamp);
400 
404  virtual void onVisibilityChanged(bool vis);
405 
410  virtual void onAction(const Atlas::Objects::Operation::RootOperation& act, const TypeInfo& typeInfo);
411 
416  virtual void onHit(const Atlas::Objects::Operation::Hit& hit, const TypeInfo& typeInfo);
417 
422  virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op, const TypeInfo& typeInfo);
423 
428  virtual void onImaginary(const Atlas::Objects::Root& act);
429 
435  virtual void setMoving(bool moving);
436 
441  virtual void onChildAdded(Entity* child);
442 
447  virtual void onChildRemoved(Entity* child);
448 
454  virtual void onTaskAdded(const std::string& id, Task* task);
455 
456  friend class IGRouter;
457  friend class View;
458  friend class Task;
459  friend class Avatar;
460 
466  void firstSight(const Atlas::Objects::Entity::RootEntity& gent);
467 
468 
474  void setFromRoot(const Atlas::Objects::Root& obj, bool includeTypeInfoProperties = false);
475 
478  void setVisible(bool vis);
479 
480  void setProperty(const std::string &p, const Atlas::Message::Element &v);
481 
486  bool nativePropertyChanged(const std::string &p, const Atlas::Message::Element &v);
487 
494  void typeInfo_PropertyChanges(const std::string& propertyName, const Atlas::Message::Element& element);
495 
503  virtual void propertyChangedFromTypeInfo(const std::string& propertyName, const Atlas::Message::Element& element);
504 
505 
512  void fillPropertiesFromType(Entity::PropertyMap& properties, const TypeInfo& typeInfo) const;
513 
514  void beginUpdate();
515  void addToUpdate(const std::string& propertyName);
516  void endUpdate();
517 
520  void setLocationFromAtlas(const std::string& locId);
521 
525  void setLocation(Entity* newLocation, bool removeFromOldLocation = true);
526 
529  void setContentsFromAtlas(const std::vector<std::string>& contents);
530 
531  typedef std::unordered_map<std::string, Entity*> IdEntityMap;
532  void buildEntityDictFromContents(IdEntityMap& dict);
533 
534  void addChild(Entity* e);
535  void removeChild(Entity* e);
536 
537  void addToLocation();
538  void removeFromLocation();
539 
540  void updateTasks(const Atlas::Message::Element& e);
541 
544  void updateCalculatedVisibility(bool wasVisible);
545 
547  {
548  public:
549  WFMath::Point<3> position;
550  WFMath::Vector<3> velocity;
551  WFMath::Quaternion orientation;
552  };
553 
554  void updatePredictedState(const WFMath::TimeStamp& t, double simulationSpeed);
555 
560  virtual Entity* getEntity(const std::string& id) = 0;
561 
562 
563  PropertyMap m_properties;
564 
565  TypeInfo* m_type;
566 
567 // primary state, in native form
568  Entity* m_location;
569  EntityArray m_contents;
570 
571  const std::string m_id;
572  std::string m_name;
573  double m_stamp;
574  bool m_visible;
576 
577  WFMath::Vector<3> m_scale;
578  WFMath::AxisBox<3> m_bbox;
579  WFMath::AxisBox<3> m_bboxUnscaled;
580  WFMath::Point<3> m_position;
581  WFMath::Vector<3> m_velocity;
582  WFMath::Quaternion m_orientation;
583  WFMath::Vector<3> m_acc;
588  WFMath::Vector<3> m_angularVelocity;
592  double m_angularMag;
593 
594  DynamicState m_predicted;
595 
596 // extra state and state tracking things
601 
606  std::set<std::string> m_modifiedProperties;
607 
608  typedef sigc::signal<void, const Atlas::Message::Element&> PropertyChangedSignal;
609 
610  typedef std::unordered_map<std::string, PropertyChangedSignal> ObserverMap;
611  ObserverMap m_observers;
612 
616  bool m_hasBBox;
617 
618  WFMath::TimeStamp m_lastPosTime;
619  WFMath::TimeStamp m_lastOrientationTime;
620  bool m_moving;
621 
623 
624  std::map<std::string, std::unique_ptr<Task>> m_tasks;
625 };
626 
627 inline size_t Entity::numContained() const {
628  return m_contents.size();
629 }
630 
631 inline Entity* Entity::getContained(size_t index) const {
632  return m_contents[index];
633 }
634 
635 inline const std::string& Entity::getId() const
636 {
637  return m_id;
638 }
639 
640 inline const std::string& Entity::getName() const
641 {
642  return m_name;
643 }
644 
645 inline double Entity::getStamp() const
646 {
647  return m_stamp;
648 }
649 
650 inline TypeInfo* Entity::getType() const
651 {
652  return m_type;
653 }
654 
656 inline Entity* Entity::getLocation() const
657 {
658  return m_location;
659 }
660 
662 inline const WFMath::Point<3>& Entity::getPosition() const
663 {
664  return m_position;
665 }
667 inline const WFMath::Vector< 3 > & Entity::getVelocity() const
668 {
669  return m_velocity;
670 }
671 
672 inline const WFMath::Vector< 3 > & Entity::getAngularVelocity() const
673 {
674  return m_angularVelocity;
675 }
676 
678 inline const WFMath::Quaternion & Entity::getOrientation() const
679 {
680  return m_orientation;
681 }
682 
684 inline const WFMath::AxisBox< 3 > & Entity::getBBox() const
685 {
686  return m_bbox;
687 }
688 
689 inline bool Entity::hasBBox() const
690 {
691  return m_hasBBox;
692 }
693 
694 inline const std::map<std::string, std::unique_ptr<Task>>& Entity::getTasks() const
695 {
696  return m_tasks;
697 }
698 
699 template<class C>
700 inline C Entity::toLocationCoords(const C& c) const
701 {
702  return c.toParentCoords(getPredictedPos(), m_orientation);
703 }
704 
705 template<class C>
706 inline C Entity::fromLocationCoords(const C& c) const
707 {
708  return c.toLocalCoords(getPredictedPos(), m_orientation);
709 }
710 
711 inline WFMath::Vector<3> Entity::toLocationCoords(const WFMath::Vector<3>& v) const
712 {
713  return WFMath::Vector<3>(v).rotate(m_orientation);
714 }
715 
716 inline WFMath::Vector<3> Entity::fromLocationCoords(const WFMath::Vector<3>& v) const
717 {
718  return WFMath::Vector<3>(v).rotate(m_orientation.inverse());
719 }
720 
721 } // of namespace
722 
723 #endif
double m_angularMag
Definition: Entity.h:592
sigc::signal< void, const Atlas::Objects::Operation::Hit &, const TypeInfo & > Hit
Definition: Entity.h:345
sigc::signal< void > Moved
Definition: Entity.h:307
WFMath::Vector< 3 > m_angularVelocity
Definition: Entity.h:588
std::string m_name
a human readable name
Definition: Entity.h:572
The representation of an Atlas type (i.e a class or operation definition). This class supports effice...
Definition: TypeInfo.h:32
sigc::signal< void > BeingDeleted
Definition: Entity.h:365
sigc::signal< void, const std::string &, Task * > TaskAdded
Definition: Entity.h:370
sigc::signal< void, Entity * > LocationChanged
Signal that the entity&#39;s container changed.
Definition: Entity.h:300
const std::string m_id
the Atlas object ID
Definition: Entity.h:571
sigc::signal< void, const Atlas::Objects::Root & > Say
Emitted with the entity speaks.
Definition: Entity.h:327
sigc::slot< void, const Atlas::Message::Element & > PropertyChangedSlot
A slot which can be used for receiving property update signals.
Definition: Entity.h:123
Definition: Account.cpp:33
bool m_moving
flag recording if this entity is current considered in-motion
Definition: Entity.h:620
sigc::signal< void, const std::set< std::string > & > Changed
Definition: Entity.h:304
sigc::signal< void, bool > VisibilityChanged
Definition: Entity.h:358
bool m_recentlyCreated
flag set if this entity was the subject of a sight(create)
Definition: Entity.h:622
sigc::signal< void, bool > Moving
Definition: Entity.h:310
Entity is a concrete (instantiable) class representing one game entity.
Definition: Entity.h:55
sigc::signal< void, const std::string & > Emote
Definition: Entity.h:333
int m_updateLevel
Definition: Entity.h:600
bool m_waitingForParentBind
waiting for parent bind
Definition: Entity.h:575
bool m_hasBBox
Definition: Entity.h:616
std::set< std::string > m_modifiedProperties
Definition: Entity.h:606
sigc::signal< void, const Atlas::Objects::Operation::RootOperation &, const TypeInfo & > Acted
Definition: Entity.h:340
double m_stamp
last modification time (in seconds)
Definition: Entity.h:573
sigc::signal< void, const Atlas::Objects::Root &, const TypeInfo & > Noise
Definition: Entity.h:352
sigc::signal< void, const std::string &, Task * > TaskRemoved
Definition: Entity.h:374