Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10714 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

File size: 11.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 "tools/camera.h"
32#include "tools/cameraman.h"
33#include "environments/terrain.h"
34#include "environments/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 "loading/fast_factory.h"
44#include "util/loading/load_param_xml.h"
45
46#include "graphics_engine.h"
47#include "graphics_effect.h"
48#include "weather_effects/atmospheric_engine.h"
49#include "event_handler.h"
50#include "sound_engine.h"
51#include "cd_engine.h"
52#include "network_manager.h"
53#include "physics_engine.h"
54#include "ai_engine.h"
55
56#include "glmenu_imagescreen.h"
57
58#include "game_rules.h"
59
60#include "ogg_player.h"
61#include "shader.h"
62
63
64ObjectListDefinition(GameWorldData);
65/**
66 * constructor of the GameWorldData
67 */
68GameWorldData::GameWorldData()
69{
70  this->registerObject(this, GameWorldData::_objectList);
71
72  this->glmis = NULL;
73
74
75  this->localPlayer = NULL;
76  this->sky = NULL;
77  this->terrain = NULL;
78
79  this->music = NULL;
80  this->objectManager = NULL;
81  this->gameRule = NULL;
82}
83
84
85/**
86 * destructor for the GameWorldData
87 */
88GameWorldData::~GameWorldData()
89{}
90
91
92
93/**
94 *  initialize the GameWorldData
95 */
96ErrorMessage GameWorldData::init()
97{
98  this->objectManager = new ObjectManager();
99  State::setObjectManager(this->objectManager);
100
101  PNode::getNullParent();
102  Camera* localCamera = new Camera();
103    localCamera->setName ("GameWorldCamera");
104  State::setCamera(localCamera, localCamera->getTarget());
105  //CameraMan* camMan = new CameraMan();
106  //State::setCameraman(camMan);
107  LightManager::getInstance();
108  AIEngine::getInstance();
109
110//  GraphicsEngine::getInstance()->displayFPS(true);
111
112  return ErrorMessage();
113}
114
115
116/**
117 *  loads the data from the xml file
118 * @param root reference to the xml root element
119 */
120ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
121{
122  assert (root != NULL);
123  // load the parameters
124  // name
125  std::string string = LoadParamBase::grabParameter( root, "name");
126  if( string.empty() )
127  {
128    PRINTF(2)("GameWorld is missing a proper 'name'\n");
129    this->setName("Unknown");
130  }
131  else
132    this->setName(string.c_str());
133
134
135  this->loadGUI(root);
136  this->loadWorldEntities(root);
137  this->loadCameras(root);
138  this->loadScene(root);
139
140  return ErrorMessage();
141}
142
143
144/**
145 *  unloads the data from the xml file
146 */
147ErrorMessage GameWorldData::unloadData()
148{
149  this->unloadGUI();
150  this->unloadWorldEntities();
151  this->unloadScene();
152  this->unloadCameras();
153
154  // killl ai engine
155  delete AIEngine::getInstance();
156
157  return ErrorMessage();
158}
159
160/**
161 * @brief creates the cameras
162 * @param root reference to the xml root element
163 */
164ErrorMessage GameWorldData::loadCameras(const TiXmlElement* root)
165{
166  const TiXmlElement* element = root->FirstChildElement("CameraMan");
167  if( element != NULL)
168  {
169    CameraMan* camMan = new CameraMan(element);
170    State::setCameraman(camMan);
171  }
172  else
173  {
174    CameraMan* camMan = new CameraMan();
175    State::setCameraman(camMan);
176  }
177  return ErrorMessage();
178}
179
180
181/**
182 * @brief unloads the camera
183 */
184ErrorMessage GameWorldData::unloadCameras()
185{
186  CameraMan* camMan = State::getCameraman() ;
187  if(camMan != NULL){
188    delete  camMan;
189    State::setCameraman(NULL);
190   }
191  return ErrorMessage();
192}
193
194
195/**
196 * @brief loads the GUI data
197 * @param root reference to the xml root element
198 */
199ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
200{
201  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
202  if( element == NULL)
203  {
204    PRINTF(2)("no LoadScreen specified, loading default\n");
205
206    glmis->setBackgroundImage("textures/load_screens/default.jpg");
207    glmis->setPosScale(0.0,0.0,1.0,1.0);
208    glmis->setBarImage("textures/load_screens/default_bar.png");
209    glmis->setBarPosScale(0.65,0.87,0.3,0.05);
210
211    this->glmis->setMaximum(8);
212    //     this->glmis->draw();
213  }
214  else
215  {
216    this->glmis->loadParams(element);
217    //     this->glmis->draw();
218  }
219  this->glmis->draw();
220
221  return ErrorMessage();
222}
223
224
225/**
226 * @brief unloads the GUI data
227 */
228ErrorMessage GameWorldData::unloadGUI()
229{
230  delete this->glmis;
231
232  return ErrorMessage();
233}
234
235
236/**
237 * @brief loads the world entities from the xml file
238 * @param root reference to the xml root parameter
239 */
240ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
241{
242  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
243  bool mouseCaptured = EventHandler::getInstance()->grabbedEvents();
244  EventHandler::getInstance()->grabEvents(false);
245
246  if( element == NULL)
247  {
248    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
249  }
250  else
251  {
252    element = element->FirstChildElement();
253    // load Players/Objects/Whatever
254    PRINTF(4)("Loading WorldEntities\n");
255    while( element != NULL)
256    {
257      BaseObject* created = Factory::fabricate(element);
258      if( created != NULL )
259        PRINTF(4)("Created a %s: %s\n", created->getClassCName(), created->getCName());
260
261      //todo do this more elegant
262      if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID()))
263      {
264        this->sky = dynamic_cast<WorldEntity*>(created);
265        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
266      }
267      if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID()))
268      {
269        this->terrain = dynamic_cast<Terrain*>(created);
270        CDEngine::getInstance()->setTerrain(terrain);
271      }
272      element = element->NextSiblingElement();
273      this->glmis->step(); //! @todo temporary
274    }
275    PRINTF(4)("Done loading WorldEntities\n");
276  }
277
278  // Create a Player
279  this->localPlayer = new Player();
280  State::setPlayer(this->localPlayer);
281
282  if (!Playable::objectList().empty())
283  {
284    /// TODO Make this also loadable
285    this->localPlayer->setPlayable(Playable::objectList().front());
286  }
287
288  // Fill the EntityLists. Tick then Draw:
289  this->tickLists.push_back(OM_DEAD_TICK);
290  this->tickLists.push_back(OM_ENVIRON);
291  this->tickLists.push_back(OM_COMMON);
292  this->tickLists.push_back(OM_GROUP_00);
293  this->tickLists.push_back(OM_GROUP_00_PROJ);
294  this->tickLists.push_back(OM_GROUP_01);
295  this->tickLists.push_back(OM_GROUP_01_PROJ);
296  this->tickLists.push_back(OM_GROUP_02);
297
298  this->drawLists.push_back(OM_ENVIRON_NOTICK);
299  this->drawLists.push_back(OM_ENVIRON);
300  this->drawLists.push_back(OM_GROUP_00);
301  this->drawLists.push_back(OM_GROUP_00_PROJ);
302  this->drawLists.push_back(OM_GROUP_01);
303  this->drawLists.push_back(OM_GROUP_01_PROJ);
304  this->drawLists.push_back(OM_GROUP_02);
305  this->drawLists.push_back(OM_COMMON);
306
307  /* init the pnode tree */
308  PNode::getNullParent()->init();
309
310  EventHandler::getInstance()->grabEvents(mouseCaptured);
311
312  return ErrorMessage();
313}
314
315
316/**
317 *  unloads the world entities
318 */
319ErrorMessage GameWorldData::unloadWorldEntities()
320{
321  FastFactory::flushAll(true);
322  GraphicsEngine::getInstance()->displayFPS(false);
323  // erease everything that is left.
324  //secondary cleanup of PNodes;
325  while (!PNode::objectList().empty())
326    delete PNode::objectList().front();
327
328  /* remove the player object */
329  if( this->localPlayer)
330    delete this->localPlayer;
331  State::setPlayer(NULL);
332  this->localPlayer = NULL;
333  State::setCamera(NULL, NULL);
334  this->sky = NULL;
335  this->terrain = NULL;
336
337  while (!GraphicsEffect::objectList().empty())
338    delete GraphicsEffect::objectList().front();
339
340
341  while (!Element2D::objectList().empty())
342    delete Element2D::objectList().front();
343
344  // At this Point all the WorldEntites should be unloaded.
345  this->tickLists.clear();
346  this->drawLists.clear();
347
348  // unload the resources loaded in this Level !!
349  /// FIXME TODO HACK!!
350//  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
351
352  if (State::getObjectManager() == this->objectManager)
353  {
354    State::setObjectManager(NULL);
355    delete this->objectManager;
356  }
357  this->objectManager = NULL;
358
359  if(State::getSkyBox())
360    State::setSkyBox(NULL);
361
362  this->glmis = NULL;
363
364  return ErrorMessage();
365}
366
367
368/**
369 * @brief loads the scene data
370 * @param root reference to the xml root element
371 */
372ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
373{
374  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
375  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
376  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
377
378  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
379
380  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
381
382  LoadParam(root, "clip-region", State::getCameraman(), CameraMan, setClipRegion);
383
384
385  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
386  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
387
388
389  OrxSound::SoundEngine::getInstance()->setListener(State::getCamera());
390
391  return ErrorMessage();
392}
393
394
395
396/**
397 *  unloads the scene data
398 */
399ErrorMessage GameWorldData::unloadScene()
400{
401  /* delete some garphics and scene eingines */
402  delete LightManager::getInstance();
403  delete AtmosphericEngine::getInstance();
404
405  if (this->music != NULL)
406    this->setSoundTrack("");
407
408  /* unload the shaders */
409  Shader::suspendShader();
410
411  State::setGameRules(NULL);
412
413  return ErrorMessage();
414}
415
416
417void GameWorldData::setSoundTrack(const std::string& name)
418{
419  if (this->music != NULL)
420    delete this->music;
421  this->music = NULL;
422
423  if (!name.empty())
424  {
425    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
426    std::string oggFile = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(name);
427    this->music = new OrxSound::OggPlayer(oggFile);
428//     if (this->localPlayer != NULL)
429//       this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title());
430
431    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
432    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
433  }
434}
435
436
437void GameWorldData::loadGameRule(const TiXmlElement* root)
438{
439  const TiXmlElement* element = root->FirstChildElement();
440  while( element != NULL)
441  {
442    PRINTF(2)("============ GameRules ==\n");
443    PRINTF(2)("creating %s\n", element->Value());
444    BaseObject* created = Factory::fabricate(element);
445    if (created != NULL && created->isA(GameRules::staticClassID()))
446    {
447      this->gameRule = dynamic_cast<GameRules*>(created);
448      State::setGameRules(this->gameRule);
449      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
450      return;
451    }
452    else
453    {
454      PRINTF(1)("Could not create a %s\n", element->Value());
455      delete created;
456    }
457    element = element->NextSiblingElement();
458  }
459}
460
461
462
Note: See TracBrowser for help on using the repository browser.