Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

File size: 14.6 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
28#include "player.h"
29#include "camera.h"
30#include "environment.h"
31#include "terrain.h"
32#include "test_entity.h"
33#include "terrain.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
138}
139
140
141/**
142 *  loads the GameWorld by initializing all resources, and set their default values.
143 */
144ErrorMessage GameWorld::loadData()
145{
146  this->displayLoadScreen();
147
148  PRINTF(0)("Loading the GameWorld\n");
149
150  PRINTF(3)("> Loading world: '%s'\n", getLoadFile().c_str());
151  TiXmlElement* element;
152  GameLoader* loader = GameLoader::getInstance();
153
154  if( getLoadFile().empty())
155  {
156    PRINTF(1)("GameWorld has no path specified for loading\n");
157    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
158  }
159
160  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
161  // load the xml world file for further loading
162  if( !XMLDoc->LoadFile())
163  {
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;
166    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
167  }
168  // check basic validity
169  TiXmlElement* root = XMLDoc->RootElement();
170  assert( root != NULL);
171  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
172  {
173    // report an error
174    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
175    delete XMLDoc;
176    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
177  }
178  /* the whole loading process for the GameWorld */
179  this->dataTank->loadData(root);
180  this->dataXML = (TiXmlElement*)root->Clone();
181
182  delete XMLDoc;
183  this->releaseLoadScreen();
184}
185
186
187/**
188 *  unload the data of this GameWorld
189 */
190ErrorMessage GameWorld::unloadData()
191{
192  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
193  delete this->shell;
194
195  this->dataTank->unloadData();
196
197  this->shell = NULL;
198  delete AnimationPlayer::getInstance();
199  delete PhysicsEngine::getInstance();
200
201  State::setCurrentStoryEntity(NULL);
202  if (this->dataXML)
203    delete this->dataXML;
204}
205
206
207/**
208 *  starts the GameWorld
209 */
210bool GameWorld::start()
211{
212  this->bPaused = false;
213  this->bRunning = true;
214
215  this->run();
216}
217
218
219/**
220 *  stops the world.
221 */
222bool GameWorld::stop()
223{
224  PRINTF(3)("GameWorld::stop() - got stop signal\n");
225  this->bRunning = false;
226}
227
228
229/**
230 *  pauses the game
231 */
232bool GameWorld::pause()
233{
234  this->bPaused = true;
235}
236
237
238/**
239 *  ends the pause Phase
240 */
241bool GameWorld::resume()
242{
243  this->bPaused = false;
244}
245
246
247/**
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()
255{
256  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
257
258  // initialize Timing
259  this->cycle = 0;
260  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
261    this->frameTimes[i] = 100;
262  this->dtS = 0.0f;
263  this->lastFrame = SDL_GetTicks ();
264
265  if (this->dataTank->music != NULL)
266    this->dataTank->music->play();
267
268  while( this->bRunning) /* @todo implement pause */
269  {
270    /* process intput */
271    this->handleInput ();
272    if( !this->bRunning)
273      break;
274
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 ();
283    /* check the game rules */
284    this->checkGameRules();
285    /* draw everything */
286    this->display ();
287
288  }
289
290  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
291}
292
293
294void GameWorld::setPlaymode(Playable::Playmode playmode)
295{
296  if (this->dataTank->localPlayer &&
297      this->dataTank->localPlayer->getPlayable() &&
298      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
299  {
300    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode).c_str());
301  }
302  else
303  {
304    PRINTF(1)("Unable to set Playmode %d:'%s'\n", playmode, Playable::playmodeToString(playmode).c_str());
305  }
306}
307
308void GameWorld::setPlaymode(const std::string& playmode)
309{
310  this->setPlaymode(Playable::stringToPlaymode(playmode));
311}
312
313/**
314 *  synchronize local data with remote data
315*/
316void GameWorld::synchronize ()
317{}
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*/
326void GameWorld::handleInput ()
327{
328  EventHandler::getInstance()->process();
329}
330
331
332/**
333 * @brief ticks a WorldEntity list
334 * @param entityList list of the WorldEntities
335 * @param dt time passed since last frame
336 */
337void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
338{
339  ObjectManager::EntityList::iterator entity, next;
340  next = entityList.begin();
341  while (next != entityList.end())
342  {
343    entity = next++;
344    (*entity)->tick(dt);
345  }
346}
347
348
349/**
350 *  advance the timeline
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 */
356void GameWorld::tick ()
357{
358  Uint32 currentFrame = SDL_GetTicks();
359
360  if( !this->bPaused)
361  {
362    // CALCULATE FRAMERATE
363    Uint32 frameTimesIndex;
364    Uint32 getTicks;
365    Uint32 i;
366
367    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
368    getTicks = SDL_GetTicks();
369    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
370    this->lastFrame = getTicks;
371    ++this->cycle;
372    this->dtS = 0;
373    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
374      this->dtS += this->frameTimes[i];
375    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
376
377    // TICK everything
378    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
379      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
380
381    /* update tick the rest */
382    this->dataTank->localCamera->tick(this->dtS);
383    AnimationPlayer::getInstance()->tick(this->dtS);
384    PhysicsEngine::getInstance()->tick(this->dtS);
385
386    GraphicsEngine::getInstance()->tick(this->dtS);
387    AtmosphericEngine::getInstance()->tick(this->dtS);
388
389    if( likely(this->dataTank->gameRule != NULL))
390      this->dataTank->gameRule->tick(this->dtS);
391  }
392  this->lastFrame = currentFrame;
393}
394
395
396/**
397 *  this function gives the world a consistant state
398 *
399 * after ticking (updating the world state) this will give a constistant
400 * state to the whole system.
401 */
402void GameWorld::update()
403{
404  PNode::getNullParent()->updateNode (this->dtS);
405  OrxSound::SoundEngine::getInstance()->update();
406  GraphicsEngine::getInstance()->update(this->dtS);
407}
408
409
410/**
411 * kicks the CDEngine to detect the collisions between the object groups in the world
412 */
413void GameWorld::collide()
414{
415  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
416      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
417  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
418      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
419  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
420      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
421
422  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
423      this->dataTank->objectManager->getObjectList(OM_COMMON));
424  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
425      this->dataTank->objectManager->getObjectList(OM_COMMON));
426
427}
428
429
430/**
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/**
442 *  render the current frame
443 *
444 * clear all buffers and draw the world
445 */
446void GameWorld::display ()
447{
448  // render the reflection texture
449  this->renderPassReflection();
450  // redner the refraction texture
451  this->renderPassRefraction();
452  // render all
453  this->renderPassAll();
454
455  // flip buffers
456  GraphicsEngine::swapBuffers();
457}
458
459
460/**
461 * @brief draws all entities in the list drawList
462 * @param drawList the List of entities to draw.
463 */
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
472
473
474/**
475 * reflection rendering for water surfaces
476 */
477void GameWorld::renderPassReflection()
478{
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);
526  GraphicsEngine* engine = GraphicsEngine::getInstance();
527
528  AtmosphericEngine::getInstance()->draw();
529
530  // set camera
531  this->dataTank->localCamera->apply ();
532  this->dataTank->localCamera->project ();
533  LightManager::getInstance()->draw();
534
535  this->drawEntityList(State::getObjectManager()->getObjectList(OM_BACKGROUND));
536  engine->drawBackgroundElements();
537
538  /* draw all WorldEntiy groups */
539  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
540    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
541
542
543  if( unlikely( this->showBV))
544  {
545    CDEngine* engine = CDEngine::getInstance();
546    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
547      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]), this->showBVLevel);
548  }
549
550  if( unlikely(this->showPNodes))
551    PNode::getNullParent()->debugDraw(0);
552
553  engine->draw();
554
555  // draw the game ruls
556  if( likely(this->dataTank->gameRule != NULL))
557    this->dataTank->gameRule->draw();
558}
559
560
561/**
562 *  shows the loading screen
563 */
564void GameWorld::displayLoadScreen ()
565{
566  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
567  this->dataTank->glmis = new GLMenuImageScreen();
568  this->dataTank->glmis->setMaximum(8);
569  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
570}
571
572
573/**
574 *  removes the loadscreen, and changes over to the game
575 */
576void GameWorld::releaseLoadScreen ()
577{
578  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
579  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
580  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
581}
582
583
584
585/**
586 * @brief toggles the PNode visibility in the world (drawn as boxes)
587 */
588void GameWorld::togglePNodeVisibility()
589{
590  this->showPNodes = !this->showPNodes;
591};
592
593
594/**
595 * @brief toggles the bounding volume (BV) visibility
596*/
597void GameWorld::toggleBVVisibility(int level)
598{
599  if( level < 1)
600    this->showBV = false;
601  else
602  {
603    this->showBV = true;
604    this->showBVLevel = level;
605  }
606
607};
608
Note: See TracBrowser for help on using the repository browser.