Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/story_entities/game_world_data.cc @ 9777

Last change on this file since 9777 was 9140, checked in by bensch, 18 years ago

merged back

File size: 10.4 KB
Line 
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
18#include <fstream>
19#include <iostream>
20
21#include "game_world_data.h"
22
23#include "util/loading/resource_manager.h"
24#include "state.h"
25#include "class_list.h"
26#include "substring.h"
27
28#include "util/loading/game_loader.h"
29
30#include "world_entity.h"
31#include "player.h"
32#include "camera.h"
33#include "terrain_entity.h"
34#include "skybox.h"
35#include "md2/md2Model.h"
36#include "world_entities/projectiles/projectile.h"
37#include "npcs/npc_test1.h"
38#include "playable.h"
39
40#include "light.h"
41
42#include "util/loading/factory.h"
43#include "fast_factory.h"
44#include "util/loading/load_param.h"
45
46#include "graphics_engine.h"
47#include "effects/atmospheric_engine.h"
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
57#include "game_rules.h"
58
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;
82  this->gameRule = NULL;
83}
84
85
86/**
87 * destructor for the GameWorldData
88 */
89GameWorldData::~GameWorldData()
90{}
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  return ErrorMessage();
112}
113
114
115/**
116 *  loads the data from the xml file
117 * @param root reference to the xml root element
118 */
119ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
120{
121  // load the parameters
122  // name
123  std::string string = grabParameter( root, "name");
124  if( string.empty() )
125  {
126    PRINTF(2)("GameWorld is missing a proper 'name'\n");
127    this->setName("Unknown");
128  }
129  else
130    this->setName(string.c_str());
131
132  this->loadGUI(root);
133  this->loadWorldEntities(root);
134  this->loadScene(root);
135
136  return ErrorMessage();
137}
138
139
140/**
141 *  unloads the data from the xml file
142 */
143ErrorMessage GameWorldData::unloadData()
144{
145  this->unloadGUI();
146  this->unloadWorldEntities();
147  this->unloadScene();
148
149  return ErrorMessage();
150}
151
152
153/**
154 * @brief loads the GUI data
155 * @param root reference to the xml root element
156 */
157ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
158{
159  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
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);
166    //     this->glmis->draw();
167  }
168  else
169  {
170    this->glmis->loadParams(element);
171    //     this->glmis->draw();
172  }
173  this->glmis->draw();
174
175  return ErrorMessage();
176}
177
178
179/**
180 * @brief unloads the GUI data
181 */
182ErrorMessage GameWorldData::unloadGUI()
183{
184  delete this->glmis;
185
186  return ErrorMessage();
187}
188
189
190/**
191 * @brief loads the world entities from the xml file
192 * @param root reference to the xml root parameter
193 */
194ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
195{
196  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
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 )
211        PRINTF(2)("Created a %s: %s\n", created->getClassName(), created->getName());
212
213      //todo do this more elegant
214      if( element->Value() == "SkyBox" && created->isA(CL_SKYBOX))
215      {
216        this->sky = dynamic_cast<WorldEntity*>(created);
217        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
218      }
219               
220      if( element->Value() == "TerrainEntity" && created->isA(CL_TERRAIN))
221      {
222        this->terrain = dynamic_cast<TerrainEntity*>(created);
223        CDEngine::getInstance()->setTerrain(terrain);
224      }
225      element = element->NextSiblingElement();
226      this->glmis->step(); //! @todo temporary
227    }
228    PRINTF(4)("Done loading WorldEntities\n");
229  }
230
231  // Create a Player
232  this->localPlayer = new Player();
233  State::setPlayer(this->localPlayer);
234
235  Playable* playable;
236  const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
237  if (playableList != NULL && !playableList->empty())
238  {
239    /// TODO Make this also loadable
240    playable = dynamic_cast<Playable*>(playableList->front());
241    this->localPlayer->setPlayable(playable);
242  }
243
244  // Fill the EntityLists. Tick then Draw:
245  this->tickLists.push_back(OM_DEAD_TICK);
246  this->tickLists.push_back(OM_ENVIRON);
247  this->tickLists.push_back(OM_COMMON);
248  this->tickLists.push_back(OM_GROUP_00);
249  this->tickLists.push_back(OM_GROUP_00_PROJ);
250  this->tickLists.push_back(OM_GROUP_01);
251  this->tickLists.push_back(OM_GROUP_01_PROJ);
252
253  this->drawLists.push_back(OM_ENVIRON_NOTICK);
254  this->drawLists.push_back(OM_ENVIRON);
255  this->drawLists.push_back(OM_COMMON);
256  this->drawLists.push_back(OM_GROUP_00);
257  this->drawLists.push_back(OM_GROUP_00_PROJ);
258  this->drawLists.push_back(OM_GROUP_01);
259  this->drawLists.push_back(OM_GROUP_01_PROJ);
260
261  /* init the pnode tree */
262  PNode::getNullParent()->init();
263
264  return ErrorMessage();
265}
266
267
268/**
269 *  unloads the world entities
270 */
271ErrorMessage GameWorldData::unloadWorldEntities()
272{
273  FastFactory::flushAll(true);
274  GraphicsEngine::getInstance()->displayFPS(false);
275  // erease everything that is left.
276  // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner)
277  const std::list<BaseObject*>* nodeList;
278  //secondary cleanup of PNodes;
279  nodeList = ClassList::getList(CL_PARENT_NODE);
280  if (nodeList != NULL)
281    while (!nodeList->empty())
282    {
283      //    ClassList::debug( 3, CL_PARENT_NODE);
284      //    PNode::getNullParent()->debugNode(0);
285      //    printf("%s::%s\n", nodeList->front()->getClassName(), nodeList->front()->getName());
286      delete nodeList->front();
287    }
288  /* remove the player object */
289  if( this->localPlayer)
290    delete this->localPlayer;
291  State::setPlayer(NULL);
292  this->localPlayer = NULL;
293  this->localCamera = NULL;
294  State::setCamera(NULL, NULL);
295  this->sky = NULL;
296  this->terrain = NULL;
297
298  nodeList = ClassList::getList(CL_GRAPHICS_EFFECT);
299  if (nodeList != NULL)
300    while (!nodeList->empty())
301      delete nodeList->front();
302
303
304  nodeList = ClassList::getList(CL_ELEMENT_2D);
305  if (nodeList != NULL)
306    while (!nodeList->empty())
307      delete nodeList->front();
308
309  // At this Point all the WorldEntites should be unloaded.
310  this->tickLists.clear();
311  this->drawLists.clear();
312
313  // unload the resources loaded in this Level !!
314  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
315
316  if (State::getObjectManager() == this->objectManager)
317  {
318    State::setObjectManager(NULL);
319    delete this->objectManager;
320  }
321  this->objectManager = NULL;
322
323  if(State::getSkyBox())
324    State::setSkyBox(NULL);
325
326  this->glmis = NULL;
327
328  return ErrorMessage();
329}
330
331
332/**
333 * @brief loads the scene data
334 * @param root reference to the xml root element
335 */
336ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
337{
338  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
339  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
340  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
341
342  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
343
344  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
345
346
347  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
348  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
349
350  this->localCamera->setClipRegion(1, 10000.0);
351  if( this->sky != NULL)
352    this->localCamera->addChild(this->sky);
353  OrxSound::SoundEngine::getInstance()->setListener(this->localCamera);
354
355  return ErrorMessage();
356}
357
358
359
360/**
361 *  unloads the scene data
362 */
363ErrorMessage GameWorldData::unloadScene()
364{
365  /* delete some garphics and scene eingines */
366  delete LightManager::getInstance();
367  delete AtmosphericEngine::getInstance();
368
369  if (this->music != NULL)
370    this->setSoundTrack("");
371
372  /* unload the shaders */
373  Shader::suspendShader();
374
375  State::setGameRules(NULL);
376
377  return ErrorMessage();
378}
379
380
381void GameWorldData::setSoundTrack(const std::string& name)
382{
383  if (this->music != NULL)
384    delete this->music;
385  this->music = NULL;
386
387  if (!name.empty())
388  {
389    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
390    std::string oggFile = ResourceManager::getFullName(name);
391    this->music = new OrxSound::OggPlayer(oggFile);
392    if (this->localPlayer != NULL)
393      this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title());
394
395    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
396    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
397  }
398}
399
400
401void GameWorldData::loadGameRule(const TiXmlElement* root)
402{
403  const TiXmlElement* element = root->FirstChildElement();
404  while( element != NULL)
405  {
406    PRINTF(2)("============ GameRules ==\n");
407    PRINTF(2)("creating %s\n", element->Value());
408    BaseObject* created = Factory::fabricate(element);
409    if (created != NULL && created->isA(CL_GAME_RULES))
410    {
411      this->gameRule = dynamic_cast<GameRules*>(created);
412      State::setGameRules(this->gameRule);
413      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
414      return;
415    }
416    else
417    {
418      PRINTF(1)("Could not create a %s\n", element->Value());
419      delete created;
420    }
421    element = element->NextSiblingElement();
422  }
423}
424
425
426
Note: See TracBrowser for help on using the repository browser.