/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program 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, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD #include #include #include "game_world_data.h" #include "util/loading/resource_manager.h" #include "state.h" #include "class_list.h" #include "substring.h" #include "util/loading/game_loader.h" #include "world_entity.h" #include "player.h" #include "camera.h" #include "terrain.h" #include "skybox.h" #include "md2Model.h" #include "world_entities/projectiles/projectile.h" #include "npcs/npc_test1.h" #include "playable.h" #include "light.h" #include "util/loading/factory.h" #include "fast_factory.h" #include "util/loading/load_param.h" #include "graphics_engine.h" #include "event_handler.h" #include "sound_engine.h" #include "cd_engine.h" #include "network_manager.h" #include "physics_engine.h" #include "fields.h" #include "glmenu_imagescreen.h" #include "game_rules.h" #include "ogg_player.h" #include "shader.h" using namespace std; /** * constructor of the GameWorldData */ GameWorldData::GameWorldData() { this->setClassID(CL_GAME_WORLD_DATA, "GameWorldData"); this->glmis = NULL; this->localCamera = NULL; this->localPlayer = NULL; this->sky = NULL; this->terrain = NULL; this->music = NULL; this->objectManager = NULL; this->gameRule = NULL; } /** * destructor for the GameWorldData */ GameWorldData::~GameWorldData() {} /** * initialize the GameWorldData */ ErrorMessage GameWorldData::init() { this->objectManager = new ObjectManager(); State::setObjectManager(this->objectManager); PNode::getNullParent(); this->localCamera = new Camera(); this->localCamera->setName ("GameWorld-Camera"); State::setCamera(this->localCamera, this->localCamera->getTarget()); LightManager::getInstance(); GraphicsEngine::getInstance()->displayFPS(true); } /** * loads the data from the xml file * @param root reference to the xml root element */ ErrorMessage GameWorldData::loadData(const TiXmlElement* root) { // load the parameters // name std::string string = grabParameter( root, "name"); if( string.empty() ) { PRINTF(2)("GameWorld is missing a proper 'name'\n"); this->setName("Unknown"); } else this->setName(string.c_str()); this->loadGUI(root); this->loadWorldEntities(root); this->loadScene(root); } /** * unloads the data from the xml file */ ErrorMessage GameWorldData::unloadData() { this->unloadGUI(); this->unloadWorldEntities(); this->unloadScene(); } /** * @brief loads the GUI data * @param root reference to the xml root element */ ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root) { const TiXmlElement* element = root->FirstChildElement("LoadScreen"); if( element == NULL) { PRINTF(2)("no LoadScreen specified, loading default\n"); glmis->setBackgroundImage("pictures/load_screen.jpg"); this->glmis->setMaximum(8); // this->glmis->draw(); } else { this->glmis->loadParams(element); // this->glmis->draw(); } this->glmis->draw(); } /** * @brief unloads the GUI data */ ErrorMessage GameWorldData::unloadGUI() { delete this->glmis; } /** * @brief loads the world entities from the xml file * @param root reference to the xml root parameter */ ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root) { const TiXmlElement* element = root->FirstChildElement("WorldEntities"); if( element == NULL) { PRINTF(1)("GameWorld is missing 'WorldEntities'\n"); } else { element = element->FirstChildElement(); // load Players/Objects/Whatever PRINTF(4)("Loading WorldEntities\n"); while( element != NULL) { BaseObject* created = Factory::fabricate(element); if( created != NULL ) PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName()); //todo do this more elegant if( element->Value() == "SkyBox" && created->isA(CL_SKYBOX)) { this->sky = dynamic_cast(created); State::setSkyBox(dynamic_cast(this->sky)); } if( element->Value() == "Terrain" && created->isA(CL_TERRAIN)) { this->terrain = dynamic_cast(created); CDEngine::getInstance()->setTerrain(terrain); } element = element->NextSiblingElement(); this->glmis->step(); //! @todo temporary } PRINTF(4)("Done loading WorldEntities\n"); } // Create a Player this->localPlayer = new Player(); State::setPlayer(this->localPlayer); Playable* playable; const list* playableList = ClassList::getList(CL_PLAYABLE); if (playableList != NULL && !playableList->empty()) { /// TODO Make this also loadable playable = dynamic_cast(playableList->front()); this->localPlayer->setPlayable(playable); } // Fill the EntityLists. Tick then Draw: this->tickLists.push_back(OM_DEAD_TICK); this->tickLists.push_back(OM_ENVIRON); this->tickLists.push_back(OM_COMMON); this->tickLists.push_back(OM_GROUP_00); this->tickLists.push_back(OM_GROUP_00_PROJ); this->tickLists.push_back(OM_GROUP_01); this->tickLists.push_back(OM_GROUP_01_PROJ); this->drawLists.push_back(OM_ENVIRON_NOTICK); this->drawLists.push_back(OM_ENVIRON); this->drawLists.push_back(OM_COMMON); this->drawLists.push_back(OM_GROUP_00); this->drawLists.push_back(OM_GROUP_00_PROJ); this->drawLists.push_back(OM_GROUP_01); this->drawLists.push_back(OM_GROUP_01_PROJ); /* init the pnode tree */ PNode::getNullParent()->init(); } /** * unloads the world entities */ ErrorMessage GameWorldData::unloadWorldEntities() { FastFactory::flushAll(true); GraphicsEngine::getInstance()->displayFPS(false); // erease everything that is left. // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner) const std::list* nodeList; //secondary cleanup of PNodes; nodeList = ClassList::getList(CL_PARENT_NODE); if (nodeList != NULL) while (!nodeList->empty()) { // ClassList::debug( 3, CL_PARENT_NODE); // PNode::getNullParent()->debugNode(0); // printf("%s::%s\n", nodeList->front()->getClassName(), nodeList->front()->getName()); delete nodeList->front(); } /* remove the player object */ if( this->localPlayer) delete this->localPlayer; State::setPlayer(NULL); this->localPlayer = NULL; this->localCamera = NULL; State::setCamera(NULL, NULL); this->sky = NULL; this->terrain = NULL; nodeList = ClassList::getList(CL_GRAPHICS_EFFECT); if (nodeList != NULL) while (!nodeList->empty()) delete nodeList->front(); nodeList = ClassList::getList(CL_ELEMENT_2D); if (nodeList != NULL) while (!nodeList->empty()) delete nodeList->front(); // At this Point all the WorldEntites should be unloaded. this->tickLists.clear(); this->drawLists.clear(); // unload the resources loaded in this Level !! ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); if (State::getObjectManager() == this->objectManager) { State::setObjectManager(NULL); delete this->objectManager; } this->objectManager = NULL; if(State::getSkyBox()) State::setSkyBox(NULL); this->glmis = NULL; } /** * @brief loads the scene data * @param root reference to the xml root element */ ErrorMessage GameWorldData::loadScene(const TiXmlElement* root) { LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams); LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams); LoadParam(root, "Music", this, GameWorldData, setSoundTrack); LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule); //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams); //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams); this->localCamera->setClipRegion(1, 10000.0); if( this->sky != NULL) this->localCamera->addChild(this->sky); OrxSound::SoundEngine::getInstance()->setListener(this->localCamera); } /** * unloads the scene data */ ErrorMessage GameWorldData::unloadScene() { /* delete some garphics and scene eingines */ delete LightManager::getInstance(); if (this->music != NULL) this->setSoundTrack(""); /* unload the shaders */ Shader::suspendShader(); } void GameWorldData::setSoundTrack(const std::string& name) { if (this->music != NULL) delete this->music; this->music = NULL; if (!name.empty()) { PRINTF(3)("Setting Sound Track to %s\n", name.c_str()); std::string oggFile = ResourceManager::getFullName(name); this->music = new OrxSound::OggPlayer(oggFile); //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL); //assert(this->music->isA(CL_SOUND_OGG_PLAYER)); } } void GameWorldData::loadGameRule(const TiXmlElement* root) { const TiXmlElement* element = root->FirstChildElement(); while( element != NULL) { PRINTF(4)("============ GameRules =="); PRINTF(4)("creating %s\n", element->Value()); BaseObject* created = Factory::fabricate(element); if (created != NULL /*&& created->isA(CL_GAME_RULES)*/) { this->gameRule = dynamic_cast(created); State::setGameRules(this->gameRule); } else { PRINTF(1)("Could not create a %s\n", element->Value()); delete created; } element = element->NextSiblingElement(); } }