00001 /* 00002 * The Mana World 00003 * Copyright (C) 2004 The Mana World Development Team 00004 * 00005 * This file is part of The Mana World. 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00022 #include "beingmanager.h" 00023 #include "engine.h" 00024 #include "flooritemmanager.h" 00025 #include "game.h" 00026 #include "localplayer.h" 00027 #include "log.h" 00028 #include "map.h" 00029 #include "particle.h" 00030 #include "sound.h" 00031 00032 #include "gui/gui.h" 00033 #include "gui/minimap.h" 00034 #include "gui/viewport.h" 00035 00036 #include "net/maphandler.h" 00037 #include "net/net.h" 00038 00039 #include "resources/mapreader.h" 00040 #include "resources/monsterdb.h" 00041 #include "resources/resourcemanager.h" 00042 00043 #include "utils/stringutils.h" 00044 00045 Engine::Engine(): 00046 mCurrentMap(NULL) 00047 { 00048 } 00049 00050 Engine::~Engine() 00051 { 00052 delete mCurrentMap; 00053 } 00054 00055 bool Engine::changeMap(const std::string &mapPath) 00056 { 00057 // Clean up floor items, beings and particles 00058 floorItemManager->clear(); 00059 beingManager->clear(); 00060 00061 // Close the popup menu on map change so that invalid options can't be 00062 // executed. 00063 viewport->closePopupMenu(); 00064 00065 // Unset the map of the player so that its particles are cleared before 00066 // being deleted in the next step 00067 if (player_node) 00068 player_node->setMap(0); 00069 00070 particleEngine->clear(); 00071 00072 mMapName = mapPath; 00073 00074 // Store full map path in global var 00075 map_path = "maps/" + mapPath + ".tmx"; 00076 ResourceManager *resman = ResourceManager::getInstance(); 00077 if (!resman->exists(map_path)) 00078 map_path += ".gz"; 00079 00080 // Attempt to load the new map 00081 Map *newMap = MapReader::readMap(map_path); 00082 00083 if (!newMap) 00084 logger->error("Could not find map file"); 00085 00086 // Notify the minimap and beingManager about the map change 00087 Image *mapImage = NULL; 00088 if (newMap->hasProperty("minimap")) 00089 { 00090 mapImage = resman->getImage(newMap->getProperty("minimap")); 00091 00092 // Set the title for the Minimap 00093 if (newMap->hasProperty("mapname")) 00094 minimap->setCaption(newMap->getProperty("mapname")); 00095 else if (newMap->hasProperty("name")) 00096 minimap->setCaption(newMap->getProperty("name")); 00097 else 00098 { 00099 minimap->setCaption("Unknown"); 00100 logger->log("WARNING: Map file '%s' defines a minimap image but " 00101 "does not define a 'mapname' property", 00102 map_path.c_str()); 00103 } 00104 00105 // How many pixels equal one tile. .5 (which is the TMW default) is 00106 // 2 tiles to a pixel, while 1 is 1 tile to 1 pixel 00107 if (newMap->hasProperty("minimapproportion")) 00108 minimap->setProportion(atof( 00109 newMap->getProperty("minimapproportion").c_str())); 00110 else 00111 minimap->setProportion(0.5); 00112 } 00113 if (newMap->hasProperty("name")) 00114 { 00115 minimap->setCaption(newMap->getProperty("name")); 00116 } else { 00117 minimap->setCaption("Map"); 00118 } 00119 minimap->setMapImage(mapImage); 00120 beingManager->setMap(newMap); 00121 particleEngine->setMap(newMap); 00122 viewport->setMap(newMap); 00123 00124 // Initialize map-based particle effects 00125 newMap->initializeParticleEffects(particleEngine); 00126 00127 // Start playing new music file when necessary 00128 std::string oldMusic = ""; 00129 00130 if (mCurrentMap) 00131 { 00132 oldMusic = mCurrentMap->getProperty("music"); 00133 delete mCurrentMap; 00134 } 00135 00136 std::string newMusic = newMap->getProperty("music"); 00137 00138 if (newMusic != oldMusic) 00139 sound.playMusic(newMusic); 00140 00141 mCurrentMap = newMap; 00142 00143 Net::getMapHandler()->mapLoaded(mapPath); 00144 return true; 00145 } 00146 00147 void Engine::logic() 00148 { 00149 beingManager->logic(); 00150 particleEngine->update(); 00151 gui->logic(); 00152 }