Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/game_world.cc @ 7871

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

orxonox/trunk: Element2D is better updated (after PNode :)

File size: 14.6 KB
RevLine 
[6352]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   co-programmer: Christian Meyer
14   co-programmer: Benjamin Grauer
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
18
[6358]19#include "game_world.h"
[6402]20#include "game_world_data.h"
[6352]21
[7287]22#include "util/loading/resource_manager.h"
[6352]23#include "state.h"
[6404]24#include "class_list.h"
[6352]25
[7193]26#include "util/loading/game_loader.h"
[6404]27
[6352]28#include "player.h"
29#include "camera.h"
30#include "environment.h"
31#include "terrain.h"
[7287]32#include "test_entity.h"
33#include "terrain.h"
[6404]34#include "playable.h"
[7785]35#include "environments/mapped_water.h"
[6404]36
[6352]37#include "light.h"
[6404]38
[7193]39#include "util/loading/factory.h"
[7368]40#include "util/loading/load_param.h"
[6404]41#include "fast_factory.h"
42#include "shell_command.h"
[6352]43
44#include "graphics_engine.h"
[7810]45#include "effects/atmospheric_engine.h"
[6404]46#include "event_handler.h"
47#include "sound_engine.h"
48#include "cd_engine.h"
49#include "network_manager.h"
[6352]50#include "physics_engine.h"
[7287]51#include "fields.h"
[6352]52
53#include "glmenu_imagescreen.h"
[6404]54#include "shell.h"
[6352]55
56#include "ogg_player.h"
[6404]57#include "shader.h"
[6352]58
[7369]59#include "animation_player.h"
60
[7035]61#include "game_rules.h"
[6352]62
[6404]63using namespace std;
[6352]64
65
[7412]66SHELL_COMMAND(speed, GameWorld, setSpeed) ->describe("set the Speed of the Level");
67SHELL_COMMAND(playmode, GameWorld, setPlaymode)
[7723]68->describe("Set the Playmode of the current Level")
69->completionPlugin(0, OrxShell::CompletorStringArray(Playable::playmodeNames, Playable::PlaymodeCount));
[7412]70
[7739]71SHELL_COMMAND(togglePNodeVisibility, GameWorld, togglePNodeVisibility);
72SHELL_COMMAND(showBVLevel, GameWorld, toggleBVVisibility);
[6352]73
74
75
[6989]76GameWorld::GameWorld()
[6402]77    : StoryEntity()
[6352]78{
[6368]79  this->setClassID(CL_GAME_WORLD, "GameWorld");
80  this->setName("Preloaded World - no name yet");
81
82  this->gameTime = 0.0f;
[6370]83  this->setSpeed(1.0f);
[6368]84  this->shell = NULL;
85
86  this->showPNodes = false;
87  this->showBV = false;
[7739]88  this->showBVLevel = 3;
[6368]89
[6845]90  this->dataXML = NULL;
[7391]91  this->gameRules = NULL;
[6352]92}
93
94/**
[6358]95 *  remove the GameWorld from memory
[6352]96 *
97 *  delete everything explicitly, that isn't contained in the parenting tree!
98 *  things contained in the tree are deleted automaticaly
99 */
[6358]100GameWorld::~GameWorld ()
[6352]101{
[6408]102  PRINTF(4)("Deleted GameWorld\n");
[7283]103
[6352]104}
105
106
107
108/**
[6358]109 * loads the parameters of a GameWorld from an XML-element
[6352]110 * @param root the XML-element to load from
111 */
[6358]112void GameWorld::loadParams(const TiXmlElement* root)
[6352]113{
[6512]114  StoryEntity::loadParams(root);
[6352]115
[6376]116  PRINTF(4)("Loaded GameWorld specific stuff\n");
[6352]117}
118
[6368]119
[6352]120/**
121 * this is executed just before load
122 *
123 * since the load function sometimes needs data, that has been initialized
124 * before the load and after the proceeding storyentity has finished
125*/
[6370]126ErrorMessage GameWorld::init()
[6352]127{
128  /* init the world interface */
[7374]129  this->shell = new OrxShell::Shell();
[6352]130
[6498]131  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
[6407]132  this->dataTank->init();
[7369]133
134  /* initialize some engines and graphical elements */
135  AnimationPlayer::getInstance();
136  PhysicsEngine::getInstance();
137
[6352]138}
139
[7288]140
[6352]141/**
[6358]142 *  loads the GameWorld by initializing all resources, and set their default values.
[6352]143 */
[6372]144ErrorMessage GameWorld::loadData()
[6352]145{
[6370]146  this->displayLoadScreen();
147
[6407]148  PRINTF(0)("Loading the GameWorld\n");
149
[7677]150  PRINTF(3)("> Loading world: '%s'\n", getLoadFile().c_str());
[6352]151  TiXmlElement* element;
152  GameLoader* loader = GameLoader::getInstance();
153
[7221]154  if( getLoadFile().empty())
[6402]155  {
[6634]156    PRINTF(1)("GameWorld has no path specified for loading\n");
[6402]157    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
158  }
[6352]159
[7287]160  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
[6402]161  // load the xml world file for further loading
[7287]162  if( !XMLDoc->LoadFile())
[6352]163  {
[7287]164    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile().c_str(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
165    delete XMLDoc;
[6358]166    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
[6352]167  }
168  // check basic validity
[7287]169  TiXmlElement* root = XMLDoc->RootElement();
[6352]170  assert( root != NULL);
171  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
172  {
[6402]173    // report an error
174    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
[7287]175    delete XMLDoc;
[6402]176    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
[6352]177  }
[6402]178  /* the whole loading process for the GameWorld */
[6407]179  this->dataTank->loadData(root);
[6845]180  this->dataXML = (TiXmlElement*)root->Clone();
[6352]181
[7287]182  delete XMLDoc;
[6386]183  this->releaseLoadScreen();
[6352]184}
185
186
187/**
[6387]188 *  unload the data of this GameWorld
189 */
190ErrorMessage GameWorld::unloadData()
191{
[7287]192  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
[7370]193  delete this->shell;
[6387]194
[6981]195  this->dataTank->unloadData();
196
[7369]197  this->shell = NULL;
198  delete AnimationPlayer::getInstance();
199  delete PhysicsEngine::getInstance();
200
[6988]201  State::setCurrentStoryEntity(NULL);
[6845]202  if (this->dataXML)
203    delete this->dataXML;
[6387]204}
205
206
207/**
[6358]208 *  starts the GameWorld
[6352]209 */
[6387]210bool GameWorld::start()
[6352]211{
[7283]212  this->bPaused = false;
213  this->bRunning = true;
[6387]214
215  this->run();
[6352]216}
217
[6402]218
[6352]219/**
220 *  stops the world.
[6402]221 */
[6387]222bool GameWorld::stop()
[6352]223{
[6358]224  PRINTF(3)("GameWorld::stop() - got stop signal\n");
[7283]225  this->bRunning = false;
[6352]226}
227
[6402]228
[6352]229/**
[6402]230 *  pauses the game
231 */
[6387]232bool GameWorld::pause()
[6352]233{
[7283]234  this->bPaused = true;
[6352]235}
236
[6402]237
[6352]238/**
239 *  ends the pause Phase
[6409]240 */
[6387]241bool GameWorld::resume()
[6352]242{
[7283]243  this->bPaused = false;
[6352]244}
245
246
247/**
[6402]248 *  main loop of the world: executing all world relevant function
249 *
250 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
251 * all other member-entities of the world (tick to player, enemies etc.), checking for
252 * collisions drawing everything to the screen.
253 */
254void GameWorld::run()
[6352]255{
[6402]256  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
[6352]257
[7131]258  // initialize Timing
259  this->cycle = 0;
260  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
[7132]261    this->frameTimes[i] = 100;
[7131]262  this->dtS = 0.0f;
263  this->lastFrame = SDL_GetTicks ();
264
[7304]265  if (this->dataTank->music != NULL)
266    this->dataTank->music->play();
267
[7283]268  while( this->bRunning) /* @todo implement pause */
[6402]269  {
270    /* process intput */
271    this->handleInput ();
[7322]272    if( !this->bRunning)
273      break;
[6386]274
[6402]275    /* network synchronisation */
276    this->synchronize ();
277    /* process time */
278    this->tick ();
279    /* process collision */
280    this->collide ();
281    /* update the state */
282    this->update ();
[7391]283    /* check the game rules */
284    this->checkGameRules();
[6402]285    /* draw everything */
286    this->display ();
[7306]287
[6402]288  }
289
[7330]290  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
[6352]291}
292
293
[7338]294void GameWorld::setPlaymode(Playable::Playmode playmode)
295{
296  if (this->dataTank->localPlayer &&
297      this->dataTank->localPlayer->getPlayable() &&
[7339]298      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
[7338]299  {
[7412]300    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode).c_str());
[7338]301  }
[7339]302  else
303  {
[7414]304    PRINTF(1)("Unable to set Playmode %d:'%s'\n", playmode, Playable::playmodeToString(playmode).c_str());
[7339]305  }
[7338]306}
307
[7339]308void GameWorld::setPlaymode(const std::string& playmode)
309{
310  this->setPlaymode(Playable::stringToPlaymode(playmode));
311}
[7338]312
[6352]313/**
314 *  synchronize local data with remote data
315*/
[6358]316void GameWorld::synchronize ()
[6402]317{}
[6352]318
319
320/**
321 *  run all input processing
322
323   the command node is the central input event dispatcher. the node uses the even-queue from
324   sdl and has its own event-passing-queue.
325*/
[6358]326void GameWorld::handleInput ()
[6352]327{
328  EventHandler::getInstance()->process();
329}
330
[6402]331
332/**
[7370]333 * @brief ticks a WorldEntity list
[6402]334 * @param entityList list of the WorldEntities
335 * @param dt time passed since last frame
336 */
[7370]337void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
[6352]338{
[7370]339  ObjectManager::EntityList::iterator entity, next;
[6710]340  next = entityList.begin();
341  while (next != entityList.end())
342  {
343    entity = next++;
[6352]344    (*entity)->tick(dt);
[6710]345  }
[6352]346}
347
[7131]348
[6352]349/**
350 *  advance the timeline
[6402]351 *
352 * this calculates the time used to process one frame (with all input handling, drawing, etc)
353 * the time is mesured in ms and passed to all world-entities and other classes that need
354 * a heart-beat.
355 */
[6358]356void GameWorld::tick ()
[6352]357{
358  Uint32 currentFrame = SDL_GetTicks();
[6402]359
[7283]360  if( !this->bPaused)
[6402]361  {
[7131]362    // CALCULATE FRAMERATE
363    Uint32 frameTimesIndex;
364    Uint32 getTicks;
365    Uint32 i;
[6402]366
[7131]367    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
368    getTicks = SDL_GetTicks();
369    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
370    this->lastFrame = getTicks;
[7132]371    ++this->cycle;
[7131]372    this->dtS = 0;
[7132]373    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
[7131]374      this->dtS += this->frameTimes[i];
[7132]375    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
[6352]376
[7131]377    // TICK everything
[7370]378    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
379      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
[6352]380
[6402]381    /* update tick the rest */
[6407]382    this->dataTank->localCamera->tick(this->dtS);
[6402]383    AnimationPlayer::getInstance()->tick(this->dtS);
384    PhysicsEngine::getInstance()->tick(this->dtS);
[6352]385
[6402]386    GraphicsEngine::getInstance()->tick(this->dtS);
[7810]387    AtmosphericEngine::getInstance()->tick(this->dtS);
[7035]388
389    if( likely(this->dataTank->gameRule != NULL))
390      this->dataTank->gameRule->tick(this->dtS);
[6402]391  }
[6352]392  this->lastFrame = currentFrame;
393}
394
395
396/**
397 *  this function gives the world a consistant state
[6402]398 *
399 * after ticking (updating the world state) this will give a constistant
400 * state to the whole system.
401 */
[6358]402void GameWorld::update()
[6352]403{
404  PNode::getNullParent()->updateNode (this->dtS);
[7460]405  OrxSound::SoundEngine::getInstance()->update();
[7871]406  GraphicsEngine::getInstance()->update(this->dtS);
[6352]407}
408
409
[6402]410/**
411 * kicks the CDEngine to detect the collisions between the object groups in the world
412 */
[6358]413void GameWorld::collide()
[6352]414{
[6407]415  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7004]416      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
[6407]417  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[7004]418      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
[7078]419  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7288]420      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
[6433]421
[7083]422  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
423      this->dataTank->objectManager->getObjectList(OM_COMMON));
[6433]424  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[6407]425      this->dataTank->objectManager->getObjectList(OM_COMMON));
[7083]426
[6352]427}
428
[7391]429
[6352]430/**
[7391]431 *  check the game rules: winning conditions, etc.
432 *
433 */
434void GameWorld::checkGameRules()
435{
436  if( this->gameRules)
437    this->gameRules->tick(this->dtS);
438}
439
440
441/**
[6352]442 *  render the current frame
[6402]443 *
444 * clear all buffers and draw the world
445 */
[6358]446void GameWorld::display ()
[6352]447{
[7785]448  // render the reflection texture
449  this->renderPassReflection();
450  // redner the refraction texture
451  this->renderPassRefraction();
452  // render all
453  this->renderPassAll();
454
[6352]455  // flip buffers
456  GraphicsEngine::swapBuffers();
457}
458
459
460/**
[7370]461 * @brief draws all entities in the list drawList
462 * @param drawList the List of entities to draw.
[6352]463 */
[7370]464void GameWorld::drawEntityList(const ObjectManager::EntityList& drawList) const
465{
466  ObjectManager::EntityList::const_iterator entity;
467  for (entity = drawList.begin(); entity != drawList.end(); entity++)
468    if ((*entity)->isVisible())
469      (*entity)->draw();
470}
471
[7785]472
473
[7370]474/**
[7785]475 * reflection rendering for water surfaces
[7370]476 */
[7785]477void GameWorld::renderPassReflection()
[6352]478{
[7785]479    // clear buffer
480  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
481  glLoadIdentity();
482
483  const std::list<BaseObject*>* reflectedWaters;
484  MappedWater* mw;
485
486  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
487  {
488    std::list<BaseObject*>::const_iterator it;
489    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
490    {
491      mw =  dynamic_cast<MappedWater*>(*it);
492
493      // prepare for reflection rendering
494      mw->activateReflection();
495
496      //camera and light
497      this->dataTank->localCamera->apply ();
498      this->dataTank->localCamera->project ();
499      LightManager::getInstance()->draw();
500      // draw everything to be included in the reflection
501      for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
502        this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
503
504      // clean up from reflection rendering
505      mw->deactivateReflection();
506    }
507  }
508
509}
510
511
512/**
513 *  refraction rendering for water surfaces
514 */
515void GameWorld::renderPassRefraction()
516{}
517
518
519/**
520 *  this render pass renders the whole wolrd
521 */
522void GameWorld::renderPassAll()
523{
524  // clear buffer
525  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
[6352]526  GraphicsEngine* engine = GraphicsEngine::getInstance();
[6402]527
[7810]528  AtmosphericEngine::getInstance()->draw();
529
[6780]530  // set camera
531  this->dataTank->localCamera->apply ();
[7108]532  this->dataTank->localCamera->project ();
533  LightManager::getInstance()->draw();
[6416]534
[7840]535  this->drawEntityList(State::getObjectManager()->getObjectList(OM_BACKGROUND));
536  engine->drawBackgroundElements();
537
[6416]538  /* draw all WorldEntiy groups */
[7370]539  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
[7723]540    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
[6416]541
[6780]542
[6402]543  if( unlikely( this->showBV))
544  {
545    CDEngine* engine = CDEngine::getInstance();
[7370]546    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
[7739]547      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]), this->showBVLevel);
[6402]548  }
[6416]549
[6402]550  if( unlikely(this->showPNodes))
551    PNode::getNullParent()->debugDraw(0);
552
[6780]553  engine->draw();
[7035]554
555  // draw the game ruls
556  if( likely(this->dataTank->gameRule != NULL))
557    this->dataTank->gameRule->draw();
[6352]558}
559
[7785]560
[6352]561/**
[6402]562 *  shows the loading screen
563 */
564void GameWorld::displayLoadScreen ()
[6387]565{
[6402]566  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
[6407]567  this->dataTank->glmis = new GLMenuImageScreen();
568  this->dataTank->glmis->setMaximum(8);
[6402]569  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
[6387]570}
571
572
[6402]573/**
574 *  removes the loadscreen, and changes over to the game
575 */
576void GameWorld::releaseLoadScreen ()
[6387]577{
[6402]578  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
[6407]579  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
[6402]580  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
[6387]581}
582
[7004]583
584
585/**
586 * @brief toggles the PNode visibility in the world (drawn as boxes)
587 */
588void GameWorld::togglePNodeVisibility()
589{
[7723]590  this->showPNodes = !this->showPNodes;
[7004]591};
592
593
594/**
595 * @brief toggles the bounding volume (BV) visibility
596*/
[7739]597void GameWorld::toggleBVVisibility(int level)
[7004]598{
[7739]599  if( level < 1)
600    this->showBV = false;
601  else
602  {
603    this->showBV = true;
604    this->showBVLevel = level;
605  }
606
[7004]607};
608
Note: See TracBrowser for help on using the repository browser.