Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged cleanup into trunk (only improvements)

File size: 11.2 KB
RevLine 
[6402]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
[7036]18#include <fstream>
19#include <iostream>
20
[6402]21#include "game_world_data.h"
22
23#include "state.h"
24#include "substring.h"
25
[7193]26#include "util/loading/game_loader.h"
[9869]27#include "util/loading/resource_manager.h"
[6402]28
29#include "world_entity.h"
30#include "player.h"
[10618]31#include "tools/camera.h"
32#include "tools/cameraman.h"
33#include "environments/terrain.h"
34#include "environments/skybox.h"
[8490]35#include "md2/md2Model.h"
[7287]36#include "world_entities/projectiles/projectile.h"
37#include "npcs/npc_test1.h"
[6402]38#include "playable.h"
39
40#include "light.h"
41
[7193]42#include "util/loading/factory.h"
[9869]43#include "loading/fast_factory.h"
44#include "util/loading/load_param_xml.h"
[6402]45
46#include "graphics_engine.h"
[9869]47#include "graphics_effect.h"
48#include "weather_effects/atmospheric_engine.h"
[6402]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"
[10513]54#include "ai_engine.h"
[6402]55
56#include "glmenu_imagescreen.h"
57
[7020]58#include "game_rules.h"
59
[6402]60#include "ogg_player.h"
61#include "shader.h"
62
63
[9869]64ObjectListDefinition(GameWorldData);
[6402]65/**
66 * constructor of the GameWorldData
67 */
68GameWorldData::GameWorldData()
69{
[9869]70  this->registerObject(this, GameWorldData::_objectList);
[6402]71
72  this->glmis = NULL;
73
[10379]74
[6402]75  this->localPlayer = NULL;
76  this->sky = NULL;
77  this->terrain = NULL;
78
79  this->music = NULL;
80  this->objectManager = NULL;
[7035]81  this->gameRule = NULL;
[6402]82}
83
84
85/**
86 * destructor for the GameWorldData
87 */
88GameWorldData::~GameWorldData()
[6404]89{}
[6402]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();
[10379]102  Camera* localCamera = new Camera();
[10618]103    localCamera->setName ("GameWorldCamera");
[10379]104  State::setCamera(localCamera, localCamera->getTarget());
[10394]105  //CameraMan* camMan = new CameraMan();
106  //State::setCameraman(camMan);
[6402]107  LightManager::getInstance();
[10513]108  AIEngine::getInstance();
[6402]109
[8975]110//  GraphicsEngine::getInstance()->displayFPS(true);
[8717]111
112  return ErrorMessage();
[6402]113}
114
115
116/**
117 *  loads the data from the xml file
118 * @param root reference to the xml root element
119 */
[7370]120ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
[6402]121{
[9869]122  assert (root != NULL);
[6402]123  // load the parameters
124  // name
[9869]125  std::string string = LoadParamBase::grabParameter( root, "name");
[7221]126  if( string.empty() )
[6402]127  {
128    PRINTF(2)("GameWorld is missing a proper 'name'\n");
129    this->setName("Unknown");
130  }
131  else
[7221]132    this->setName(string.c_str());
[6402]133
[10513]134
[6402]135  this->loadGUI(root);
136  this->loadWorldEntities(root);
[10394]137  this->loadCameras(root);
[6402]138  this->loadScene(root);
[8717]139
140  return ErrorMessage();
[6402]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();
[10394]152  this->unloadCameras();
[8717]153
[10513]154  // killl ai engine
155  delete AIEngine::getInstance();
156
[8717]157  return ErrorMessage();
[6402]158}
159
[10394]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  }
[10399]172  else
173  {
174    CameraMan* camMan = new CameraMan();
175    State::setCameraman(camMan);
176  }
[10394]177  return ErrorMessage();
178}
[6402]179
[10394]180
[6402]181/**
[10394]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/**
[7370]196 * @brief loads the GUI data
[6402]197 * @param root reference to the xml root element
198 */
[7370]199ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
[6402]200{
[7370]201  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
[6402]202  if( element == NULL)
203  {
204    PRINTF(2)("no LoadScreen specified, loading default\n");
205
[10317]206    glmis->setBackgroundImage("textures/load_screens/default.jpg");
[10618]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
[6402]211    this->glmis->setMaximum(8);
[6988]212    //     this->glmis->draw();
[6402]213  }
214  else
215  {
216    this->glmis->loadParams(element);
[6988]217    //     this->glmis->draw();
[6402]218  }
219  this->glmis->draw();
[8717]220
221  return ErrorMessage();
[6402]222}
223
224
225/**
[7370]226 * @brief unloads the GUI data
[6402]227 */
228ErrorMessage GameWorldData::unloadGUI()
229{
230  delete this->glmis;
[8717]231
232  return ErrorMessage();
[6402]233}
234
235
236/**
[7370]237 * @brief loads the world entities from the xml file
[6402]238 * @param root reference to the xml root parameter
239 */
[7370]240ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
[6402]241{
[7370]242  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
[9406]243  bool mouseCaptured = EventHandler::getInstance()->grabbedEvents();
244  EventHandler::getInstance()->grabEvents(false);
[6402]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 )
[9406]259        PRINTF(4)("Created a %s: %s\n", created->getClassCName(), created->getCName());
[6402]260
261      //todo do this more elegant
[9869]262      if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID()))
[6771]263      {
[6402]264        this->sky = dynamic_cast<WorldEntity*>(created);
[6771]265        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
266      }
[9869]267      if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID()))
[6402]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();
[7076]280  State::setPlayer(this->localPlayer);
[6402]281
[9869]282  if (!Playable::objectList().empty())
[6402]283  {
[7370]284    /// TODO Make this also loadable
[9869]285    this->localPlayer->setPlayable(Playable::objectList().front());
[6402]286  }
287
[7370]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);
[9235]296  this->tickLists.push_back(OM_GROUP_02);
[7370]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);
[9235]304  this->drawLists.push_back(OM_GROUP_02);
305  this->drawLists.push_back(OM_COMMON);
[7370]306
[6402]307  /* init the pnode tree */
308  PNode::getNullParent()->init();
[8717]309
[9406]310  EventHandler::getInstance()->grabEvents(mouseCaptured);
311
[8717]312  return ErrorMessage();
[6402]313}
314
315
316/**
[6404]317 *  unloads the world entities
[6402]318 */
319ErrorMessage GameWorldData::unloadWorldEntities()
320{
321  FastFactory::flushAll(true);
[7029]322  GraphicsEngine::getInstance()->displayFPS(false);
[6402]323  // erease everything that is left.
324  //secondary cleanup of PNodes;
[9869]325  while (!PNode::objectList().empty())
326    delete PNode::objectList().front();
327
[6402]328  /* remove the player object */
329  if( this->localPlayer)
330    delete this->localPlayer;
[7126]331  State::setPlayer(NULL);
[7311]332  this->localPlayer = NULL;
333  State::setCamera(NULL, NULL);
334  this->sky = NULL;
335  this->terrain = NULL;
[6402]336
[9869]337  while (!GraphicsEffect::objectList().empty())
338    delete GraphicsEffect::objectList().front();
[6981]339
340
[9869]341  while (!Element2D::objectList().empty())
342    delete Element2D::objectList().front();
[6981]343
[7370]344  // At this Point all the WorldEntites should be unloaded.
345  this->tickLists.clear();
346  this->drawLists.clear();
[6981]347
[7370]348  // unload the resources loaded in this Level !!
[9869]349  /// FIXME TODO HACK!!
350//  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
[6402]351
352  if (State::getObjectManager() == this->objectManager)
353  {
354    State::setObjectManager(NULL);
355    delete this->objectManager;
356  }
[7311]357  this->objectManager = NULL;
[6863]358
[6771]359  if(State::getSkyBox())
360    State::setSkyBox(NULL);
[6863]361
[7311]362  this->glmis = NULL;
[8717]363
364  return ErrorMessage();
[6402]365}
366
367
368/**
[7370]369 * @brief loads the scene data
[6402]370 * @param root reference to the xml root element
371 */
[7370]372ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
[6402]373{
374  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
[6815]375  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
[7810]376  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
[6402]377
[6827]378  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
379
[7020]380  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
381
[10379]382  LoadParam(root, "clip-region", State::getCameraman(), CameraMan, setClipRegion);
[7020]383
[9235]384
[7020]385  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
[6402]386  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
387
[8717]388
[10379]389  OrxSound::SoundEngine::getInstance()->setListener(State::getCamera());
390
[8717]391  return ErrorMessage();
[6402]392}
393
394
[7020]395
[6402]396/**
397 *  unloads the scene data
398 */
399ErrorMessage GameWorldData::unloadScene()
400{
401  /* delete some garphics and scene eingines */
402  delete LightManager::getInstance();
[7810]403  delete AtmosphericEngine::getInstance();
[6402]404
[7287]405  if (this->music != NULL)
406    this->setSoundTrack("");
[6402]407
408  /* unload the shaders */
409  Shader::suspendShader();
[7488]410
411  State::setGameRules(NULL);
[8717]412
413  return ErrorMessage();
[6402]414}
415
[6634]416
[7221]417void GameWorldData::setSoundTrack(const std::string& name)
[6634]418{
[6827]419  if (this->music != NULL)
[7287]420    delete this->music;
421  this->music = NULL;
[6827]422
[7221]423  if (!name.empty())
[6987]424  {
[7221]425    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
[9869]426    std::string oggFile = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(name);
[7460]427    this->music = new OrxSound::OggPlayer(oggFile);
[10428]428//     if (this->localPlayer != NULL)
429//       this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title());
[6988]430
431    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
432    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
[6987]433  }
[6634]434}
435
436
[7020]437void GameWorldData::loadGameRule(const TiXmlElement* root)
438{
[7036]439  const TiXmlElement* element = root->FirstChildElement();
440  while( element != NULL)
[7020]441  {
[7461]442    PRINTF(2)("============ GameRules ==\n");
443    PRINTF(2)("creating %s\n", element->Value());
[7036]444    BaseObject* created = Factory::fabricate(element);
[9869]445    if (created != NULL && created->isA(GameRules::staticClassID()))
[7020]446    {
[7036]447      this->gameRule = dynamic_cast<GameRules*>(created);
[7039]448      State::setGameRules(this->gameRule);
[7482]449      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
[7462]450      return;
[7020]451    }
[7036]452    else
453    {
454      PRINTF(1)("Could not create a %s\n", element->Value());
455      delete created;
456    }
457    element = element->NextSiblingElement();
[7020]458  }
459}
460
461
462
Note: See TracBrowser for help on using the repository browser.