diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2013-04-14 11:15:35 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2013-04-14 11:15:35 +0200 |
commit | 3a132996f798dbf552ac858b06fa656b6790a501 (patch) | |
tree | 050396efcc609d5234414f15d52b6f4c123e6f80 /src/game-server | |
parent | ff7041ab8d0c176a7c9764656c21177944f52c31 (diff) | |
download | manaserv-3a132996f798dbf552ac858b06fa656b6790a501.tar.gz manaserv-3a132996f798dbf552ac858b06fa656b6790a501.tar.bz2 manaserv-3a132996f798dbf552ac858b06fa656b6790a501.tar.xz manaserv-3a132996f798dbf552ac858b06fa656b6790a501.zip |
Use a full user data for Entity references
Only moved a single script function to a member for now, will do others
in a separate commit.
Diffstat (limited to 'src/game-server')
-rw-r--r-- | src/game-server/entity.cpp | 2 | ||||
-rw-r--r-- | src/game-server/entity.h | 21 |
2 files changed, 17 insertions, 6 deletions
diff --git a/src/game-server/entity.cpp b/src/game-server/entity.cpp index cbfa96f3..b40ac442 100644 --- a/src/game-server/entity.cpp +++ b/src/game-server/entity.cpp @@ -26,7 +26,7 @@ Entity::Entity(EntityType type, MapComposite *map) : mType(type) { for (int i = 0; i < ComponentTypeCount; ++i) - mComponents[i] = 0; + mComponents[i] = nullptr; } Entity::~Entity() diff --git a/src/game-server/entity.h b/src/game-server/entity.h index 81faa941..2f360df6 100644 --- a/src/game-server/entity.h +++ b/src/game-server/entity.h @@ -44,7 +44,7 @@ class MapComposite; class Entity : public sigc::trackable { public: - Entity(EntityType type, MapComposite *map = 0); + Entity(EntityType type, MapComposite *map = nullptr); virtual ~Entity(); @@ -52,8 +52,7 @@ class Entity : public sigc::trackable template <class T> void addComponent(T *component); template <class T> T *getComponent() const; - - Component *getComponent(ComponentType type) const; + template <class T> bool hasComponent() const; bool isVisible() const; bool canMove() const; @@ -69,6 +68,8 @@ class Entity : public sigc::trackable sigc::signal<void, Entity *> signal_map_changed; private: + Component *getComponent(ComponentType type) const; + MapComposite *mMap; /**< Map the entity is on */ EntityType mType; /**< Type of this entity. */ @@ -101,7 +102,6 @@ inline void Entity::addComponent(T *component) */ inline Component *Entity::getComponent(ComponentType type) const { - assert(mComponents[type]); return mComponents[type]; } @@ -112,7 +112,18 @@ inline Component *Entity::getComponent(ComponentType type) const template <class T> inline T *Entity::getComponent() const { - return static_cast<T*>(getComponent(T::type)); + T *component = static_cast<T*>(getComponent(T::type)); + assert(component); + return component; +} + +/** + * Returns whether this class has a certain component. + */ +template <class T> +inline bool Entity::hasComponent() const +{ + return getComponent(T::type) != nullptr; } /** |