Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10399 was 10399, checked in by snellen, 17 years ago

levels without CameraMan tag work again

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