diff options
Diffstat (limited to 'src/game-server')
-rw-r--r-- | src/game-server/entity.h | 18 | ||||
-rw-r--r-- | src/game-server/state.cpp | 8 | ||||
-rw-r--r-- | src/game-server/state.h | 2 |
3 files changed, 21 insertions, 7 deletions
diff --git a/src/game-server/entity.h b/src/game-server/entity.h index 2f360df6..bb76e440 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> diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp index 10869669..58d848de 100644 --- a/src/game-server/state.cpp +++ b/src/game-server/state.cpp @@ -823,18 +823,18 @@ void GameState::enqueueWarp(Entity *ptr, enqueueEvent(ptr, event); } -void GameState::sayAround(Entity *obj, const std::string &text) +void GameState::sayAround(Entity *entity, const std::string &text) { - Point speakerPosition = obj->getComponent<ActorComponent>()->getPosition(); + Point speakerPosition = entity->getComponent<ActorComponent>()->getPosition(); int visualRange = Configuration::getValue("game_visualRange", 448); - for (CharacterIterator i(obj->getMap()->getAroundActorIterator(obj, visualRange)); i; ++i) + for (CharacterIterator i(entity->getMap()->getAroundActorIterator(entity, visualRange)); i; ++i) { const Point &point = (*i)->getComponent<ActorComponent>()->getPosition(); if (speakerPosition.inRangeOf(point, visualRange)) { - sayTo(*i, obj, text); + sayTo(*i, entity, text); } } } diff --git a/src/game-server/state.h b/src/game-server/state.h index 4f690ad0..147a57cb 100644 --- a/src/game-server/state.h +++ b/src/game-server/state.h @@ -99,7 +99,7 @@ namespace GameState /** * Says something to everything around an actor. */ - void sayAround(Entity *, const std::string &text); + void sayAround(Entity *entity, const std::string &text); /** * Says something to every player on the server. |