Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/story_entities/game_world.cc @ 8493

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