Eris  1.4.0
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 Declerations
34 class Entity;
35 class TypeInfo;
36 class View;
37 class EntityRouter;
38 class Task;
39 
40 typedef std::vector<Entity*> EntityArray;
41 typedef std::vector<TypeInfoPtr> TypeInfoArray;
42 
58 class Entity : virtual public sigc::trackable
59 {
60 public:
61  typedef std::map<std::string, Atlas::Message::Element> AttrMap;
62 
63  explicit Entity(const std::string& id, TypeInfo* ty);
64  virtual ~Entity();
65 
71  virtual void shutdown();
72 
73 // heirarchy interface
79  size_t numContained() const;
80 
86  Entity* getContained(size_t index) const;
87 
95  const Atlas::Message::Element& valueOfAttr(const std::string& attr) const;
96 
102  bool hasAttr(const std::string &p) const;
103 
104 
111  const Atlas::Message::Element* ptrOfAttr(const std::string& attr) const;
112 
116  typedef sigc::slot<void, const Atlas::Message::Element&> AttrChangedSlot;
117 
125  sigc::connection observe(const std::string& attr, const AttrChangedSlot& aslot);
126 
127 // accessors
132  const std::string& getId() const;
133 
139  const std::string& getName() const;
140 
145  float getStamp() const;
146 
151  TypeInfo* getType() const;
152 
157  Entity* getLocation() const;
158 
163  WFMath::Point<3> getPosition() const;
164 
172  const AttrMap getAttributes() const;
173 
181  const AttrMap& getInstanceAttributes() const;
182 
187  bool isMoving() const;
188 
194  const WFMath::Point<3>& getPredictedPos() const;
195 
201  const WFMath::Vector<3>& getPredictedVelocity() const;
202 
207  const WFMath::Quaternion& getPredictedOrientation() const;
208 
210  WFMath::Point<3> getViewPosition() const;
211 
213  WFMath::Quaternion getViewOrientation() const;
214 
216  const WFMath::Vector< 3 > & getVelocity() const;
217 
219  const WFMath::Vector< 3 > & getAngularVelocity() const;
220 
222  const WFMath::Quaternion & getOrientation() const;
223 
225  const WFMath::AxisBox< 3 > & getBBox() const;
226 
232  bool hasBBox() const;
233 
238  const std::map<std::string, Task*>& getTasks() const;
239 
240  bool hasChild(const std::string& eid) const;
241 
243  bool isVisible() const;
244 
257  static boost::optional<std::string> extractEntityId(const Atlas::Message::Element& element);
258 
259 // coordinate transformations
260  template<class C>
261  C toLocationCoords(const C& c) const;
262 
263  template<class C>
264  C fromLocationCoords(const C& c) const;
265 
266  // A vector (e.g., the distance between two points, or
267  // a velocity) gets rotated by a coordinate transformation,
268  // but doesn't get shifted by the change in the position
269  // of the origin, so we handle it separately. We also
270  // need to copy the vector before rotating, because
271  // Vector::rotate() rotates it in place.
272  WFMath::Vector<3> toLocationCoords(const WFMath::Vector<3>& v) const;
273 
274  WFMath::Vector<3> fromLocationCoords(const WFMath::Vector<3>& v) const;
275 
276 // Signals
277  sigc::signal<void, Entity*> ChildAdded;
278  sigc::signal<void, Entity*> ChildRemoved;
279 
281 
285  sigc::signal<void, Entity*> LocationChanged;
286 
289  sigc::signal<void, const StringSet&> Changed;
290 
292  sigc::signal<void> Moved;
293 
296  sigc::signal<void, bool> Moving;
297 
313  sigc::signal< void, const Atlas::Objects::Root & > Say;
314 
319  sigc::signal<void, const std::string&> Emote;
320 
326  sigc::signal<void, const Atlas::Objects::Operation::RootOperation&> Acted;
327 
333  sigc::signal<void, const Atlas::Objects::Root&> Noise;
334 
339  sigc::signal<void, bool> VisibilityChanged;
340 
346  sigc::signal<void> BeingDeleted;
347 
351  sigc::signal<void, Task*> TaskAdded;
355  sigc::signal<void, Task*> TaskRemoved;
356 protected:
360  virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp);
361 
365  virtual void onTalk(const Atlas::Objects::Operation::RootOperation& talk);
366 
367  virtual void onAttrChanged(const std::string& attr, const Atlas::Message::Element &v);
368 
369  virtual void onLocationChanged(Entity* oldLoc);
370 
373  virtual void onMoved();
374 
378  virtual void onVisibilityChanged(bool vis);
379 
384  virtual void onAction(const Atlas::Objects::Operation::RootOperation& act);
385 
390  virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation& op);
391 
396  virtual void onImaginary(const Atlas::Objects::Root& act);
397 
403  virtual void setMoving(bool moving);
404 
409  virtual void onChildAdded(Entity* child);
410 
415  virtual void onChildRemoved(Entity* child);
416 
421  virtual void onTaskAdded(Task* task);
422 
423  friend class IGRouter;
424  friend class View;
425  friend class Task;
426 
431  void sight(const Atlas::Objects::Entity::RootEntity& gent);
432 
433 
440  void setFromRoot(const Atlas::Objects::Root& obj, bool allowMotion, bool includeTypeInfoAttributes = false);
441 
444  void setVisible(bool vis);
445 
446  void setAttr(const std::string &p, const Atlas::Message::Element &v);
447 
452  bool nativeAttrChanged(const std::string &p, const Atlas::Message::Element &v);
453 
460  void typeInfo_AttributeChanges(const std::string& attributeName, const Atlas::Message::Element& element);
461 
468  virtual void attrChangedFromTypeInfo(const std::string& attributeName, const Atlas::Message::Element& element);
469 
470 
477  void fillAttributesFromType(Entity::AttrMap& attributes, TypeInfo* typeInfo) const;
478 
479  void beginUpdate();
480  void addToUpdate(const std::string& attr);
481  void endUpdate();
482 
485  void setLocationFromAtlas(const std::string& locId);
486 
490  void setLocation(Entity* newLocation);
491 
494  void setContentsFromAtlas(const StringList& contents);
495 
500  void filterMoveAttrs(Atlas::Message::MapType& attrs) const;
501 
502  typedef std::unordered_map<std::string, Entity*> IdEntityMap;
503  void buildEntityDictFromContents(IdEntityMap& dict);
504 
505  void addChild(Entity* e);
506  void removeChild(Entity* e);
507 
508  void addToLocation();
509  void removeFromLocation();
510 
511  void updateTasks(const Atlas::Message::Element& e);
512 
515  void updateCalculatedVisibility(bool wasVisible);
516 
518  {
519  public:
520  WFMath::Point<3> position;
521  WFMath::Vector<3> velocity;
522  WFMath::Quaternion orientation;
523  };
524 
525  void updatePredictedState(const WFMath::TimeStamp& t);
526 
531  virtual TypeService* getTypeService() const = 0;
532 
537  virtual void removeFromMovementPrediction() = 0;
538 
543  virtual void addToMovementPredition() = 0;
544 
549  virtual Entity* getEntity(const std::string& id) = 0;
550 
551 
552  AttrMap m_attrs;
553 
554  TypeInfo* m_type;
555 
556 // primary state, in native form
557  Entity* m_location;
558  EntityArray m_contents;
559 
560  const std::string m_id;
561  std::string m_name;
562  float m_stamp;
563  std::string m_description;
564  bool m_visible;
565  bool m_limbo;
566 
567  WFMath::Vector<3> m_scale;
568  WFMath::AxisBox<3> m_bbox;
569  WFMath::AxisBox<3> m_bboxUnscaled;
570  WFMath::Point<3> m_position;
571  WFMath::Vector<3> m_velocity;
572  WFMath::Quaternion m_orientation;
573  WFMath::Vector<3> m_acc;
578  WFMath::Vector<3> m_angularVelocity;
583 
584  DynamicState m_predicted;
585 
586 // extra state and state tracking things
591 
596  StringSet m_modifiedAttrs;
597 
598  typedef sigc::signal<void, const Atlas::Message::Element&> AttrChangedSignal;
599 
600  typedef std::unordered_map<std::string, AttrChangedSignal> ObserverMap;
601  ObserverMap m_observers;
602 
606  bool m_hasBBox;
607 
608  WFMath::TimeStamp m_lastMoveTime;
609  bool m_moving;
610 
612 
613  std::map<std::string, Task*> m_tasks;
614 
615  bool m_initialised;
616 };
617 
618 inline size_t Entity::numContained() const {
619  return m_contents.size();
620 }
621 
622 inline Entity* Entity::getContained(size_t index) const {
623  return m_contents[index];
624 }
625 
626 inline const std::string& Entity::getId() const
627 {
628  return m_id;
629 }
630 
631 inline const std::string& Entity::getName() const
632 {
633  return m_name;
634 }
635 
636 inline float Entity::getStamp() const
637 {
638  return m_stamp;
639 }
640 
641 inline TypeInfo* Entity::getType() const
642 {
643  return m_type;
644 }
645 
647 inline Entity* Entity::getLocation() const
648 {
649  return m_location;
650 }
651 
653 inline WFMath::Point<3> Entity::getPosition() const
654 {
655  return m_position;
656 }
658 inline const WFMath::Vector< 3 > & Entity::getVelocity() const
659 {
660  return m_velocity;
661 }
662 
663 inline const WFMath::Vector< 3 > & Entity::getAngularVelocity() const
664 {
665  return m_angularVelocity;
666 }
667 
669 inline const WFMath::Quaternion & Entity::getOrientation() const
670 {
671  return m_orientation;
672 }
673 
675 inline const WFMath::AxisBox< 3 > & Entity::getBBox() const
676 {
677  return m_bbox;
678 }
679 
680 inline bool Entity::hasBBox() const
681 {
682  return m_hasBBox;
683 }
684 
685 inline const std::map<std::string, Task*>& Entity::getTasks() const
686 {
687  return m_tasks;
688 }
689 
690 template<class C>
691 inline C Entity::toLocationCoords(const C& c) const
692 {
693  return c.toParentCoords(getPredictedPos(), m_orientation);
694 }
695 
696 template<class C>
697 inline C Entity::fromLocationCoords(const C& c) const
698 {
699  return c.toLocalCoords(getPredictedPos(), m_orientation);
700 }
701 
702 inline WFMath::Vector<3> Entity::toLocationCoords(const WFMath::Vector<3>& v) const
703 {
704  return WFMath::Vector<3>(v).rotate(m_orientation);
705 }
706 
707 inline WFMath::Vector<3> Entity::fromLocationCoords(const WFMath::Vector<3>& v) const
708 {
709  return WFMath::Vector<3>(v).rotate(m_orientation.inverse());
710 }
711 
712 } // of namespace
713 
714 #endif
sigc::signal< void, Task * > TaskAdded
Emitted when a task has been added to the entity.
Definition: Entity.h:351
sigc::signal< void > Moved
Emitted when then entity&#39;s position, orientation or velocity change.
Definition: Entity.h:292
sigc::signal< void, const Atlas::Objects::Root & > Noise
Emitted when this entity performs an action which causes a noise.
Definition: Entity.h:333
sigc::signal< void, const StringSet & > Changed
Emitted when one or more attributes change.
Definition: Entity.h:289
WFMath::Vector< 3 > m_angularVelocity
Angular velocity.
Definition: Entity.h:578
std::string m_name
a human readable name
Definition: Entity.h:561
The representation of an Atlas type (i.e a class or operation definition).
Definition: TypeInfo.h:31
sigc::slot< void, const Atlas::Message::Element & > AttrChangedSlot
A slot which can be used for receiving attribute update signals.
Definition: Entity.h:116
View encapsulates the set of entities currently visible to an Avatar, as well as those that have rece...
Definition: View.h:37
sigc::signal< void > BeingDeleted
Emitted prior to deletion.
Definition: Entity.h:346
bool m_limbo
waiting for parent bind
Definition: Entity.h:565
Definition: IGRouter.h:13
sigc::signal< void, Entity * > LocationChanged
Signal that the entity&#39;s container changed.
Definition: Entity.h:285
const std::string m_id
the Atlas object ID
Definition: Entity.h:560
sigc::signal< void, const Atlas::Objects::Root &> Say
Emitted with the entity speaks.
Definition: Entity.h:313
float m_stamp
last modification time (in seconds)
Definition: Entity.h:562
Every Eris class and type lives inside the Eris namespace; certain utility functions live in the Util...
Definition: Account.cpp:34
bool m_moving
flag recording if this entity is current considered in-motion
Definition: Entity.h:609
sigc::signal< void, bool > VisibilityChanged
Emitted when the visibility of the entity changes.
Definition: Entity.h:339
bool m_recentlyCreated
flag set if this entity was the subject of a sight(create)
Definition: Entity.h:611
sigc::signal< void, bool > Moving
Emitted when an entity starts or stops moving (as determined by the &#39;setMoving&#39; method.
Definition: Entity.h:296
sigc::signal< void, Task * > TaskRemoved
Emitted when a task has been removed from the entity.
Definition: Entity.h:355
StringSet m_modifiedAttrs
When a batched property update is in progress, the set tracks the names of each modified property...
Definition: Entity.h:596
Definition: Entity.h:517
sigc::signal< void, const Atlas::Objects::Operation::RootOperation & > Acted
Emitted when this entity performs an action.
Definition: Entity.h:326
A service class querying and caching types.
Definition: TypeService.h:24
Entity is a concrete (instantiable) class representing one game entity.
Definition: Entity.h:58
sigc::signal< void, const std::string & > Emote
Emitted when this entity emits an imaginary operation (also known as an emote.
Definition: Entity.h:319
int m_updateLevel
If greater than zero, we are doing a batched update.
Definition: Entity.h:590
Definition: Task.h:25
bool m_hasBBox
This flag should be set when the server notifies that this entity has a bounding box.
Definition: Entity.h:606
Definition: BaseConnection.h:18
float m_angularMag
The magnitude of the angular velocity.
Definition: Entity.h:582