Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/story_entities/game_world_data.cc @ 9715

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

renamed newclassid to classid and newobjectlist to objectlist

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