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
24namespace Atlas {
25 namespace Message {
26 class Element;
27 typedef std::map<std::string, Element> MapType;
28 }
29}
30
31namespace Eris {
32
33// Forward Declarations
34class Entity;
35class TypeInfo;
36class View;
37class EntityRouter;
38class Task;
39
40typedef std::vector<Entity*> EntityArray;
41
55class Entity : virtual public sigc::trackable
56{
57 friend class EntityRouter;
58public:
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
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;
375protected:
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;
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
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
627inline size_t Entity::numContained() const {
628 return m_contents.size();
629}
630
631inline Entity* Entity::getContained(size_t index) const {
632 return m_contents[index];
633}
634
635inline const std::string& Entity::getId() const
636{
637 return m_id;
638}
639
640inline const std::string& Entity::getName() const
641{
642 return m_name;
643}
644
645inline double Entity::getStamp() const
646{
647 return m_stamp;
648}
649
651{
652 return m_type;
653}
654
657{
658 return m_location;
659}
660
662inline const WFMath::Point<3>& Entity::getPosition() const
663{
664 return m_position;
665}
667inline const WFMath::Vector< 3 > & Entity::getVelocity() const
668{
669 return m_velocity;
670}
671
672inline const WFMath::Vector< 3 > & Entity::getAngularVelocity() const
673{
674 return m_angularVelocity;
675}
676
678inline const WFMath::Quaternion & Entity::getOrientation() const
679{
680 return m_orientation;
681}
682
684inline const WFMath::AxisBox< 3 > & Entity::getBBox() const
685{
686 return m_bbox;
687}
688
689inline bool Entity::hasBBox() const
690{
691 return m_hasBBox;
692}
693
694inline const std::map<std::string, std::unique_ptr<Task>>& Entity::getTasks() const
695{
696 return m_tasks;
697}
698
699template<class C>
700inline C Entity::toLocationCoords(const C& c) const
701{
702 return c.toParentCoords(getPredictedPos(), m_orientation);
703}
704
705template<class C>
706inline C Entity::fromLocationCoords(const C& c) const
707{
708 return c.toLocalCoords(getPredictedPos(), m_orientation);
709}
710
711inline WFMath::Vector<3> Entity::toLocationCoords(const WFMath::Vector<3>& v) const
712{
713 return WFMath::Vector<3>(v).rotate(m_orientation);
714}
715
716inline 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
Entity is a concrete (instantiable) class representing one game entity.
Definition: Entity.h:56
virtual void onTaskAdded(const std::string &id, Task *task)
Over-rideable hook for when tasks are added.
Definition: Entity.cpp:374
const WFMath::AxisBox< 3 > & getBBox() const
Definition: Entity.h:684
sigc::signal< void, const Atlas::Objects::Root & > Say
Emitted with the entity speaks.
Definition: Entity.h:327
virtual void onMoved(const WFMath::TimeStamp &timeStamp)
Definition: Entity.cpp:323
const std::map< std::string, std::unique_ptr< Task > > & getTasks() const
Gets the tasks associated with this entity.
Definition: Entity.h:694
virtual void onChildAdded(Entity *child)
Definition: Entity.cpp:364
virtual Entity * getEntity(const std::string &id)=0
Gets an entity with the supplied id from the system.
void setVisible(bool vis)
Definition: Entity.cpp:762
sigc::signal< void, bool > VisibilityChanged
Definition: Entity.h:358
void typeInfo_PropertyChanges(const std::string &propertyName, const Atlas::Message::Element &element)
Connected to the TypeInfo::PropertyeChanges event. This will in turn call the propertyChangedFromType...
Definition: Entity.cpp:483
bool isAncestorTo(Eris::Entity &entity) const
Definition: Entity.cpp:103
bool hasBBox() const
Returns true if the entity has a bounding box. Not all entities have bounding boxes,...
Definition: Entity.h:689
const WFMath::Quaternion & getPredictedOrientation() const
Retrieve the current predicted orientation of an entity.
Definition: Entity.cpp:219
sigc::signal< void, const std::string &, Task * > TaskRemoved
Definition: Entity.h:374
bool isVisible() const
Definition: Entity.cpp:774
virtual void propertyChangedFromTypeInfo(const std::string &propertyName, const Atlas::Message::Element &element)
Called when an property has been changed in the TypeInfo for this entity. If the property doesn't hav...
Definition: Entity.cpp:488
void setFromRoot(const Atlas::Objects::Root &obj, bool includeTypeInfoProperties=false)
Initialise all simple state from a Root. This excludes location and contents, and may optionally excl...
Definition: Entity.cpp:267
void setContentsFromAtlas(const std::vector< std::string > &contents)
Definition: Entity.cpp:690
const std::string & getName() const
Gets the name of the entity. In contrast to getId() this is not unique, and doesn't even have to be s...
Definition: Entity.h:640
void setLocationFromAtlas(const std::string &locId)
Definition: Entity.cpp:622
const WFMath::Quaternion & getOrientation() const
Definition: Entity.h:678
virtual void onImaginary(const Atlas::Objects::Root &act)
Definition: Entity.cpp:347
const WFMath::Point< 3 > & getPredictedPos() const
Retrieve the predicted position of this entity, based on it's velocity and acceleration....
Definition: Entity.cpp:209
std::string m_name
a human readable name
Definition: Entity.h:572
const WFMath::Point< 3 > & getPosition() const
Returns the Entity's position inside it's parent in the parent's local system coordinates.
Definition: Entity.h:662
void updateCalculatedVisibility(bool wasVisible)
Definition: Entity.cpp:785
sigc::signal< void, const std::string &, Task * > TaskAdded
Definition: Entity.h:370
void fillPropertiesFromType(Entity::PropertyMap &properties, const TypeInfo &typeInfo) const
Utility method for recursively filling a map of properties from a TypeInfo instance....
Definition: Entity.cpp:185
sigc::signal< void, const Atlas::Objects::Root &, const TypeInfo & > Noise
Definition: Entity.h:352
static boost::optional< std::string > extractEntityId(const Atlas::Message::Element &element)
Extracts an entity id from the supplied element.
Definition: Entity.cpp:816
virtual void onAction(const Atlas::Objects::Operation::RootOperation &act, const TypeInfo &typeInfo)
Definition: Entity.cpp:332
size_t numContained() const
Gets the number of contained entities, i.e. entities that are direct children of this....
Definition: Entity.h:627
sigc::signal< void, const Atlas::Objects::Operation::RootOperation &, const TypeInfo & > Acted
Definition: Entity.h:340
sigc::signal< void, const std::set< std::string > & > Changed
Definition: Entity.h:304
virtual void onSoundAction(const Atlas::Objects::Operation::RootOperation &op, const TypeInfo &typeInfo)
Definition: Entity.cpp:342
TypeInfo * getType() const
Gets the type of this entity.
Definition: Entity.h:650
void firstSight(const Atlas::Objects::Entity::RootEntity &gent)
Definition: Entity.cpp:254
sigc::signal< void, Entity * > LocationChanged
Signal that the entity's container changed.
Definition: Entity.h:300
sigc::signal< void, const Atlas::Objects::Operation::Hit &, const TypeInfo & > Hit
Definition: Entity.h:345
virtual void onVisibilityChanged(bool vis)
Definition: Entity.cpp:811
const WFMath::Vector< 3 > & getAngularVelocity() const
Definition: Entity.h:672
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
void setLocation(Entity *newLocation, bool removeFromOldLocation=true)
Definition: Entity.cpp:644
virtual void onTalk(const Atlas::Objects::Operation::RootOperation &talk)
Definition: Entity.cpp:303
int m_updateLevel
Definition: Entity.h:600
double getStamp() const
Access the current time-stamp of the entity.
Definition: Entity.h:645
const PropertyMap & getInstanceProperties() const
Gets all locally defined properties. This will only return those properties that are locally defined ...
Definition: Entity.cpp:180
const WFMath::Vector< 3 > & getVelocity() const
Definition: Entity.h:667
const std::string m_id
the Atlas object ID
Definition: Entity.h:571
double m_angularMag
Definition: Entity.h:592
bool m_hasBBox
Definition: Entity.h:616
WFMath::Vector< 3 > m_angularVelocity
Definition: Entity.h:588
sigc::signal< void, const std::string & > Emote
Definition: Entity.h:333
sigc::signal< void > Moved
Definition: Entity.h:307
const WFMath::Vector< 3 > & getPredictedVelocity() const
Retrieve the current predicted velocity of an entity. If the entity have no acceleration,...
Definition: Entity.cpp:214
std::set< std::string > m_modifiedProperties
Definition: Entity.h:606
const Atlas::Message::Element * ptrOfProperty(const std::string &name) const
Gets the value of a named property, or null if none exists. If no property by the specified name can ...
Definition: Entity.cpp:149
double m_stamp
last modification time (in seconds)
Definition: Entity.h:573
const std::string & getId() const
Retrieve the unique entity ID.
Definition: Entity.h:635
virtual void onChildRemoved(Entity *child)
Definition: Entity.cpp:369
bool nativePropertyChanged(const std::string &p, const Atlas::Message::Element &v)
Definition: Entity.cpp:400
virtual void init(const Atlas::Objects::Entity::RootEntity &ge, bool fromCreateOp)
Definition: Entity.cpp:80
bool m_moving
flag recording if this entity is current considered in-motion
Definition: Entity.h:620
virtual void setMoving(bool moving)
Definition: Entity.cpp:355
sigc::signal< void > BeingDeleted
Definition: Entity.h:365
Entity * getContained(size_t index) const
Gets the child entity at the specified index.
Definition: Entity.h:631
Entity * getTopEntity()
Gets the top level entity for this entity, i.e. the parent location which has no parent....
Definition: Entity.cpp:92
bool hasProperty(const std::string &p) const
Checks whether an property exists.
Definition: Entity.cpp:135
PropertyMap getProperties() const
Gets all properties defined for this entity. The collection of entities returned will include both lo...
Definition: Entity.cpp:169
void shutdown()
Definition: Entity.cpp:65
bool m_waitingForParentBind
waiting for parent bind
Definition: Entity.h:575
bool isMoving() const
Test if this entity has a non-zero velocity vector.
Definition: Entity.cpp:224
Entity * getLocation() const
The containing entity, or null if this is a top-level visible entity.
Definition: Entity.h:656
sigc::connection observe(const std::string &propertyName, const PropertyChangedSlot &aslot, bool evaluateNow)
Setup an observer so that the specified slot is fired when the named property's value changes.
Definition: Entity.cpp:196
const Atlas::Message::Element & valueOfProperty(const std::string &name) const
Gets the value of a named property. If no property by the specified name can be found an InvalidOpera...
Definition: Entity.cpp:115
virtual void onHit(const Atlas::Objects::Operation::Hit &hit, const TypeInfo &typeInfo)
Definition: Entity.cpp:337
sigc::slot< void, const Atlas::Message::Element & > PropertyChangedSlot
A slot which can be used for receiving property update signals.
Definition: Entity.h:123
The representation of an Atlas type (i.e a class or operation definition). This class supports effice...
Definition: TypeInfo.h:33
Definition: Account.cpp:33