[6402] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
| 17 | |
---|
[7036] | 18 | #include <fstream> |
---|
| 19 | #include <iostream> |
---|
| 20 | |
---|
[6402] | 21 | #include "game_world_data.h" |
---|
| 22 | |
---|
[7193] | 23 | #include "util/loading/resource_manager.h" |
---|
[6402] | 24 | #include "state.h" |
---|
| 25 | #include "class_list.h" |
---|
| 26 | #include "substring.h" |
---|
| 27 | |
---|
[7193] | 28 | #include "util/loading/game_loader.h" |
---|
[6402] | 29 | |
---|
| 30 | #include "world_entity.h" |
---|
| 31 | #include "player.h" |
---|
| 32 | #include "camera.h" |
---|
[7287] | 33 | #include "terrain.h" |
---|
[7286] | 34 | #include "skybox.h" |
---|
[8490] | 35 | #include "md2/md2Model.h" |
---|
[7287] | 36 | #include "world_entities/projectiles/projectile.h" |
---|
| 37 | #include "npcs/npc_test1.h" |
---|
[6402] | 38 | #include "playable.h" |
---|
| 39 | |
---|
| 40 | #include "light.h" |
---|
| 41 | |
---|
[7193] | 42 | #include "util/loading/factory.h" |
---|
[6402] | 43 | #include "fast_factory.h" |
---|
[7193] | 44 | #include "util/loading/load_param.h" |
---|
[6402] | 45 | |
---|
| 46 | #include "graphics_engine.h" |
---|
[7810] | 47 | #include "effects/atmospheric_engine.h" |
---|
[6402] | 48 | #include "event_handler.h" |
---|
| 49 | #include "sound_engine.h" |
---|
| 50 | #include "cd_engine.h" |
---|
| 51 | #include "network_manager.h" |
---|
| 52 | #include "physics_engine.h" |
---|
| 53 | #include "fields.h" |
---|
| 54 | |
---|
| 55 | #include "glmenu_imagescreen.h" |
---|
| 56 | |
---|
[7020] | 57 | #include "game_rules.h" |
---|
| 58 | |
---|
[6402] | 59 | #include "ogg_player.h" |
---|
| 60 | #include "shader.h" |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | using namespace std; |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * constructor of the GameWorldData |
---|
| 68 | */ |
---|
| 69 | GameWorldData::GameWorldData() |
---|
| 70 | { |
---|
| 71 | this->setClassID(CL_GAME_WORLD_DATA, "GameWorldData"); |
---|
| 72 | |
---|
| 73 | this->glmis = NULL; |
---|
| 74 | |
---|
| 75 | this->localCamera = NULL; |
---|
| 76 | this->localPlayer = NULL; |
---|
| 77 | this->sky = NULL; |
---|
| 78 | this->terrain = NULL; |
---|
| 79 | |
---|
| 80 | this->music = NULL; |
---|
| 81 | this->objectManager = NULL; |
---|
[7035] | 82 | this->gameRule = NULL; |
---|
[6402] | 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * destructor for the GameWorldData |
---|
| 88 | */ |
---|
| 89 | GameWorldData::~GameWorldData() |
---|
[6404] | 90 | {} |
---|
[6402] | 91 | |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * initialize the GameWorldData |
---|
| 96 | */ |
---|
| 97 | ErrorMessage GameWorldData::init() |
---|
| 98 | { |
---|
| 99 | this->objectManager = new ObjectManager(); |
---|
| 100 | State::setObjectManager(this->objectManager); |
---|
| 101 | |
---|
| 102 | PNode::getNullParent(); |
---|
| 103 | this->localCamera = new Camera(); |
---|
| 104 | this->localCamera->setName ("GameWorld-Camera"); |
---|
| 105 | State::setCamera(this->localCamera, this->localCamera->getTarget()); |
---|
| 106 | |
---|
| 107 | LightManager::getInstance(); |
---|
| 108 | |
---|
[8975] | 109 | // GraphicsEngine::getInstance()->displayFPS(true); |
---|
[8717] | 110 | |
---|
| 111 | return ErrorMessage(); |
---|
[6402] | 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * loads the data from the xml file |
---|
| 117 | * @param root reference to the xml root element |
---|
| 118 | */ |
---|
[7370] | 119 | ErrorMessage GameWorldData::loadData(const TiXmlElement* root) |
---|
[6402] | 120 | { |
---|
| 121 | // load the parameters |
---|
| 122 | // name |
---|
[7221] | 123 | std::string string = grabParameter( root, "name"); |
---|
| 124 | if( string.empty() ) |
---|
[6402] | 125 | { |
---|
| 126 | PRINTF(2)("GameWorld is missing a proper 'name'\n"); |
---|
| 127 | this->setName("Unknown"); |
---|
| 128 | } |
---|
| 129 | else |
---|
[7221] | 130 | this->setName(string.c_str()); |
---|
[6402] | 131 | |
---|
| 132 | this->loadGUI(root); |
---|
| 133 | this->loadWorldEntities(root); |
---|
| 134 | this->loadScene(root); |
---|
[8717] | 135 | |
---|
| 136 | return ErrorMessage(); |
---|
[6402] | 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * unloads the data from the xml file |
---|
| 142 | */ |
---|
| 143 | ErrorMessage GameWorldData::unloadData() |
---|
| 144 | { |
---|
| 145 | this->unloadGUI(); |
---|
| 146 | this->unloadWorldEntities(); |
---|
| 147 | this->unloadScene(); |
---|
[8717] | 148 | |
---|
| 149 | return ErrorMessage(); |
---|
[6402] | 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | /** |
---|
[7370] | 154 | * @brief loads the GUI data |
---|
[6402] | 155 | * @param root reference to the xml root element |
---|
| 156 | */ |
---|
[7370] | 157 | ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root) |
---|
[6402] | 158 | { |
---|
[7370] | 159 | const TiXmlElement* element = root->FirstChildElement("LoadScreen"); |
---|
[6402] | 160 | if( element == NULL) |
---|
| 161 | { |
---|
| 162 | PRINTF(2)("no LoadScreen specified, loading default\n"); |
---|
| 163 | |
---|
| 164 | glmis->setBackgroundImage("pictures/load_screen.jpg"); |
---|
| 165 | this->glmis->setMaximum(8); |
---|
[6988] | 166 | // this->glmis->draw(); |
---|
[6402] | 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | this->glmis->loadParams(element); |
---|
[6988] | 171 | // this->glmis->draw(); |
---|
[6402] | 172 | } |
---|
| 173 | this->glmis->draw(); |
---|
[8717] | 174 | |
---|
| 175 | return ErrorMessage(); |
---|
[6402] | 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | /** |
---|
[7370] | 180 | * @brief unloads the GUI data |
---|
[6402] | 181 | */ |
---|
| 182 | ErrorMessage GameWorldData::unloadGUI() |
---|
| 183 | { |
---|
| 184 | delete this->glmis; |
---|
[8717] | 185 | |
---|
| 186 | return ErrorMessage(); |
---|
[6402] | 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | /** |
---|
[7370] | 191 | * @brief loads the world entities from the xml file |
---|
[6402] | 192 | * @param root reference to the xml root parameter |
---|
| 193 | */ |
---|
[7370] | 194 | ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root) |
---|
[6402] | 195 | { |
---|
[7370] | 196 | const TiXmlElement* element = root->FirstChildElement("WorldEntities"); |
---|
[6402] | 197 | |
---|
| 198 | if( element == NULL) |
---|
| 199 | { |
---|
| 200 | PRINTF(1)("GameWorld is missing 'WorldEntities'\n"); |
---|
| 201 | } |
---|
| 202 | else |
---|
| 203 | { |
---|
| 204 | element = element->FirstChildElement(); |
---|
| 205 | // load Players/Objects/Whatever |
---|
| 206 | PRINTF(4)("Loading WorldEntities\n"); |
---|
| 207 | while( element != NULL) |
---|
| 208 | { |
---|
| 209 | BaseObject* created = Factory::fabricate(element); |
---|
| 210 | if( created != NULL ) |
---|
[6834] | 211 | PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName()); |
---|
[6402] | 212 | |
---|
| 213 | //todo do this more elegant |
---|
[7370] | 214 | if( element->Value() == "SkyBox" && created->isA(CL_SKYBOX)) |
---|
[6771] | 215 | { |
---|
[6402] | 216 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
[6771] | 217 | State::setSkyBox(dynamic_cast<SkyBox*>(this->sky)); |
---|
| 218 | } |
---|
[7370] | 219 | if( element->Value() == "Terrain" && created->isA(CL_TERRAIN)) |
---|
[6402] | 220 | { |
---|
| 221 | this->terrain = dynamic_cast<Terrain*>(created); |
---|
| 222 | CDEngine::getInstance()->setTerrain(terrain); |
---|
| 223 | } |
---|
| 224 | element = element->NextSiblingElement(); |
---|
| 225 | this->glmis->step(); //! @todo temporary |
---|
| 226 | } |
---|
| 227 | PRINTF(4)("Done loading WorldEntities\n"); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | // Create a Player |
---|
| 231 | this->localPlayer = new Player(); |
---|
[7076] | 232 | State::setPlayer(this->localPlayer); |
---|
[6402] | 233 | |
---|
| 234 | Playable* playable; |
---|
| 235 | const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE); |
---|
[7370] | 236 | if (playableList != NULL && !playableList->empty()) |
---|
[6402] | 237 | { |
---|
[7370] | 238 | /// TODO Make this also loadable |
---|
[6402] | 239 | playable = dynamic_cast<Playable*>(playableList->front()); |
---|
[6985] | 240 | this->localPlayer->setPlayable(playable); |
---|
[6402] | 241 | } |
---|
| 242 | |
---|
[7370] | 243 | // Fill the EntityLists. Tick then Draw: |
---|
| 244 | this->tickLists.push_back(OM_DEAD_TICK); |
---|
| 245 | this->tickLists.push_back(OM_ENVIRON); |
---|
| 246 | this->tickLists.push_back(OM_COMMON); |
---|
| 247 | this->tickLists.push_back(OM_GROUP_00); |
---|
| 248 | this->tickLists.push_back(OM_GROUP_00_PROJ); |
---|
| 249 | this->tickLists.push_back(OM_GROUP_01); |
---|
| 250 | this->tickLists.push_back(OM_GROUP_01_PROJ); |
---|
| 251 | |
---|
| 252 | this->drawLists.push_back(OM_ENVIRON_NOTICK); |
---|
| 253 | this->drawLists.push_back(OM_ENVIRON); |
---|
| 254 | this->drawLists.push_back(OM_COMMON); |
---|
| 255 | this->drawLists.push_back(OM_GROUP_00); |
---|
| 256 | this->drawLists.push_back(OM_GROUP_00_PROJ); |
---|
| 257 | this->drawLists.push_back(OM_GROUP_01); |
---|
| 258 | this->drawLists.push_back(OM_GROUP_01_PROJ); |
---|
| 259 | |
---|
[6402] | 260 | /* init the pnode tree */ |
---|
| 261 | PNode::getNullParent()->init(); |
---|
[8717] | 262 | |
---|
| 263 | return ErrorMessage(); |
---|
[6402] | 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | /** |
---|
[6404] | 268 | * unloads the world entities |
---|
[6402] | 269 | */ |
---|
| 270 | ErrorMessage GameWorldData::unloadWorldEntities() |
---|
| 271 | { |
---|
| 272 | FastFactory::flushAll(true); |
---|
[7029] | 273 | GraphicsEngine::getInstance()->displayFPS(false); |
---|
[6402] | 274 | // erease everything that is left. |
---|
[6626] | 275 | // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner) |
---|
[6981] | 276 | const std::list<BaseObject*>* nodeList; |
---|
[6402] | 277 | //secondary cleanup of PNodes; |
---|
[6981] | 278 | nodeList = ClassList::getList(CL_PARENT_NODE); |
---|
[6402] | 279 | if (nodeList != NULL) |
---|
| 280 | while (!nodeList->empty()) |
---|
[7370] | 281 | { |
---|
| 282 | // ClassList::debug( 3, CL_PARENT_NODE); |
---|
| 283 | // PNode::getNullParent()->debugNode(0); |
---|
| 284 | // printf("%s::%s\n", nodeList->front()->getClassName(), nodeList->front()->getName()); |
---|
| 285 | delete nodeList->front(); |
---|
| 286 | } |
---|
[6402] | 287 | /* remove the player object */ |
---|
| 288 | if( this->localPlayer) |
---|
| 289 | delete this->localPlayer; |
---|
[7126] | 290 | State::setPlayer(NULL); |
---|
[7311] | 291 | this->localPlayer = NULL; |
---|
| 292 | this->localCamera = NULL; |
---|
| 293 | State::setCamera(NULL, NULL); |
---|
| 294 | this->sky = NULL; |
---|
| 295 | this->terrain = NULL; |
---|
[6402] | 296 | |
---|
[6988] | 297 | nodeList = ClassList::getList(CL_GRAPHICS_EFFECT); |
---|
| 298 | if (nodeList != NULL) |
---|
| 299 | while (!nodeList->empty()) |
---|
| 300 | delete nodeList->front(); |
---|
[6981] | 301 | |
---|
| 302 | |
---|
[7029] | 303 | nodeList = ClassList::getList(CL_ELEMENT_2D); |
---|
[7370] | 304 | if (nodeList != NULL) |
---|
| 305 | while (!nodeList->empty()) |
---|
| 306 | delete nodeList->front(); |
---|
[6981] | 307 | |
---|
[7370] | 308 | // At this Point all the WorldEntites should be unloaded. |
---|
| 309 | this->tickLists.clear(); |
---|
| 310 | this->drawLists.clear(); |
---|
[6981] | 311 | |
---|
[7370] | 312 | // unload the resources loaded in this Level !! |
---|
[6402] | 313 | ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
| 314 | |
---|
| 315 | if (State::getObjectManager() == this->objectManager) |
---|
| 316 | { |
---|
| 317 | State::setObjectManager(NULL); |
---|
| 318 | delete this->objectManager; |
---|
| 319 | } |
---|
[7311] | 320 | this->objectManager = NULL; |
---|
[6863] | 321 | |
---|
[6771] | 322 | if(State::getSkyBox()) |
---|
| 323 | State::setSkyBox(NULL); |
---|
[6863] | 324 | |
---|
[7311] | 325 | this->glmis = NULL; |
---|
[8717] | 326 | |
---|
| 327 | return ErrorMessage(); |
---|
[6402] | 328 | } |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | /** |
---|
[7370] | 332 | * @brief loads the scene data |
---|
[6402] | 333 | * @param root reference to the xml root element |
---|
| 334 | */ |
---|
[7370] | 335 | ErrorMessage GameWorldData::loadScene(const TiXmlElement* root) |
---|
[6402] | 336 | { |
---|
| 337 | LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams); |
---|
[6815] | 338 | LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams); |
---|
[7810] | 339 | LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams); |
---|
[6402] | 340 | |
---|
[6827] | 341 | LoadParam(root, "Music", this, GameWorldData, setSoundTrack); |
---|
| 342 | |
---|
[7020] | 343 | LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule); |
---|
| 344 | |
---|
| 345 | |
---|
| 346 | //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams); |
---|
[6402] | 347 | //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams); |
---|
| 348 | |
---|
| 349 | this->localCamera->setClipRegion(1, 10000.0); |
---|
| 350 | if( this->sky != NULL) |
---|
| 351 | this->localCamera->addChild(this->sky); |
---|
[7460] | 352 | OrxSound::SoundEngine::getInstance()->setListener(this->localCamera); |
---|
[8717] | 353 | |
---|
| 354 | return ErrorMessage(); |
---|
[6402] | 355 | } |
---|
| 356 | |
---|
| 357 | |
---|
[7020] | 358 | |
---|
[6402] | 359 | /** |
---|
| 360 | * unloads the scene data |
---|
| 361 | */ |
---|
| 362 | ErrorMessage GameWorldData::unloadScene() |
---|
| 363 | { |
---|
| 364 | /* delete some garphics and scene eingines */ |
---|
| 365 | delete LightManager::getInstance(); |
---|
[7810] | 366 | delete AtmosphericEngine::getInstance(); |
---|
[6402] | 367 | |
---|
[7287] | 368 | if (this->music != NULL) |
---|
| 369 | this->setSoundTrack(""); |
---|
[6402] | 370 | |
---|
| 371 | /* unload the shaders */ |
---|
| 372 | Shader::suspendShader(); |
---|
[7488] | 373 | |
---|
| 374 | State::setGameRules(NULL); |
---|
[8717] | 375 | |
---|
| 376 | return ErrorMessage(); |
---|
[6402] | 377 | } |
---|
| 378 | |
---|
[6634] | 379 | |
---|
[7221] | 380 | void GameWorldData::setSoundTrack(const std::string& name) |
---|
[6634] | 381 | { |
---|
[6827] | 382 | if (this->music != NULL) |
---|
[7287] | 383 | delete this->music; |
---|
| 384 | this->music = NULL; |
---|
[6827] | 385 | |
---|
[7221] | 386 | if (!name.empty()) |
---|
[6987] | 387 | { |
---|
[7221] | 388 | PRINTF(3)("Setting Sound Track to %s\n", name.c_str()); |
---|
| 389 | std::string oggFile = ResourceManager::getFullName(name); |
---|
[7460] | 390 | this->music = new OrxSound::OggPlayer(oggFile); |
---|
[9019] | 391 | if (this->localPlayer != NULL) |
---|
| 392 | this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title()); |
---|
[6988] | 393 | |
---|
| 394 | //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL); |
---|
| 395 | //assert(this->music->isA(CL_SOUND_OGG_PLAYER)); |
---|
[6987] | 396 | } |
---|
[6634] | 397 | } |
---|
| 398 | |
---|
| 399 | |
---|
[7020] | 400 | void GameWorldData::loadGameRule(const TiXmlElement* root) |
---|
| 401 | { |
---|
[7036] | 402 | const TiXmlElement* element = root->FirstChildElement(); |
---|
| 403 | while( element != NULL) |
---|
[7020] | 404 | { |
---|
[7461] | 405 | PRINTF(2)("============ GameRules ==\n"); |
---|
| 406 | PRINTF(2)("creating %s\n", element->Value()); |
---|
[7036] | 407 | BaseObject* created = Factory::fabricate(element); |
---|
[7466] | 408 | if (created != NULL && created->isA(CL_GAME_RULES)) |
---|
[7020] | 409 | { |
---|
[7036] | 410 | this->gameRule = dynamic_cast<GameRules*>(created); |
---|
[7039] | 411 | State::setGameRules(this->gameRule); |
---|
[7482] | 412 | // if there is a valid game rule loaded, return because it is not thought to load multiple game rules |
---|
[7462] | 413 | return; |
---|
[7020] | 414 | } |
---|
[7036] | 415 | else |
---|
| 416 | { |
---|
| 417 | PRINTF(1)("Could not create a %s\n", element->Value()); |
---|
| 418 | delete created; |
---|
| 419 | } |
---|
| 420 | element = element->NextSiblingElement(); |
---|
[7020] | 421 | } |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | |
---|
| 425 | |
---|