Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the bsp branche back to trunk

File size: 10.0 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.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
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  // load the parameters
120  // name
121  std::string string = 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
135
136/**
137 *  unloads the data from the xml file
138 */
139ErrorMessage GameWorldData::unloadData()
140{
141  this->unloadGUI();
142  this->unloadWorldEntities();
143  this->unloadScene();
144}
145
146
147/**
148 * @brief loads the GUI data
149 * @param root reference to the xml root element
150 */
151ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
152{
153  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
154  if( element == NULL)
155  {
156    PRINTF(2)("no LoadScreen specified, loading default\n");
157
158    glmis->setBackgroundImage("pictures/load_screen.jpg");
159    this->glmis->setMaximum(8);
160    //     this->glmis->draw();
161  }
162  else
163  {
164    this->glmis->loadParams(element);
165    //     this->glmis->draw();
166  }
167  this->glmis->draw();
168}
169
170
171/**
172 * @brief unloads the GUI data
173 */
174ErrorMessage GameWorldData::unloadGUI()
175{
176  delete this->glmis;
177}
178
179
180/**
181 * @brief loads the world entities from the xml file
182 * @param root reference to the xml root parameter
183 */
184ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
185{
186  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
187
188  if( element == NULL)
189  {
190    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
191  }
192  else
193  {
194    element = element->FirstChildElement();
195    // load Players/Objects/Whatever
196    PRINTF(4)("Loading WorldEntities\n");
197    while( element != NULL)
198    {
199      BaseObject* created = Factory::fabricate(element);
200      if( created != NULL )
201        PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName());
202
203      //todo do this more elegant
204      if( element->Value() == "SkyBox" && created->isA(CL_SKYBOX))
205      {
206        this->sky = dynamic_cast<WorldEntity*>(created);
207        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
208      }
209      if( element->Value() == "Terrain" && created->isA(CL_TERRAIN))
210      {
211        this->terrain = dynamic_cast<Terrain*>(created);
212        CDEngine::getInstance()->setTerrain(terrain);
213      }
214      element = element->NextSiblingElement();
215      this->glmis->step(); //! @todo temporary
216    }
217    PRINTF(4)("Done loading WorldEntities\n");
218  }
219
220  // Create a Player
221  this->localPlayer = new Player();
222  State::setPlayer(this->localPlayer);
223
224  Playable* playable;
225  const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
226  if (playableList != NULL && !playableList->empty())
227  {
228    /// TODO Make this also loadable
229    playable = dynamic_cast<Playable*>(playableList->front());
230    this->localPlayer->setPlayable(playable);
231  }
232
233  // Fill the EntityLists. Tick then Draw:
234  this->tickLists.push_back(OM_DEAD_TICK);
235  this->tickLists.push_back(OM_ENVIRON);
236  this->tickLists.push_back(OM_COMMON);
237  this->tickLists.push_back(OM_GROUP_00);
238  this->tickLists.push_back(OM_GROUP_00_PROJ);
239  this->tickLists.push_back(OM_GROUP_01);
240  this->tickLists.push_back(OM_GROUP_01_PROJ);
241
242  this->drawLists.push_back(OM_ENVIRON_NOTICK);
243  this->drawLists.push_back(OM_ENVIRON);
244  this->drawLists.push_back(OM_COMMON);
245  this->drawLists.push_back(OM_GROUP_00);
246  this->drawLists.push_back(OM_GROUP_00_PROJ);
247  this->drawLists.push_back(OM_GROUP_01);
248  this->drawLists.push_back(OM_GROUP_01_PROJ);
249
250  /* init the pnode tree */
251  PNode::getNullParent()->init();
252}
253
254
255/**
256 *  unloads the world entities
257 */
258ErrorMessage GameWorldData::unloadWorldEntities()
259{
260  FastFactory::flushAll(true);
261  GraphicsEngine::getInstance()->displayFPS(false);
262  // erease everything that is left.
263  // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner)
264  const std::list<BaseObject*>* nodeList;
265  //secondary cleanup of PNodes;
266  nodeList = ClassList::getList(CL_PARENT_NODE);
267  if (nodeList != NULL)
268    while (!nodeList->empty())
269    {
270      //    ClassList::debug( 3, CL_PARENT_NODE);
271      //    PNode::getNullParent()->debugNode(0);
272      //    printf("%s::%s\n", nodeList->front()->getClassName(), nodeList->front()->getName());
273      delete nodeList->front();
274    }
275  /* remove the player object */
276  if( this->localPlayer)
277    delete this->localPlayer;
278  State::setPlayer(NULL);
279  this->localPlayer = NULL;
280  this->localCamera = NULL;
281  State::setCamera(NULL, NULL);
282  this->sky = NULL;
283  this->terrain = NULL;
284
285  nodeList = ClassList::getList(CL_GRAPHICS_EFFECT);
286  if (nodeList != NULL)
287    while (!nodeList->empty())
288      delete nodeList->front();
289
290
291  nodeList = ClassList::getList(CL_ELEMENT_2D);
292  if (nodeList != NULL)
293    while (!nodeList->empty())
294      delete nodeList->front();
295
296  // At this Point all the WorldEntites should be unloaded.
297  this->tickLists.clear();
298  this->drawLists.clear();
299
300  // unload the resources loaded in this Level !!
301  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
302
303  if (State::getObjectManager() == this->objectManager)
304  {
305    State::setObjectManager(NULL);
306    delete this->objectManager;
307  }
308  this->objectManager = NULL;
309
310  if(State::getSkyBox())
311    State::setSkyBox(NULL);
312
313  this->glmis = NULL;
314}
315
316
317/**
318 * @brief loads the scene data
319 * @param root reference to the xml root element
320 */
321ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
322{
323  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
324  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
325  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
326
327  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
328
329  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
330
331
332  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
333  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
334
335  this->localCamera->setClipRegion(1, 10000.0);
336  if( this->sky != NULL)
337    this->localCamera->addChild(this->sky);
338  OrxSound::SoundEngine::getInstance()->setListener(this->localCamera);
339}
340
341
342
343/**
344 *  unloads the scene data
345 */
346ErrorMessage GameWorldData::unloadScene()
347{
348  /* delete some garphics and scene eingines */
349  delete LightManager::getInstance();
350  delete AtmosphericEngine::getInstance();
351
352  if (this->music != NULL)
353    this->setSoundTrack("");
354
355  /* unload the shaders */
356  Shader::suspendShader();
357
358  State::setGameRules(NULL);
359}
360
361
362void GameWorldData::setSoundTrack(const std::string& name)
363{
364  if (this->music != NULL)
365    delete this->music;
366  this->music = NULL;
367
368  if (!name.empty())
369  {
370    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
371    std::string oggFile = ResourceManager::getFullName(name);
372    this->music = new OrxSound::OggPlayer(oggFile);
373
374    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
375    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
376  }
377}
378
379
380void GameWorldData::loadGameRule(const TiXmlElement* root)
381{
382  const TiXmlElement* element = root->FirstChildElement();
383  while( element != NULL)
384  {
385    PRINTF(2)("============ GameRules ==\n");
386    PRINTF(2)("creating %s\n", element->Value());
387    BaseObject* created = Factory::fabricate(element);
388    if (created != NULL && created->isA(CL_GAME_RULES))
389    {
390      this->gameRule = dynamic_cast<GameRules*>(created);
391      State::setGameRules(this->gameRule);
392      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
393      return;
394    }
395    else
396    {
397      PRINTF(1)("Could not create a %s\n", element->Value());
398      delete created;
399    }
400    element = element->NextSiblingElement();
401  }
402}
403
404
405
Note: See TracBrowser for help on using the repository browser.