diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-02-06 02:56:48 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-02-06 02:56:48 +0000 |
commit | e2c61ef8ec4f8685c0eafa2284c7485cec489021 (patch) | |
tree | cf2805221603b14e679205845fa0734702a8aae2 | |
parent | 2a7690e2687a43fdc0004684b7de97b2d84cb8b0 (diff) | |
download | mana-e2c61ef8ec4f8685c0eafa2284c7485cec489021.tar.gz mana-e2c61ef8ec4f8685c0eafa2284c7485cec489021.tar.bz2 mana-e2c61ef8ec4f8685c0eafa2284c7485cec489021.tar.xz mana-e2c61ef8ec4f8685c0eafa2284c7485cec489021.zip |
Added a FloorItemManager class.
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/engine.cpp | 4 | ||||
-rwxr-xr-x | src/floor_item.cpp | 68 | ||||
-rwxr-xr-x | src/floor_item.h | 15 | ||||
-rw-r--r-- | src/flooritemmanager.cpp | 84 | ||||
-rw-r--r-- | src/flooritemmanager.h | 57 | ||||
-rw-r--r-- | src/game.cpp | 16 | ||||
-rw-r--r-- | src/gui/gui.cpp | 6 | ||||
-rw-r--r-- | src/net/itemhandler.cpp | 9 |
10 files changed, 175 insertions, 95 deletions
@@ -1,3 +1,12 @@ +2006-02-06 Björn Steinbrink <B.Steinbrink@gmx.de> + + * ChangeLog, data/graphics/gui/browserfont.png, + data/graphics/gui/fixedfont.png, data/graphics/gui/rpgfont_wider.png, + data/graphics/gui/sansserif8.png, src/Makefile.am, src/engine.cpp, + src/floor_item.cpp, src/floor_item.h, src/flooritemmanager.cpp, + src/flooritemmanager.h, src/game.cpp, src/gui/gui.cpp, + src/net/itemhandler.cpp: Introduced a FloorItemManager class. + 2006-02-05 Yohann Ferreira <bertram@cegetel.net> * data/graphics/gui/rpgfont_wider.png, diff --git a/src/Makefile.am b/src/Makefile.am index 79512e66..6e0747a8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -187,6 +187,8 @@ tmw_SOURCES = graphic/spriteset.cpp \ equipment.h \ floor_item.cpp \ floor_item.h \ + flooritemmanager.cpp \ + flooritemmanager.h \ game.cpp \ game.h \ graphics.cpp \ diff --git a/src/engine.cpp b/src/engine.cpp index a6cad8b8..4a637777 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -28,7 +28,7 @@ #include "being.h" #include "beingmanager.h" -#include "floor_item.h" +#include "flooritemmanager.h" #include "game.h" #include "graphics.h" #include "localplayer.h" @@ -114,7 +114,7 @@ Map *Engine::getCurrentMap() void Engine::changeMap(std::string mapPath) { // Clean up floor items - empty_floor_items(); + floorItemManager->clear(); beingManager->clear(); diff --git a/src/floor_item.cpp b/src/floor_item.cpp index 66adcd9b..775a9cb8 100755 --- a/src/floor_item.cpp +++ b/src/floor_item.cpp @@ -23,8 +23,6 @@ #include "floor_item.h" -#include <list> - #include "map.h" #include "graphic/spriteset.h" @@ -34,9 +32,6 @@ extern Spriteset *itemset; -typedef std::list<FloorItem*> FloorItems; -FloorItems floorItems; - FloorItem::FloorItem(unsigned int id, unsigned int itemId, unsigned short x, @@ -60,66 +55,3 @@ FloorItem::~FloorItem() // Remove and delete the representative sprite mMap->removeSprite(mSpriteIterator); } - -void empty_floor_items() -{ - FloorItems::iterator i; - for (i = floorItems.begin(); i != floorItems.end(); i++) { - delete (*i); - } - floorItems.clear(); -} - -void add_floor_item(FloorItem *floorItem) -{ - floorItems.push_back(floorItem); -} - -void remove_floor_item(unsigned int id) -{ - FloorItems::iterator i; - - for (i = floorItems.begin(); i != floorItems.end(); i++) - { - if ((*i)->getId() == id) - { - delete (*i); - floorItems.erase(i); - return; - } - } -} - -FloorItem* find_floor_item_by_cor(unsigned short x, unsigned short y) -{ - FloorItems::iterator i; - - for (i = floorItems.begin(); i != floorItems.end(); i++) - { - FloorItem *floorItem = (*i); - - if (floorItem->getX() == x && floorItem->getY() == y) - { - return floorItem; - } - } - - return 0; -} - -FloorItem *find_floor_item_by_id(unsigned int int_id) -{ - FloorItems::iterator i; - - for (i = floorItems.begin(); i != floorItems.end(); i++) - { - FloorItem *floorItem = (*i); - - if (floorItem->getId() == int_id) - { - return floorItem; - } - } - - return NULL; -} diff --git a/src/floor_item.h b/src/floor_item.h index 2471de2c..24eb495d 100755 --- a/src/floor_item.h +++ b/src/floor_item.h @@ -102,19 +102,4 @@ class FloorItem : public Sprite Map *mMap; }; -/** Removes all items from the list */ -void empty_floor_items(); - -/** Add an Item to the list */ -void add_floor_item(FloorItem *floorItem); - -/** Return a specific id FloorItem */ -FloorItem *find_floor_item_by_id(unsigned int int_id); - -/** Remove a FloorItem */ -void remove_floor_item(unsigned int int_id); - -/** Find a Item id based on its coordinates */ -FloorItem* find_floor_item_by_cor(unsigned short x, unsigned short y); - #endif diff --git a/src/flooritemmanager.cpp b/src/flooritemmanager.cpp new file mode 100644 index 00000000..f87c925b --- /dev/null +++ b/src/flooritemmanager.cpp @@ -0,0 +1,84 @@ +/* + * The Mana World + * 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$ + */ + +#include "flooritemmanager.h" + +#include "floor_item.h" + +FloorItemManager::~FloorItemManager() +{ + clear(); +} + +FloorItem* FloorItemManager::create(unsigned int id, unsigned int itemId, + unsigned short x, unsigned short y, Map *map) +{ + FloorItem *floorItem = new FloorItem(id, itemId, x, y, map); + mFloorItems.push_back(floorItem); + return floorItem; +} + +void FloorItemManager::destroy(FloorItem *item) +{ + mFloorItems.remove(item); + delete item; +} + +void FloorItemManager::clear() +{ + FloorItemIterator i; + for (i = mFloorItems.begin(); i != mFloorItems.end(); i++) + { + delete *i; + } + mFloorItems.clear(); +} + +FloorItem* FloorItemManager::findById(unsigned int id) +{ + FloorItemIterator i; + for (i = mFloorItems.begin(); i != mFloorItems.end(); i++) + { + if ((*i)->getId() == id) + { + return *i; + } + } + + return NULL; +} + +FloorItem* FloorItemManager::findByCoordinates(unsigned short x, + unsigned short y) +{ + FloorItemIterator i; + for (i = mFloorItems.begin(); i != mFloorItems.end(); i++) + { + if ((*i)->getX() == x && (*i)->getY() == y) + { + return *i; + } + } + + return NULL; +} diff --git a/src/flooritemmanager.h b/src/flooritemmanager.h new file mode 100644 index 00000000..c12883a4 --- /dev/null +++ b/src/flooritemmanager.h @@ -0,0 +1,57 @@ +/* + * The Mana World + * 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 _TMW_FLOORITEMMANAGER_H +#define _TMW_FLOORITEMMANAGER_H + +#include <list> + +class FloorItem; +class Map; + +class FloorItemManager +{ + public: + ~FloorItemManager(); + + FloorItem* create(unsigned int id, unsigned int itemId, + unsigned short x, unsigned short y, Map *map); + + void destroy(FloorItem *item); + + void clear(); + + FloorItem* findById(unsigned int id); + FloorItem* findByCoordinates(unsigned short x, unsigned short y); + + private: + typedef std::list<FloorItem*> FloorItems; + typedef FloorItems::iterator FloorItemIterator; + FloorItems mFloorItems; + +}; + +// TODO Get rid of the global? +extern FloorItemManager *floorItemManager; + +#endif diff --git a/src/game.cpp b/src/game.cpp index 75545de4..a2e0912b 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -34,7 +34,7 @@ #include "beingmanager.h" #include "configuration.h" #include "engine.h" -#include "floor_item.h" +#include "flooritemmanager.h" #include "graphics.h" #include "localplayer.h" #include "log.h" @@ -133,6 +133,7 @@ HelpWindow *helpWindow; DebugWindow *debugWindow; BeingManager *beingManager = NULL; +FloorItemManager *floorItemManager = NULL; const int MAX_TIME = 10000; @@ -277,6 +278,7 @@ Game::Game(Network *network): engine = new Engine(network); beingManager = new BeingManager(network); + floorItemManager = new FloorItemManager(); // Initialize timers tick_time = 0; @@ -352,6 +354,12 @@ Game::~Game() delete player_node; destroyGuiWindows(); + delete beingManager; + delete floorItemManager; + + beingManager = NULL; + floorItemManager = NULL; + if (joypad != NULL) { SDL_JoystickClose(joypad); @@ -536,7 +544,7 @@ void Game::handleInput() case SDLK_z: if (!chatWindow->isFocused()) { - FloorItem *item = find_floor_item_by_cor( + FloorItem *item = floorItemManager->findByCoordinates( player_node->x, player_node->y); // If none below the player, try the tile in front of @@ -557,7 +565,7 @@ void Game::handleInput() case Being::SE: x++; y++; break; default: break; } - item = find_floor_item_by_cor(x, y); + item = floorItemManager->findByCoordinates(x, y); } if (item) @@ -774,7 +782,7 @@ void Game::handleInput() if (joy[JOY_BTN1]) { - FloorItem *item = find_floor_item_by_cor( + FloorItem *item = floorItemManager->findByCoordinates( player_node->x, player_node->y); if (item) diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 3d8d8a98..0017e23a 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -43,7 +43,7 @@ #include "../configlistener.h" #include "../configuration.h" #include "../engine.h" -#include "../floor_item.h" +#include "../flooritemmanager.h" #include "../graphics.h" #include "../localplayer.h" #include "../log.h" @@ -265,7 +265,7 @@ Gui::mousePress(int mx, int my, int button) showPopup(mx, my, being); return; } - else if((floorItem = find_floor_item_by_cor(tilex, tiley))) + else if((floorItem = floorItemManager->findByCoordinates(tilex, tiley))) { showPopup(mx, my, floorItem); return; @@ -308,7 +308,7 @@ Gui::mousePress(int mx, int my, int button) } } // Pick up some item - else if ((item = find_floor_item_by_cor(tilex, tiley))) + else if ((item = floorItemManager->findByCoordinates(tilex, tiley))) { player_node->pickUp(item); } diff --git a/src/net/itemhandler.cpp b/src/net/itemhandler.cpp index d2a938fd..567a5382 100644 --- a/src/net/itemhandler.cpp +++ b/src/net/itemhandler.cpp @@ -27,7 +27,7 @@ #include "protocol.h" #include "../engine.h" -#include "../floor_item.h" +#include "../flooritemmanager.h" ItemHandler::ItemHandler() { @@ -57,11 +57,14 @@ void ItemHandler::handleMessage(MessageIn *msg) y = msg->readInt16(); msg->skip(4); // amount,subX,subY / subX,subY,amount - add_floor_item(new FloorItem(id, itemId, x, y, engine->getCurrentMap())); + floorItemManager->create(id, itemId, x, y, engine->getCurrentMap()); break; case SMSG_ITEM_REMOVE: - remove_floor_item(msg->readInt32()); + FloorItem *item; + item = floorItemManager->findById(msg->readInt32()); + if (item) + floorItemManager->destroy(item); break; } } |