Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/heathaze/src/story_entities/game_world_data.cc

Last change on this file was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 10.2 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 "state.h"
24#include "substring.h"
25
26#include "util/loading/game_loader.h"
27#include "util/loading/resource_manager.h"
28
29#include "world_entity.h"
30#include "player.h"
31#include "camera.h"
32#include "terrain.h"
33#include "skybox.h"
34#include "md2/md2Model.h"
35#include "world_entities/projectiles/projectile.h"
36#include "npcs/npc_test1.h"
37#include "playable.h"
38
39#include "light.h"
40
41#include "util/loading/factory.h"
42#include "loading/fast_factory.h"
43#include "util/loading/load_param_xml.h"
44
45#include "graphics_engine.h"
46#include "graphics_effect.h"
47#include "weather_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
54#include "glmenu_imagescreen.h"
55
56#include "game_rules.h"
57
58#include "ogg_player.h"
59#include "shader.h"
60
61
62ObjectListDefinition(GameWorldData);
63/**
64 * constructor of the GameWorldData
65 */
66GameWorldData::GameWorldData()
67{
68  this->registerObject(this, GameWorldData::_objectList);
69
70  this->glmis = NULL;
71
72  this->localCamera = NULL;
73  this->localPlayer = NULL;
74  this->sky = NULL;
75  this->terrain = NULL;
76
77  this->music = NULL;
78  this->objectManager = NULL;
79  this->gameRule = NULL;
80}
81
82
83/**
84 * destructor for the GameWorldData
85 */
86GameWorldData::~GameWorldData()
87{}
88
89
90
91/**
92 *  initialize the GameWorldData
93 */
94ErrorMessage GameWorldData::init()
95{
96  this->objectManager = new ObjectManager();
97  State::setObjectManager(this->objectManager);
98
99  PNode::getNullParent();
100  this->localCamera = new Camera();
101  this->localCamera->setName ("GameWorld-Camera");
102  State::setCamera(this->localCamera, this->localCamera->getTarget());
103
104  LightManager::getInstance();
105
106//  GraphicsEngine::getInstance()->displayFPS(true);
107
108  return ErrorMessage();
109}
110
111
112/**
113 *  loads the data from the xml file
114 * @param root reference to the xml root element
115 */
116ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
117{
118  assert (root != NULL);
119  // load the parameters
120  // name
121  std::string string = LoadParamBase::grabParameter( root, "name");
122  if( string.empty() )
123  {
124    PRINTF(2)("GameWorld is missing a proper 'name'\n");
125    this->setName("Unknown");
126  }
127  else
128    this->setName(string.c_str());
129
130  this->loadGUI(root);
131  this->loadWorldEntities(root);
132  this->loadScene(root);
133
134  return ErrorMessage();
135}
136
137
138/**
139 *  unloads the data from the xml file
140 */
141ErrorMessage GameWorldData::unloadData()
142{
143  this->unloadGUI();
144  this->unloadWorldEntities();
145  this->unloadScene();
146
147  return ErrorMessage();
148}
149
150
151/**
152 * @brief loads the GUI data
153 * @param root reference to the xml root element
154 */
155ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
156{
157  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
158  if( element == NULL)
159  {
160    PRINTF(2)("no LoadScreen specified, loading default\n");
161
162    glmis->setBackgroundImage("pictures/load_screen.jpg");
163    this->glmis->setMaximum(8);
164    //     this->glmis->draw();
165  }
166  else
167  {
168    this->glmis->loadParams(element);
169    //     this->glmis->draw();
170  }
171  this->glmis->draw();
172
173  return ErrorMessage();
174}
175
176
177/**
178 * @brief unloads the GUI data
179 */
180ErrorMessage GameWorldData::unloadGUI()
181{
182  delete this->glmis;
183
184  return ErrorMessage();
185}
186
187
188/**
189 * @brief loads the world entities from the xml file
190 * @param root reference to the xml root parameter
191 */
192ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
193{
194  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
195  bool mouseCaptured = EventHandler::getInstance()->grabbedEvents();
196  EventHandler::getInstance()->grabEvents(false);
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(4)("Created a %s: %s\n", created->getClassCName(), created->getCName());
212
213      //todo do this more elegant
214      if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID()))
215      {
216        this->sky = dynamic_cast<WorldEntity*>(created);
217        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
218      }
219      if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID()))
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();
232  State::setPlayer(this->localPlayer);
233
234  Playable* playable;
235  if (!Playable::objectList().empty())
236  {
237    /// TODO Make this also loadable
238    this->localPlayer->setPlayable(Playable::objectList().front());
239  }
240
241  // Fill the EntityLists. Tick then Draw:
242  this->tickLists.push_back(OM_DEAD_TICK);
243  this->tickLists.push_back(OM_ENVIRON);
244  this->tickLists.push_back(OM_COMMON);
245  this->tickLists.push_back(OM_GROUP_00);
246  this->tickLists.push_back(OM_GROUP_00_PROJ);
247  this->tickLists.push_back(OM_GROUP_01);
248  this->tickLists.push_back(OM_GROUP_01_PROJ);
249  this->tickLists.push_back(OM_GROUP_02);
250
251  this->drawLists.push_back(OM_ENVIRON_NOTICK);
252  this->drawLists.push_back(OM_ENVIRON);
253  this->drawLists.push_back(OM_GROUP_00);
254  this->drawLists.push_back(OM_GROUP_00_PROJ);
255  this->drawLists.push_back(OM_GROUP_01);
256  this->drawLists.push_back(OM_GROUP_01_PROJ);
257  this->drawLists.push_back(OM_GROUP_02);
258  this->drawLists.push_back(OM_COMMON);
259
260  /* init the pnode tree */
261  PNode::getNullParent()->init();
262
263  EventHandler::getInstance()->grabEvents(mouseCaptured);
264
265  return ErrorMessage();
266}
267
268
269/**
270 *  unloads the world entities
271 */
272ErrorMessage GameWorldData::unloadWorldEntities()
273{
274  FastFactory::flushAll(true);
275  GraphicsEngine::getInstance()->displayFPS(false);
276  // erease everything that is left.
277  //secondary cleanup of PNodes;
278  while (!PNode::objectList().empty())
279    delete PNode::objectList().front();
280
281  /* remove the player object */
282  if( this->localPlayer)
283    delete this->localPlayer;
284  State::setPlayer(NULL);
285  this->localPlayer = NULL;
286  this->localCamera = NULL;
287  State::setCamera(NULL, NULL);
288  this->sky = NULL;
289  this->terrain = NULL;
290
291  while (!GraphicsEffect::objectList().empty())
292    delete GraphicsEffect::objectList().front();
293
294
295  while (!Element2D::objectList().empty())
296    delete Element2D::objectList().front();
297
298  // At this Point all the WorldEntites should be unloaded.
299  this->tickLists.clear();
300  this->drawLists.clear();
301
302  // unload the resources loaded in this Level !!
303  /// FIXME TODO HACK!!
304//  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
305
306  if (State::getObjectManager() == this->objectManager)
307  {
308    State::setObjectManager(NULL);
309    delete this->objectManager;
310  }
311  this->objectManager = NULL;
312
313  if(State::getSkyBox())
314    State::setSkyBox(NULL);
315
316  this->glmis = NULL;
317
318  return ErrorMessage();
319}
320
321
322/**
323 * @brief loads the scene data
324 * @param root reference to the xml root element
325 */
326ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
327{
328  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
329  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
330  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
331
332  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
333
334  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
335
336  LoadParam(root, "clip-region", this->localCamera, Camera, setClipRegion);
337
338
339  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
340  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
341
342  if( this->sky != NULL)
343    this->localCamera->addChild(this->sky);
344  OrxSound::SoundEngine::getInstance()->setListener(this->localCamera);
345
346  return ErrorMessage();
347}
348
349
350
351/**
352 *  unloads the scene data
353 */
354ErrorMessage GameWorldData::unloadScene()
355{
356  /* delete some garphics and scene eingines */
357  delete LightManager::getInstance();
358  delete AtmosphericEngine::getInstance();
359
360  if (this->music != NULL)
361    this->setSoundTrack("");
362
363  /* unload the shaders */
364  Shader::suspendShader();
365
366  State::setGameRules(NULL);
367
368  return ErrorMessage();
369}
370
371
372void GameWorldData::setSoundTrack(const std::string& name)
373{
374  if (this->music != NULL)
375    delete this->music;
376  this->music = NULL;
377
378  if (!name.empty())
379  {
380    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
381    std::string oggFile = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(name);
382    this->music = new OrxSound::OggPlayer(oggFile);
383    if (this->localPlayer != NULL)
384      this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title());
385
386    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
387    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
388  }
389}
390
391
392void GameWorldData::loadGameRule(const TiXmlElement* root)
393{
394  const TiXmlElement* element = root->FirstChildElement();
395  while( element != NULL)
396  {
397    PRINTF(2)("============ GameRules ==\n");
398    PRINTF(2)("creating %s\n", element->Value());
399    BaseObject* created = Factory::fabricate(element);
400    if (created != NULL && created->isA(GameRules::staticClassID()))
401    {
402      this->gameRule = dynamic_cast<GameRules*>(created);
403      State::setGameRules(this->gameRule);
404      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
405      return;
406    }
407    else
408    {
409      PRINTF(1)("Could not create a %s\n", element->Value());
410      delete created;
411    }
412    element = element->NextSiblingElement();
413  }
414}
415
416
417
Note: See TracBrowser for help on using the repository browser.