[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 | |
---|
[7287] | 30 | #include "p_node.h" |
---|
[6402] | 31 | #include "world_entity.h" |
---|
| 32 | #include "player.h" |
---|
| 33 | #include "camera.h" |
---|
| 34 | #include "environment.h" |
---|
[7287] | 35 | #include "terrain.h" |
---|
| 36 | #include "test_entity.h" |
---|
| 37 | #include "terrain.h" |
---|
[7286] | 38 | #include "skybox.h" |
---|
[7287] | 39 | #include "md2Model.h" |
---|
| 40 | #include "world_entities/projectiles/projectile.h" |
---|
| 41 | #include "npcs/npc_test1.h" |
---|
[6402] | 42 | #include "playable.h" |
---|
| 43 | |
---|
| 44 | #include "light.h" |
---|
| 45 | |
---|
[7193] | 46 | #include "util/loading/factory.h" |
---|
[6402] | 47 | #include "fast_factory.h" |
---|
[7193] | 48 | #include "util/loading/load_param.h" |
---|
[6402] | 49 | |
---|
| 50 | #include "graphics_engine.h" |
---|
| 51 | #include "event_handler.h" |
---|
| 52 | #include "sound_engine.h" |
---|
| 53 | #include "cd_engine.h" |
---|
| 54 | #include "network_manager.h" |
---|
| 55 | #include "physics_engine.h" |
---|
| 56 | #include "fields.h" |
---|
| 57 | |
---|
| 58 | #include "glmenu_imagescreen.h" |
---|
| 59 | |
---|
| 60 | #include "animation_player.h" |
---|
| 61 | #include "animation3d.h" |
---|
| 62 | |
---|
[7020] | 63 | #include "game_rules.h" |
---|
| 64 | |
---|
[6402] | 65 | #include "ogg_player.h" |
---|
| 66 | #include "shader.h" |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | using namespace std; |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * constructor of the GameWorldData |
---|
| 74 | */ |
---|
| 75 | GameWorldData::GameWorldData() |
---|
| 76 | { |
---|
| 77 | this->setClassID(CL_GAME_WORLD_DATA, "GameWorldData"); |
---|
| 78 | |
---|
| 79 | this->glmis = NULL; |
---|
| 80 | |
---|
| 81 | this->localCamera = NULL; |
---|
| 82 | this->localPlayer = NULL; |
---|
| 83 | this->sky = NULL; |
---|
| 84 | this->terrain = NULL; |
---|
| 85 | |
---|
| 86 | this->music = NULL; |
---|
| 87 | this->objectManager = NULL; |
---|
[7035] | 88 | this->gameRule = NULL; |
---|
[6402] | 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * destructor for the GameWorldData |
---|
| 94 | */ |
---|
| 95 | GameWorldData::~GameWorldData() |
---|
[6404] | 96 | {} |
---|
[6402] | 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * initialize the GameWorldData |
---|
| 102 | */ |
---|
| 103 | ErrorMessage GameWorldData::init() |
---|
| 104 | { |
---|
| 105 | this->objectManager = new ObjectManager(); |
---|
| 106 | State::setObjectManager(this->objectManager); |
---|
| 107 | |
---|
| 108 | PNode::getNullParent(); |
---|
| 109 | this->localCamera = new Camera(); |
---|
| 110 | this->localCamera->setName ("GameWorld-Camera"); |
---|
| 111 | State::setCamera(this->localCamera, this->localCamera->getTarget()); |
---|
| 112 | |
---|
| 113 | LightManager::getInstance(); |
---|
| 114 | |
---|
| 115 | GraphicsEngine::getInstance()->displayFPS(true); |
---|
| 116 | |
---|
| 117 | /* initialize some graphics engines and graphical elements */ |
---|
| 118 | AnimationPlayer::getInstance(); |
---|
| 119 | PhysicsEngine::getInstance(); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * loads the data from the xml file |
---|
| 125 | * @param root reference to the xml root element |
---|
| 126 | */ |
---|
[7287] | 127 | ErrorMessage GameWorldData::loadData(TiXmlElement* root) |
---|
[6402] | 128 | { |
---|
| 129 | // load the parameters |
---|
| 130 | // name |
---|
[7221] | 131 | std::string string = grabParameter( root, "name"); |
---|
| 132 | if( string.empty() ) |
---|
[6402] | 133 | { |
---|
| 134 | PRINTF(2)("GameWorld is missing a proper 'name'\n"); |
---|
| 135 | this->setName("Unknown"); |
---|
| 136 | } |
---|
| 137 | else |
---|
[7221] | 138 | this->setName(string.c_str()); |
---|
[6402] | 139 | |
---|
| 140 | this->loadGUI(root); |
---|
| 141 | this->loadWorldEntities(root); |
---|
| 142 | this->loadScene(root); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * unloads the data from the xml file |
---|
| 148 | */ |
---|
| 149 | ErrorMessage GameWorldData::unloadData() |
---|
| 150 | { |
---|
| 151 | this->unloadGUI(); |
---|
| 152 | this->unloadWorldEntities(); |
---|
| 153 | this->unloadScene(); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * loads the GUI data |
---|
| 159 | * @param root reference to the xml root element |
---|
| 160 | */ |
---|
[7287] | 161 | ErrorMessage GameWorldData::loadGUI(TiXmlElement* root) |
---|
[6402] | 162 | { |
---|
[7287] | 163 | TiXmlElement* element = root->FirstChildElement("LoadScreen"); |
---|
[6402] | 164 | if( element == NULL) |
---|
| 165 | { |
---|
| 166 | PRINTF(2)("no LoadScreen specified, loading default\n"); |
---|
| 167 | |
---|
| 168 | glmis->setBackgroundImage("pictures/load_screen.jpg"); |
---|
| 169 | this->glmis->setMaximum(8); |
---|
[6988] | 170 | // this->glmis->draw(); |
---|
[6402] | 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | this->glmis->loadParams(element); |
---|
[6988] | 175 | // this->glmis->draw(); |
---|
[6402] | 176 | } |
---|
| 177 | this->glmis->draw(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | /** |
---|
[6404] | 182 | * unloads the GUI data |
---|
[6402] | 183 | */ |
---|
| 184 | ErrorMessage GameWorldData::unloadGUI() |
---|
| 185 | { |
---|
| 186 | delete this->glmis; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * loads the world entities from the xml file |
---|
| 192 | * @param root reference to the xml root parameter |
---|
| 193 | */ |
---|
[7287] | 194 | ErrorMessage GameWorldData::loadWorldEntities(TiXmlElement* root) |
---|
[6402] | 195 | { |
---|
[7287] | 196 | 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 |
---|
| 214 | if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) |
---|
[6771] | 215 | { |
---|
[6402] | 216 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
[6771] | 217 | State::setSkyBox(dynamic_cast<SkyBox*>(this->sky)); |
---|
| 218 | } |
---|
[6402] | 219 | if( element->Value() != NULL && !strcmp( element->Value(), "Terrain")) |
---|
| 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); |
---|
| 236 | if (playableList != NULL) |
---|
| 237 | { |
---|
| 238 | playable = dynamic_cast<Playable*>(playableList->front()); |
---|
[6985] | 239 | this->localPlayer->setPlayable(playable); |
---|
[6402] | 240 | } |
---|
| 241 | |
---|
| 242 | /* init the pnode tree */ |
---|
| 243 | PNode::getNullParent()->init(); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | /** |
---|
[6404] | 248 | * unloads the world entities |
---|
[6402] | 249 | */ |
---|
| 250 | ErrorMessage GameWorldData::unloadWorldEntities() |
---|
| 251 | { |
---|
| 252 | FastFactory::flushAll(true); |
---|
[7029] | 253 | GraphicsEngine::getInstance()->displayFPS(false); |
---|
[6402] | 254 | // erease everything that is left. |
---|
[6626] | 255 | // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner) |
---|
[6981] | 256 | const std::list<BaseObject*>* nodeList; |
---|
[6402] | 257 | //secondary cleanup of PNodes; |
---|
[6981] | 258 | nodeList = ClassList::getList(CL_PARENT_NODE); |
---|
[6402] | 259 | if (nodeList != NULL) |
---|
| 260 | while (!nodeList->empty()) |
---|
[7125] | 261 | { |
---|
[7126] | 262 | // ClassList::debug( 3, CL_PARENT_NODE); |
---|
| 263 | // PNode::getNullParent()->debugNode(0); |
---|
| 264 | // printf("%s::%s\n", nodeList->front()->getClassName(), nodeList->front()->getName()); |
---|
[7125] | 265 | delete nodeList->front(); |
---|
| 266 | } |
---|
[6402] | 267 | /* remove the player object */ |
---|
| 268 | if( this->localPlayer) |
---|
| 269 | delete this->localPlayer; |
---|
[7126] | 270 | State::setPlayer(NULL); |
---|
[7311] | 271 | this->localPlayer = NULL; |
---|
| 272 | this->localCamera = NULL; |
---|
| 273 | State::setCamera(NULL, NULL); |
---|
| 274 | this->sky = NULL; |
---|
| 275 | this->terrain = NULL; |
---|
[6402] | 276 | |
---|
[6988] | 277 | nodeList = ClassList::getList(CL_GRAPHICS_EFFECT); |
---|
| 278 | if (nodeList != NULL) |
---|
| 279 | while (!nodeList->empty()) |
---|
| 280 | delete nodeList->front(); |
---|
[6981] | 281 | |
---|
| 282 | |
---|
[7029] | 283 | nodeList = ClassList::getList(CL_ELEMENT_2D); |
---|
| 284 | if (nodeList != NULL) |
---|
| 285 | while (!nodeList->empty()) |
---|
| 286 | delete nodeList->front(); |
---|
[6981] | 287 | |
---|
| 288 | |
---|
[7029] | 289 | |
---|
| 290 | |
---|
[6402] | 291 | // unload the resources !! |
---|
| 292 | ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
| 293 | |
---|
| 294 | if (State::getObjectManager() == this->objectManager) |
---|
| 295 | { |
---|
| 296 | State::setObjectManager(NULL); |
---|
| 297 | delete this->objectManager; |
---|
| 298 | } |
---|
[7311] | 299 | this->objectManager = NULL; |
---|
[6863] | 300 | |
---|
[6771] | 301 | if(State::getSkyBox()) |
---|
| 302 | State::setSkyBox(NULL); |
---|
[6863] | 303 | |
---|
[7311] | 304 | this->glmis = NULL; |
---|
[6402] | 305 | } |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | /** |
---|
| 309 | * loads the scene data |
---|
| 310 | * @param root reference to the xml root element |
---|
| 311 | */ |
---|
[7287] | 312 | ErrorMessage GameWorldData::loadScene(TiXmlElement* root) |
---|
[6402] | 313 | { |
---|
| 314 | LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams); |
---|
[6815] | 315 | LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams); |
---|
[6402] | 316 | |
---|
[6827] | 317 | LoadParam(root, "Music", this, GameWorldData, setSoundTrack); |
---|
| 318 | |
---|
[7020] | 319 | |
---|
| 320 | LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule); |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams); |
---|
[6402] | 324 | //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams); |
---|
| 325 | |
---|
| 326 | this->localCamera->setClipRegion(1, 10000.0); |
---|
| 327 | if( this->sky != NULL) |
---|
| 328 | this->localCamera->addChild(this->sky); |
---|
| 329 | SoundEngine::getInstance()->setListener(this->localCamera); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | |
---|
[7020] | 333 | |
---|
[6402] | 334 | /** |
---|
| 335 | * unloads the scene data |
---|
| 336 | */ |
---|
| 337 | ErrorMessage GameWorldData::unloadScene() |
---|
| 338 | { |
---|
| 339 | /* delete some garphics and scene eingines */ |
---|
| 340 | delete LightManager::getInstance(); |
---|
| 341 | delete AnimationPlayer::getInstance(); |
---|
| 342 | delete PhysicsEngine::getInstance(); |
---|
| 343 | |
---|
[7287] | 344 | if (this->music != NULL) |
---|
| 345 | this->setSoundTrack(""); |
---|
[6402] | 346 | |
---|
| 347 | /* unload the shaders */ |
---|
| 348 | Shader::suspendShader(); |
---|
| 349 | } |
---|
| 350 | |
---|
[6634] | 351 | |
---|
[7221] | 352 | void GameWorldData::setSoundTrack(const std::string& name) |
---|
[6634] | 353 | { |
---|
[6827] | 354 | if (this->music != NULL) |
---|
[7287] | 355 | delete this->music; |
---|
| 356 | this->music = NULL; |
---|
[6827] | 357 | |
---|
[7221] | 358 | if (!name.empty()) |
---|
[6987] | 359 | { |
---|
[7221] | 360 | PRINTF(3)("Setting Sound Track to %s\n", name.c_str()); |
---|
| 361 | std::string oggFile = ResourceManager::getFullName(name); |
---|
[7287] | 362 | this->music = new OggPlayer(oggFile); |
---|
[6988] | 363 | |
---|
| 364 | //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL); |
---|
| 365 | //assert(this->music->isA(CL_SOUND_OGG_PLAYER)); |
---|
[6987] | 366 | } |
---|
[6634] | 367 | } |
---|
| 368 | |
---|
| 369 | |
---|
[7020] | 370 | void GameWorldData::loadGameRule(const TiXmlElement* root) |
---|
| 371 | { |
---|
[7036] | 372 | const TiXmlElement* element = root->FirstChildElement(); |
---|
| 373 | while( element != NULL) |
---|
[7020] | 374 | { |
---|
[7036] | 375 | PRINTF(4)("creating %s\n", element->Value()); |
---|
| 376 | BaseObject* created = Factory::fabricate(element); |
---|
| 377 | if (created != NULL /*&& created->isA(CL_GAME_RULES)*/) |
---|
[7020] | 378 | { |
---|
[7036] | 379 | this->gameRule = dynamic_cast<GameRules*>(created); |
---|
[7039] | 380 | State::setGameRules(this->gameRule); |
---|
[7020] | 381 | } |
---|
[7036] | 382 | else |
---|
| 383 | { |
---|
| 384 | PRINTF(1)("Could not create a %s\n", element->Value()); |
---|
| 385 | delete created; |
---|
| 386 | } |
---|
| 387 | element = element->NextSiblingElement(); |
---|
[7020] | 388 | } |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | |
---|
| 392 | |
---|