diff options
Diffstat (limited to 'src/game-server/entity.h')
-rw-r--r-- | src/game-server/entity.h | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/game-server/entity.h b/src/game-server/entity.h index 2f360df..bb76e44 100644 --- a/src/game-server/entity.h +++ b/src/game-server/entity.h @@ -52,6 +52,7 @@ class Entity : public sigc::trackable template <class T> void addComponent(T *component); template <class T> T *getComponent() const; + template <class T> T *findComponent() const; template <class T> bool hasComponent() const; bool isVisible() const; @@ -106,8 +107,11 @@ inline Component *Entity::getComponent(ComponentType type) const } /** - * Get a component by its class. Avoids the need for doing a static- - * cast in the calling code. + * Get a component by its class. Avoids the need for doing a static-cast in the + * calling code. + * + * Asserts that the component is actually there. Use findComponent instead if + * you're not sure whether the requested component is actually present. */ template <class T> inline T *Entity::getComponent() const @@ -118,6 +122,16 @@ inline T *Entity::getComponent() const } /** + * Finds a component by its class. Returns 0 when the entity does not have the + * requested component. + */ +template <class T> +inline T *Entity::findComponent() const +{ + return static_cast<T*>(getComponent(T::type)); +} + +/** * Returns whether this class has a certain component. */ template <class T> |