Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10428 was 10428, checked in by patrick, 17 years ago

added a script interface to game world for displaying txt

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