diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-03-31 12:15:39 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-03-31 12:15:39 +0000 |
commit | e0c185864c09cebd3a7a0118d20ca3a368cbdb6e (patch) | |
tree | ef1b06b20e1d5ef2ae94a8b0a6ea739619fb4eed /src | |
parent | eba3a8635b308475aa4bcfc5f5cd058c48ed679d (diff) | |
download | manaserv-e0c185864c09cebd3a7a0118d20ca3a368cbdb6e.tar.gz manaserv-e0c185864c09cebd3a7a0118d20ca3a368cbdb6e.tar.bz2 manaserv-e0c185864c09cebd3a7a0118d20ca3a368cbdb6e.tar.xz manaserv-e0c185864c09cebd3a7a0118d20ca3a368cbdb6e.zip |
Added a DeathListener interface, which the SpawnArea now uses to get notified
about dying beings, so that it knows when to spawn more.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/game-server/being.cpp | 9 | ||||
-rw-r--r-- | src/game-server/being.hpp | 18 | ||||
-rw-r--r-- | src/game-server/deathlistener.hpp | 50 | ||||
-rw-r--r-- | src/game-server/monster.cpp | 4 | ||||
-rw-r--r-- | src/game-server/monster.hpp | 2 | ||||
-rw-r--r-- | src/game-server/movingobject.hpp | 2 | ||||
-rw-r--r-- | src/game-server/spawnarea.cpp | 8 | ||||
-rw-r--r-- | src/game-server/spawnarea.hpp | 7 |
9 files changed, 95 insertions, 6 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 229d7112..fbf488a8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -84,6 +84,7 @@ tmwserv_game_SOURCES = \ game-server/character.cpp \ game-server/collisiondetection.hpp \ game-server/collisiondetection.cpp \ + game-server/deathlistener.hpp \ game-server/gamehandler.hpp \ game-server/gamehandler.cpp \ game-server/inventory.hpp \ diff --git a/src/game-server/being.cpp b/src/game-server/being.cpp index 87bcb0a3..bbaa73aa 100644 --- a/src/game-server/being.cpp +++ b/src/game-server/being.cpp @@ -24,6 +24,7 @@ #include "defines.h" #include "game-server/collisiondetection.hpp" +#include "game-server/deathlistener.hpp" #include "game-server/mapcomposite.hpp" #include "utils/logger.h" @@ -80,6 +81,14 @@ void Being::die() setAction(DEAD); // dead beings stay where they are setDestination(getPosition()); + + // Notify death listeners + DeathListeners::iterator i_end = mDeathListeners.end(); + DeathListeners::iterator i; + for (i = mDeathListeners.begin(); i != i_end; ++i) + { + (*i)->died(this); + } } void Being::move() diff --git a/src/game-server/being.hpp b/src/game-server/being.hpp index 4423c68e..e0d064bd 100644 --- a/src/game-server/being.hpp +++ b/src/game-server/being.hpp @@ -31,6 +31,7 @@ #include "game-server/movingobject.hpp" class Being; +class DeathListener; class MapComposite; /** @@ -208,6 +209,22 @@ class Being : public MovingObject unsigned short getAttribute(int attributeNumber) const { return mAttributes.at(attributeNumber); } + /** + * Adds a death listener. + */ + void addDeathListener(DeathListener *listener) + { + mDeathListeners.push_back(listener); + } + + /** + * Removes a death listener. + */ + void removeDeathListener(DeathListener *listener) + { + mDeathListeners.remove(listener); + } + protected: /** * Calculates all derived attributes of a beings @@ -229,6 +246,7 @@ class Being : public MovingObject Being(Being const &rhs); Being &operator=(Being const &rhs); + std::list<DeathListener*> mDeathListeners; Hits mHitsTaken; /**< List of punches taken since last update */ }; diff --git a/src/game-server/deathlistener.hpp b/src/game-server/deathlistener.hpp new file mode 100644 index 00000000..b220b846 --- /dev/null +++ b/src/game-server/deathlistener.hpp @@ -0,0 +1,50 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMWSERV_DEATHLISTENER +#define _TMWSERV_DEATHLISTENER + +#include <list> + +class Being; + +/** + * The listener that's notified when a being dies. + */ +class DeathListener +{ + public: + /** + * The obligatory empty virtual destructor. + */ + virtual ~DeathListener() {} + + /** + * Called when a being died. + */ + virtual void died(Being *being) = 0; +}; + +typedef std::list<DeathListener*> DeathListeners; + + +#endif // _TMWSERV_DEATHLISTENER diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp index 03cb6384..de277ee7 100644 --- a/src/game-server/monster.cpp +++ b/src/game-server/monster.cpp @@ -41,8 +41,8 @@ void Monster::update() { if (mAction != DEAD) { - Point randomPos( rand() % 320 + 720, - rand() % 320 + 840 ); + Point randomPos(rand() % 160 - 80 + getPosition().x, + rand() % 160 - 80 + getPosition().y); setDestination(randomPos); mCountDown = 10 + rand() % 10; diff --git a/src/game-server/monster.hpp b/src/game-server/monster.hpp index da5cb71f..c92a7698 100644 --- a/src/game-server/monster.hpp +++ b/src/game-server/monster.hpp @@ -28,7 +28,7 @@ /** * The class for a fightable monster with its own AI */ -class Monster: public Being +class Monster : public Being { public: /** diff --git a/src/game-server/movingobject.hpp b/src/game-server/movingobject.hpp index 99f1caa3..48900863 100644 --- a/src/game-server/movingobject.hpp +++ b/src/game-server/movingobject.hpp @@ -131,4 +131,4 @@ class MovingObject : public Object unsigned mSize; /**< Radius of bounding circle. */ }; -#endif // _TMWSERV_OBJECT_H_ +#endif // _TMWSERV_MOVINGOBJECT_H_ diff --git a/src/game-server/spawnarea.cpp b/src/game-server/spawnarea.cpp index 56430473..9d7a4eb2 100644 --- a/src/game-server/spawnarea.cpp +++ b/src/game-server/spawnarea.cpp @@ -30,7 +30,6 @@ /* * TODO: Take into account spawn rate. - * TODO: Be a death listener to spawned monsters, to adjust mNumBeings. * TODO: Allow specifying being type and use it. */ @@ -51,6 +50,7 @@ SpawnArea::update() while (mNumBeings < mMaxBeings) { Being *being = new Monster(); + being->addDeathListener(this); // some bogus stats for testing being->setSpeed(150); @@ -66,3 +66,9 @@ SpawnArea::update() mNumBeings++; } } + +void +SpawnArea::died(Being *being) +{ + mNumBeings--; +} diff --git a/src/game-server/spawnarea.hpp b/src/game-server/spawnarea.hpp index 5d141fa8..fb293b53 100644 --- a/src/game-server/spawnarea.hpp +++ b/src/game-server/spawnarea.hpp @@ -25,13 +25,16 @@ #define _TMWSERV_SPAWNAREA #include "point.h" +#include "game-server/deathlistener.hpp" #include "game-server/thing.hpp" +class Being; + /** * A spawn area, where monsters spawn. The area is a rectangular field and will * spawn a certain number of a given monster type. */ -class SpawnArea : public Thing +class SpawnArea : public Thing, public DeathListener { public: SpawnArea(int mapId, const Rectangle &zone); @@ -40,6 +43,8 @@ class SpawnArea : public Thing virtual void update(); + virtual void died(Being *being); + protected: Rectangle mZone; int mMaxBeings; /**< Maximum population of this area. */ |