Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/game_world_data.cc @ 8317

Last change on this file since 8317 was 7810, checked in by bensch, 18 years ago

orxonox/trunk: merged the Weather effects back here

File size: 10.0 KB
RevLine 
[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"
[7287]35#include "md2Model.h"
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
63using namespace std;
64
65
66/**
67 * constructor of the GameWorldData
68 */
69GameWorldData::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 */
89GameWorldData::~GameWorldData()
[6404]90{}
[6402]91
92
93
94/**
95 *  initialize the GameWorldData
96 */
97ErrorMessage 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
109  GraphicsEngine::getInstance()->displayFPS(true);
110}
111
112
113/**
114 *  loads the data from the xml file
115 * @param root reference to the xml root element
116 */
[7370]117ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
[6402]118{
119  // load the parameters
120  // name
[7221]121  std::string string = grabParameter( root, "name");
122  if( string.empty() )
[6402]123  {
124    PRINTF(2)("GameWorld is missing a proper 'name'\n");
125    this->setName("Unknown");
126  }
127  else
[7221]128    this->setName(string.c_str());
[6402]129
130  this->loadGUI(root);
131  this->loadWorldEntities(root);
132  this->loadScene(root);
133}
134
135
136/**
137 *  unloads the data from the xml file
138 */
139ErrorMessage GameWorldData::unloadData()
140{
141  this->unloadGUI();
142  this->unloadWorldEntities();
143  this->unloadScene();
144}
145
146
147/**
[7370]148 * @brief loads the GUI data
[6402]149 * @param root reference to the xml root element
150 */
[7370]151ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
[6402]152{
[7370]153  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
[6402]154  if( element == NULL)
155  {
156    PRINTF(2)("no LoadScreen specified, loading default\n");
157
158    glmis->setBackgroundImage("pictures/load_screen.jpg");
159    this->glmis->setMaximum(8);
[6988]160    //     this->glmis->draw();
[6402]161  }
162  else
163  {
164    this->glmis->loadParams(element);
[6988]165    //     this->glmis->draw();
[6402]166  }
167  this->glmis->draw();
168}
169
170
171/**
[7370]172 * @brief unloads the GUI data
[6402]173 */
174ErrorMessage GameWorldData::unloadGUI()
175{
176  delete this->glmis;
177}
178
179
180/**
[7370]181 * @brief loads the world entities from the xml file
[6402]182 * @param root reference to the xml root parameter
183 */
[7370]184ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
[6402]185{
[7370]186  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
[6402]187
188  if( element == NULL)
189  {
190    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
191  }
192  else
193  {
194    element = element->FirstChildElement();
195    // load Players/Objects/Whatever
196    PRINTF(4)("Loading WorldEntities\n");
197    while( element != NULL)
198    {
199      BaseObject* created = Factory::fabricate(element);
200      if( created != NULL )
[6834]201        PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName());
[6402]202
203      //todo do this more elegant
[7370]204      if( element->Value() == "SkyBox" && created->isA(CL_SKYBOX))
[6771]205      {
[6402]206        this->sky = dynamic_cast<WorldEntity*>(created);
[6771]207        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
208      }
[7370]209      if( element->Value() == "Terrain" && created->isA(CL_TERRAIN))
[6402]210      {
211        this->terrain = dynamic_cast<Terrain*>(created);
212        CDEngine::getInstance()->setTerrain(terrain);
213      }
214      element = element->NextSiblingElement();
215      this->glmis->step(); //! @todo temporary
216    }
217    PRINTF(4)("Done loading WorldEntities\n");
218  }
219
220  // Create a Player
221  this->localPlayer = new Player();
[7076]222  State::setPlayer(this->localPlayer);
[6402]223
224  Playable* playable;
225  const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
[7370]226  if (playableList != NULL && !playableList->empty())
[6402]227  {
[7370]228    /// TODO Make this also loadable
[6402]229    playable = dynamic_cast<Playable*>(playableList->front());
[6985]230    this->localPlayer->setPlayable(playable);
[6402]231  }
232
[7370]233  // Fill the EntityLists. Tick then Draw:
234  this->tickLists.push_back(OM_DEAD_TICK);
235  this->tickLists.push_back(OM_ENVIRON);
236  this->tickLists.push_back(OM_COMMON);
237  this->tickLists.push_back(OM_GROUP_00);
238  this->tickLists.push_back(OM_GROUP_00_PROJ);
239  this->tickLists.push_back(OM_GROUP_01);
240  this->tickLists.push_back(OM_GROUP_01_PROJ);
241
242  this->drawLists.push_back(OM_ENVIRON_NOTICK);
243  this->drawLists.push_back(OM_ENVIRON);
244  this->drawLists.push_back(OM_COMMON);
245  this->drawLists.push_back(OM_GROUP_00);
246  this->drawLists.push_back(OM_GROUP_00_PROJ);
247  this->drawLists.push_back(OM_GROUP_01);
248  this->drawLists.push_back(OM_GROUP_01_PROJ);
249
[6402]250  /* init the pnode tree */
251  PNode::getNullParent()->init();
252}
253
254
255/**
[6404]256 *  unloads the world entities
[6402]257 */
258ErrorMessage GameWorldData::unloadWorldEntities()
259{
260  FastFactory::flushAll(true);
[7029]261  GraphicsEngine::getInstance()->displayFPS(false);
[6402]262  // erease everything that is left.
[6626]263  // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner)
[6981]264  const std::list<BaseObject*>* nodeList;
[6402]265  //secondary cleanup of PNodes;
[6981]266  nodeList = ClassList::getList(CL_PARENT_NODE);
[6402]267  if (nodeList != NULL)
268    while (!nodeList->empty())
[7370]269    {
270      //    ClassList::debug( 3, CL_PARENT_NODE);
271      //    PNode::getNullParent()->debugNode(0);
272      //    printf("%s::%s\n", nodeList->front()->getClassName(), nodeList->front()->getName());
273      delete nodeList->front();
274    }
[6402]275  /* remove the player object */
276  if( this->localPlayer)
277    delete this->localPlayer;
[7126]278  State::setPlayer(NULL);
[7311]279  this->localPlayer = NULL;
280  this->localCamera = NULL;
281  State::setCamera(NULL, NULL);
282  this->sky = NULL;
283  this->terrain = NULL;
[6402]284
[6988]285  nodeList = ClassList::getList(CL_GRAPHICS_EFFECT);
286  if (nodeList != NULL)
287    while (!nodeList->empty())
288      delete nodeList->front();
[6981]289
290
[7029]291  nodeList = ClassList::getList(CL_ELEMENT_2D);
[7370]292  if (nodeList != NULL)
293    while (!nodeList->empty())
294      delete nodeList->front();
[6981]295
[7370]296  // At this Point all the WorldEntites should be unloaded.
297  this->tickLists.clear();
298  this->drawLists.clear();
[6981]299
[7370]300  // unload the resources loaded in this Level !!
[6402]301  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
302
303  if (State::getObjectManager() == this->objectManager)
304  {
305    State::setObjectManager(NULL);
306    delete this->objectManager;
307  }
[7311]308  this->objectManager = NULL;
[6863]309
[6771]310  if(State::getSkyBox())
311    State::setSkyBox(NULL);
[6863]312
[7311]313  this->glmis = NULL;
[6402]314}
315
316
317/**
[7370]318 * @brief loads the scene data
[6402]319 * @param root reference to the xml root element
320 */
[7370]321ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
[6402]322{
323  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
[6815]324  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
[7810]325  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
[6402]326
[6827]327  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
328
[7020]329  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
330
331
332  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
[6402]333  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
334
335  this->localCamera->setClipRegion(1, 10000.0);
336  if( this->sky != NULL)
337    this->localCamera->addChild(this->sky);
[7460]338  OrxSound::SoundEngine::getInstance()->setListener(this->localCamera);
[6402]339}
340
341
[7020]342
[6402]343/**
344 *  unloads the scene data
345 */
346ErrorMessage GameWorldData::unloadScene()
347{
348  /* delete some garphics and scene eingines */
349  delete LightManager::getInstance();
[7810]350  delete AtmosphericEngine::getInstance();
[6402]351
[7287]352  if (this->music != NULL)
353    this->setSoundTrack("");
[6402]354
355  /* unload the shaders */
356  Shader::suspendShader();
[7488]357
358  State::setGameRules(NULL);
[6402]359}
360
[6634]361
[7221]362void GameWorldData::setSoundTrack(const std::string& name)
[6634]363{
[6827]364  if (this->music != NULL)
[7287]365    delete this->music;
366  this->music = NULL;
[6827]367
[7221]368  if (!name.empty())
[6987]369  {
[7221]370    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
371    std::string oggFile = ResourceManager::getFullName(name);
[7460]372    this->music = new OrxSound::OggPlayer(oggFile);
[6988]373
374    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
375    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
[6987]376  }
[6634]377}
378
379
[7020]380void GameWorldData::loadGameRule(const TiXmlElement* root)
381{
[7036]382  const TiXmlElement* element = root->FirstChildElement();
383  while( element != NULL)
[7020]384  {
[7461]385    PRINTF(2)("============ GameRules ==\n");
386    PRINTF(2)("creating %s\n", element->Value());
[7036]387    BaseObject* created = Factory::fabricate(element);
[7466]388    if (created != NULL && created->isA(CL_GAME_RULES))
[7020]389    {
[7036]390      this->gameRule = dynamic_cast<GameRules*>(created);
[7039]391      State::setGameRules(this->gameRule);
[7482]392      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
[7462]393      return;
[7020]394    }
[7036]395    else
396    {
397      PRINTF(1)("Could not create a %s\n", element->Value());
398      delete created;
399    }
400    element = element->NextSiblingElement();
[7020]401  }
402}
403
404
405
Note: See TracBrowser for help on using the repository browser.