Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

moved around the weather effecs

File size: 10.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
[7193]23#include "util/loading/resource_manager.h"
[6402]24#include "state.h"
25#include "substring.h"
26
[7193]27#include "util/loading/game_loader.h"
[6402]28
29#include "world_entity.h"
30#include "player.h"
31#include "camera.h"
[7287]32#include "terrain.h"
[7286]33#include "skybox.h"
[8490]34#include "md2/md2Model.h"
[7287]35#include "world_entities/projectiles/projectile.h"
36#include "npcs/npc_test1.h"
[6402]37#include "playable.h"
38
39#include "light.h"
40
[7193]41#include "util/loading/factory.h"
[9709]42#include "loading/fast_factory.h"
[9727]43#include "util/loading/load_param_xml.h"
[6402]44
45#include "graphics_engine.h"
[9760]46#include "weather_effects/graphics_effect.h"
47#include "weather_effects/atmospheric_engine.h"
[6402]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
[7020]57#include "game_rules.h"
58
[6402]59#include "ogg_player.h"
60#include "shader.h"
61
62
[9715]63ObjectListDefinition(GameWorldData);
[6402]64/**
65 * constructor of the GameWorldData
66 */
67GameWorldData::GameWorldData()
68{
[9709]69  this->registerObject(this, GameWorldData::_objectList);
[6402]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;
[7035]80  this->gameRule = NULL;
[6402]81}
82
83
84/**
85 * destructor for the GameWorldData
86 */
87GameWorldData::~GameWorldData()
[6404]88{}
[6402]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
[8975]107//  GraphicsEngine::getInstance()->displayFPS(true);
[8717]108
109  return ErrorMessage();
[6402]110}
111
112
113/**
114 *  loads the data from the xml file
115 * @param root reference to the xml root element
116 */
[7370]117ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
[6402]118{
[9712]119  assert (root != NULL);
[6402]120  // load the parameters
121  // name
[7221]122  std::string string = grabParameter( root, "name");
123  if( string.empty() )
[6402]124  {
125    PRINTF(2)("GameWorld is missing a proper 'name'\n");
126    this->setName("Unknown");
127  }
128  else
[7221]129    this->setName(string.c_str());
[6402]130
131  this->loadGUI(root);
132  this->loadWorldEntities(root);
133  this->loadScene(root);
[8717]134
135  return ErrorMessage();
[6402]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();
[8717]147
148  return ErrorMessage();
[6402]149}
150
151
152/**
[7370]153 * @brief loads the GUI data
[6402]154 * @param root reference to the xml root element
155 */
[7370]156ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
[6402]157{
[7370]158  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
[6402]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);
[6988]165    //     this->glmis->draw();
[6402]166  }
167  else
168  {
169    this->glmis->loadParams(element);
[6988]170    //     this->glmis->draw();
[6402]171  }
172  this->glmis->draw();
[8717]173
174  return ErrorMessage();
[6402]175}
176
177
178/**
[7370]179 * @brief unloads the GUI data
[6402]180 */
181ErrorMessage GameWorldData::unloadGUI()
182{
183  delete this->glmis;
[8717]184
185  return ErrorMessage();
[6402]186}
187
188
189/**
[7370]190 * @brief loads the world entities from the xml file
[6402]191 * @param root reference to the xml root parameter
192 */
[7370]193ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
[6402]194{
[7370]195  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
[9406]196  bool mouseCaptured = EventHandler::getInstance()->grabbedEvents();
197  EventHandler::getInstance()->grabEvents(false);
[6402]198
199  if( element == NULL)
200  {
201    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
202  }
203  else
204  {
205    element = element->FirstChildElement();
206    // load Players/Objects/Whatever
207    PRINTF(4)("Loading WorldEntities\n");
208    while( element != NULL)
209    {
210      BaseObject* created = Factory::fabricate(element);
211      if( created != NULL )
[9406]212        PRINTF(4)("Created a %s: %s\n", created->getClassCName(), created->getCName());
[6402]213
214      //todo do this more elegant
[9757]215      if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID()))
[6771]216      {
[6402]217        this->sky = dynamic_cast<WorldEntity*>(created);
[6771]218        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
219      }
[9757]220      if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID()))
[6402]221      {
222        this->terrain = dynamic_cast<Terrain*>(created);
223        CDEngine::getInstance()->setTerrain(terrain);
224      }
225      element = element->NextSiblingElement();
226      this->glmis->step(); //! @todo temporary
227    }
228    PRINTF(4)("Done loading WorldEntities\n");
229  }
230
231  // Create a Player
232  this->localPlayer = new Player();
[7076]233  State::setPlayer(this->localPlayer);
[6402]234
235  Playable* playable;
[9709]236  if (!Playable::objectList().empty())
[6402]237  {
[7370]238    /// TODO Make this also loadable
[9712]239    this->localPlayer->setPlayable(Playable::objectList().front());
[6402]240  }
241
[7370]242  // Fill the EntityLists. Tick then Draw:
243  this->tickLists.push_back(OM_DEAD_TICK);
244  this->tickLists.push_back(OM_ENVIRON);
245  this->tickLists.push_back(OM_COMMON);
246  this->tickLists.push_back(OM_GROUP_00);
247  this->tickLists.push_back(OM_GROUP_00_PROJ);
248  this->tickLists.push_back(OM_GROUP_01);
249  this->tickLists.push_back(OM_GROUP_01_PROJ);
[9235]250  this->tickLists.push_back(OM_GROUP_02);
[7370]251
252  this->drawLists.push_back(OM_ENVIRON_NOTICK);
253  this->drawLists.push_back(OM_ENVIRON);
254  this->drawLists.push_back(OM_GROUP_00);
255  this->drawLists.push_back(OM_GROUP_00_PROJ);
256  this->drawLists.push_back(OM_GROUP_01);
257  this->drawLists.push_back(OM_GROUP_01_PROJ);
[9235]258  this->drawLists.push_back(OM_GROUP_02);
259  this->drawLists.push_back(OM_COMMON);
[7370]260
[6402]261  /* init the pnode tree */
262  PNode::getNullParent()->init();
[8717]263
[9406]264  EventHandler::getInstance()->grabEvents(mouseCaptured);
265
[8717]266  return ErrorMessage();
[6402]267}
268
269
270/**
[6404]271 *  unloads the world entities
[6402]272 */
273ErrorMessage GameWorldData::unloadWorldEntities()
274{
275  FastFactory::flushAll(true);
[7029]276  GraphicsEngine::getInstance()->displayFPS(false);
[6402]277  // erease everything that is left.
278  //secondary cleanup of PNodes;
[9709]279  while (!PNode::objectList().empty())
280    delete PNode::objectList().front();
281
[6402]282  /* remove the player object */
283  if( this->localPlayer)
284    delete this->localPlayer;
[7126]285  State::setPlayer(NULL);
[7311]286  this->localPlayer = NULL;
287  this->localCamera = NULL;
288  State::setCamera(NULL, NULL);
289  this->sky = NULL;
290  this->terrain = NULL;
[6402]291
[9709]292  while (!GraphicsEffect::objectList().empty())
293    delete GraphicsEffect::objectList().front();
[6981]294
295
[9709]296  while (!Element2D::objectList().empty())
297    delete Element2D::objectList().front();
[6981]298
[7370]299  // At this Point all the WorldEntites should be unloaded.
300  this->tickLists.clear();
301  this->drawLists.clear();
[6981]302
[7370]303  // unload the resources loaded in this Level !!
[6402]304  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
305
306  if (State::getObjectManager() == this->objectManager)
307  {
308    State::setObjectManager(NULL);
309    delete this->objectManager;
310  }
[7311]311  this->objectManager = NULL;
[6863]312
[6771]313  if(State::getSkyBox())
314    State::setSkyBox(NULL);
[6863]315
[7311]316  this->glmis = NULL;
[8717]317
318  return ErrorMessage();
[6402]319}
320
321
322/**
[7370]323 * @brief loads the scene data
[6402]324 * @param root reference to the xml root element
325 */
[7370]326ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
[6402]327{
328  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
[6815]329  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
[7810]330  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
[6402]331
[6827]332  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
333
[7020]334  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
335
[9235]336  LoadParam(root, "clip-region", this->localCamera, Camera, setClipRegion);
[7020]337
[9235]338
[7020]339  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
[6402]340  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
341
342  if( this->sky != NULL)
343    this->localCamera->addChild(this->sky);
[7460]344  OrxSound::SoundEngine::getInstance()->setListener(this->localCamera);
[8717]345
346  return ErrorMessage();
[6402]347}
348
349
[7020]350
[6402]351/**
352 *  unloads the scene data
353 */
354ErrorMessage GameWorldData::unloadScene()
355{
356  /* delete some garphics and scene eingines */
357  delete LightManager::getInstance();
[7810]358  delete AtmosphericEngine::getInstance();
[6402]359
[7287]360  if (this->music != NULL)
361    this->setSoundTrack("");
[6402]362
363  /* unload the shaders */
364  Shader::suspendShader();
[7488]365
366  State::setGameRules(NULL);
[8717]367
368  return ErrorMessage();
[6402]369}
370
[6634]371
[7221]372void GameWorldData::setSoundTrack(const std::string& name)
[6634]373{
[6827]374  if (this->music != NULL)
[7287]375    delete this->music;
376  this->music = NULL;
[6827]377
[7221]378  if (!name.empty())
[6987]379  {
[7221]380    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
381    std::string oggFile = ResourceManager::getFullName(name);
[7460]382    this->music = new OrxSound::OggPlayer(oggFile);
[9019]383    if (this->localPlayer != NULL)
384      this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title());
[6988]385
386    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
387    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
[6987]388  }
[6634]389}
390
391
[7020]392void GameWorldData::loadGameRule(const TiXmlElement* root)
393{
[7036]394  const TiXmlElement* element = root->FirstChildElement();
395  while( element != NULL)
[7020]396  {
[7461]397    PRINTF(2)("============ GameRules ==\n");
398    PRINTF(2)("creating %s\n", element->Value());
[7036]399    BaseObject* created = Factory::fabricate(element);
[9757]400    if (created != NULL && created->isA(GameRules::staticClassID()))
[7020]401    {
[7036]402      this->gameRule = dynamic_cast<GameRules*>(created);
[7039]403      State::setGameRules(this->gameRule);
[7482]404      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
[7462]405      return;
[7020]406    }
[7036]407    else
408    {
409      PRINTF(1)("Could not create a %s\n", element->Value());
410      delete created;
411    }
412    element = element->NextSiblingElement();
[7020]413  }
414}
415
416
417
Note: See TracBrowser for help on using the repository browser.