Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7035 was 7035, checked in by patrick, 18 years ago

trunk: game rules loading problems

File size: 8.8 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 "game_world_data.h"
19
20#include "resource_manager.h"
21#include "state.h"
22#include "class_list.h"
23#include "substring.h"
24
25#include "game_loader.h"
26
27#include "p_node.h"
28#include "world_entity.h"
29#include "player.h"
30#include "camera.h"
31#include "environment.h"
32#include "terrain.h"
33#include "test_entity.h"
34#include "terrain.h"
35#include "skybox.h"
36#include "md2Model.h"
37#include "world_entities/projectiles/projectile.h"
38#include "npcs/npc_test1.h"
39#include "playable.h"
40
41#include "light.h"
42
43#include "factory.h"
44#include "fast_factory.h"
45#include "load_param.h"
46
47#include "graphics_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 "animation_player.h"
58#include "animation3d.h"
59
60#include "game_rules.h"
61
62#include "ogg_player.h"
63#include "shader.h"
64
65
66using namespace std;
67
68
69/**
70 * constructor of the GameWorldData
71 */
72GameWorldData::GameWorldData()
73{
74  this->setClassID(CL_GAME_WORLD_DATA, "GameWorldData");
75
76  this->glmis = NULL;
77
78  this->localCamera = NULL;
79  this->localPlayer = NULL;
80  this->sky = NULL;
81  this->terrain = NULL;
82
83  this->music = NULL;
84  this->objectManager = NULL;
85  this->gameRule = NULL;
86}
87
88
89/**
90 * destructor for the GameWorldData
91 */
92GameWorldData::~GameWorldData()
93{}
94
95
96
97/**
98 *  initialize the GameWorldData
99 */
100ErrorMessage GameWorldData::init()
101{
102  this->objectManager = new ObjectManager();
103  State::setObjectManager(this->objectManager);
104
105  PNode::getNullParent();
106  this->localCamera = new Camera();
107  this->localCamera->setName ("GameWorld-Camera");
108  State::setCamera(this->localCamera, this->localCamera->getTarget());
109
110  LightManager::getInstance();
111
112  GraphicsEngine::getInstance()->displayFPS(true);
113
114  /* initialize some graphics engines and graphical elements */
115  AnimationPlayer::getInstance();
116  PhysicsEngine::getInstance();
117}
118
119
120/**
121 *  loads the data from the xml file
122 * @param root reference to the xml root element
123 */
124ErrorMessage GameWorldData::loadData(TiXmlElement* root)
125{
126  // load the parameters
127  // name
128  const char* string = grabParameter( root, "name");
129  if( string == NULL)
130  {
131    PRINTF(2)("GameWorld is missing a proper 'name'\n");
132    this->setName("Unknown");
133  }
134  else
135    this->setName(string);
136
137  this->loadGUI(root);
138  this->loadWorldEntities(root);
139  this->loadScene(root);
140}
141
142
143/**
144 *  unloads the data from the xml file
145 */
146ErrorMessage GameWorldData::unloadData()
147{
148  this->unloadGUI();
149  this->unloadWorldEntities();
150  this->unloadScene();
151}
152
153
154/**
155 *  loads the GUI data
156 * @param root reference to the xml root element
157 */
158ErrorMessage GameWorldData::loadGUI(TiXmlElement* root)
159{
160  TiXmlElement* element = root->FirstChildElement("LoadScreen");
161  if( element == NULL)
162  {
163    PRINTF(2)("no LoadScreen specified, loading default\n");
164
165    glmis->setBackgroundImage("pictures/load_screen.jpg");
166    this->glmis->setMaximum(8);
167    //     this->glmis->draw();
168  }
169  else
170  {
171    this->glmis->loadParams(element);
172    //     this->glmis->draw();
173  }
174  this->glmis->draw();
175}
176
177
178/**
179 *  unloads the GUI data
180 */
181ErrorMessage GameWorldData::unloadGUI()
182{
183  delete this->glmis;
184}
185
186
187/**
188 *  loads the world entities from the xml file
189 * @param root reference to the xml root parameter
190 */
191ErrorMessage GameWorldData::loadWorldEntities(TiXmlElement* root)
192{
193  TiXmlElement* element = root->FirstChildElement("WorldEntities");
194
195  if( element == NULL)
196  {
197    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
198  }
199  else
200  {
201    element = element->FirstChildElement();
202    // load Players/Objects/Whatever
203    PRINTF(4)("Loading WorldEntities\n");
204    while( element != NULL)
205    {
206      BaseObject* created = Factory::fabricate(element);
207      if( created != NULL )
208        PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName());
209
210      //todo do this more elegant
211      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
212      {
213        this->sky = dynamic_cast<WorldEntity*>(created);
214        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
215      }
216      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
217      {
218        this->terrain = dynamic_cast<Terrain*>(created);
219        CDEngine::getInstance()->setTerrain(terrain);
220      }
221      element = element->NextSiblingElement();
222      this->glmis->step(); //! @todo temporary
223    }
224    PRINTF(4)("Done loading WorldEntities\n");
225  }
226
227  // Create a Player
228  this->localPlayer = new Player();
229
230  Playable* playable;
231  const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
232  if (playableList != NULL)
233  {
234    playable = dynamic_cast<Playable*>(playableList->front());
235    this->localPlayer->setPlayable(playable);
236  }
237
238  /* init the pnode tree */
239  PNode::getNullParent()->init();
240}
241
242
243/**
244 *  unloads the world entities
245 */
246ErrorMessage GameWorldData::unloadWorldEntities()
247{
248  FastFactory::flushAll(true);
249  GraphicsEngine::getInstance()->displayFPS(false);
250
251  // erease everything that is left.
252  // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner)
253  const std::list<BaseObject*>* nodeList;
254  //secondary cleanup of PNodes;
255  nodeList = ClassList::getList(CL_PARENT_NODE);
256  if (nodeList != NULL)
257    while (!nodeList->empty())
258      delete nodeList->front();
259  /* remove the player object */
260  if( this->localPlayer)
261    delete this->localPlayer;
262
263  nodeList = ClassList::getList(CL_GRAPHICS_EFFECT);
264  if (nodeList != NULL)
265    while (!nodeList->empty())
266      delete nodeList->front();
267
268
269  nodeList = ClassList::getList(CL_ELEMENT_2D);
270    if (nodeList != NULL)
271       while (!nodeList->empty())
272         delete nodeList->front();
273
274
275
276
277  // unload the resources !!
278  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
279
280  if (State::getObjectManager() == this->objectManager)
281  {
282    State::setObjectManager(NULL);
283    delete this->objectManager;
284  }
285
286  if(State::getSkyBox())
287    State::setSkyBox(NULL);
288
289  glmis = NULL;
290  localCamera = NULL;
291  localPlayer = NULL;
292  sky = NULL;
293  terrain = NULL;
294  objectManager = NULL;
295}
296
297
298/**
299 *  loads the scene data
300 * @param root reference to the xml root element
301 */
302ErrorMessage GameWorldData::loadScene(TiXmlElement* root)
303{
304  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
305  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
306
307  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
308
309
310  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
311
312
313  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
314  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
315
316  this->localCamera->setClipRegion(1, 10000.0);
317  if( this->sky != NULL)
318    this->localCamera->addChild(this->sky);
319  SoundEngine::getInstance()->setListener(this->localCamera);
320}
321
322
323
324/**
325 *  unloads the scene data
326 */
327ErrorMessage GameWorldData::unloadScene()
328{
329  /* delete some garphics and scene eingines */
330  delete LightManager::getInstance();
331  delete AnimationPlayer::getInstance();
332  delete PhysicsEngine::getInstance();
333
334  if (this->music != NULL)
335    this->setSoundTrack(NULL);
336  this->music = NULL;
337  /* stop the sound eninge */
338  SoundEngine::getInstance()->flushAllBuffers();
339  SoundEngine::getInstance()->flushAllSources();
340
341  /* unload the shaders */
342  Shader::suspendShader();
343}
344
345
346void GameWorldData::setSoundTrack(const char* name)
347{
348  if (this->music != NULL)
349    delete this->music;
350  this->music = NULL;
351
352  if (name != NULL)
353  {
354    PRINTF(3)("Setting Sound Track to %s\n", name);
355    char* oggFile = ResourceManager::getFullName(name); /// FIXME
356    this->music = new OggPlayer(oggFile);
357    delete[] oggFile;
358
359    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
360    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
361  }
362}
363
364
365void GameWorldData::loadGameRule(const TiXmlElement* root)
366{
367
368  const TiXmlElement* element = root->FirstChildElement("GameRule");
369
370  if( element == NULL)
371  {
372    PRINTF(1)("GameWorld is missing 'GameRule'\n");
373  }
374  else
375  {
376    element = element->FirstChildElement();
377
378    while( element != NULL)
379    {
380      BaseObject* created = Factory::fabricate(element);
381      if (created == NULL /*|| !created->isA(CL_GAME_RULE)*/)
382        delete created;
383      else
384      {
385        this->gameRule = dynamic_cast<GameRules*>(created);
386        element = element->NextSiblingElement();
387      }
388    }
389  }
390
391
392
393}
394
395
396
Note: See TracBrowser for help on using the repository browser.