Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new style for resources (prework/movement)

File size: 13.2 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
[7193]22#include "util/loading/resource_manager.h"
[6352]23#include "state.h"
[6404]24#include "class_list.h"
25#include "substring.h"
[6352]26
[7193]27#include "util/loading/game_loader.h"
[6404]28
[6352]29#include "p_node.h"
30#include "world_entity.h"
31#include "player.h"
32#include "camera.h"
33#include "environment.h"
34#include "terrain.h"
35#include "test_entity.h"
36#include "terrain.h"
[6404]37#include "md2Model.h"
[6434]38#include "world_entities/projectiles/projectile.h"
[6404]39#include "npcs/npc_test1.h"
40#include "playable.h"
41
[6352]42#include "light.h"
[6404]43
[7193]44#include "util/loading/factory.h"
[6404]45#include "fast_factory.h"
[7193]46#include "util/loading/load_param.h"
[6404]47#include "shell_command.h"
[6352]48
49#include "graphics_engine.h"
[6404]50#include "event_handler.h"
51#include "sound_engine.h"
52#include "cd_engine.h"
53#include "network_manager.h"
[6352]54#include "physics_engine.h"
55#include "fields.h"
56
57#include "glmenu_imagescreen.h"
[6404]58#include "shell.h"
[6352]59
[6404]60#include "animation_player.h"
[6352]61#include "animation3d.h"
62
63#include "ogg_player.h"
[6404]64#include "shader.h"
[6352]65
[7035]66#include "game_rules.h"
[6352]67
[6404]68using namespace std;
[6352]69
70
[6358]71SHELL_COMMAND(speed, GameWorld, setSpeed);
[7004]72SHELL_COMMAND_STATIC(togglePNodeVisibility, GameWorld, GameWorld::togglePNodeVisibility);
73SHELL_COMMAND_STATIC(toggleBVVisibility, GameWorld, GameWorld::toggleBVVisibility);
[6352]74
75
76
[6989]77GameWorld::GameWorld()
[6402]78    : StoryEntity()
[6352]79{
[6368]80  this->setClassID(CL_GAME_WORLD, "GameWorld");
81  this->setName("Preloaded World - no name yet");
82
83  this->gameTime = 0.0f;
[6370]84  this->setSpeed(1.0f);
[6368]85  this->shell = NULL;
86
87  this->showPNodes = false;
88  this->showBV = false;
89
[6845]90  this->dataXML = NULL;
[6352]91}
92
93/**
[6358]94 *  remove the GameWorld from memory
[6352]95 *
96 *  delete everything explicitly, that isn't contained in the parenting tree!
97 *  things contained in the tree are deleted automaticaly
98 */
[6358]99GameWorld::~GameWorld ()
[6352]100{
[6408]101  PRINTF(4)("Deleted GameWorld\n");
[6352]102}
103
104
105
106/**
[6358]107 * loads the parameters of a GameWorld from an XML-element
[6352]108 * @param root the XML-element to load from
109 */
[6358]110void GameWorld::loadParams(const TiXmlElement* root)
[6352]111{
[6512]112  StoryEntity::loadParams(root);
[6352]113
[6376]114  PRINTF(4)("Loaded GameWorld specific stuff\n");
[6352]115}
116
[6368]117
[6352]118/**
119 * this is executed just before load
120 *
121 * since the load function sometimes needs data, that has been initialized
122 * before the load and after the proceeding storyentity has finished
123*/
[6370]124ErrorMessage GameWorld::init()
[6352]125{
126  /* init the world interface */
127  this->shell = new Shell();
128
[6498]129  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
[6407]130  this->dataTank->init();
[6352]131}
132
133
134/**
[6358]135 *  loads the GameWorld by initializing all resources, and set their default values.
[6352]136 */
[6372]137ErrorMessage GameWorld::loadData()
[6352]138{
[6370]139  this->displayLoadScreen();
140
[6407]141  PRINTF(0)("Loading the GameWorld\n");
142
[6992]143  PRINTF(3)("> Loading world: '%s'\n", getLoadFile());
[6352]144  TiXmlElement* element;
145  GameLoader* loader = GameLoader::getInstance();
146
[6992]147  if( getLoadFile() == NULL)
[6402]148  {
[6634]149    PRINTF(1)("GameWorld has no path specified for loading\n");
[6402]150    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
151  }
[6352]152
[6992]153  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
[6402]154  // load the xml world file for further loading
[6352]155  if( !XMLDoc->LoadFile())
156  {
[6992]157    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
[6352]158    delete XMLDoc;
[6358]159    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
[6352]160  }
161  // check basic validity
162  TiXmlElement* root = XMLDoc->RootElement();
163  assert( root != NULL);
164  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
165  {
[6402]166    // report an error
167    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
168    delete XMLDoc;
169    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
[6352]170  }
[6402]171  /* the whole loading process for the GameWorld */
[6407]172  this->dataTank->loadData(root);
[6845]173  this->dataXML = (TiXmlElement*)root->Clone();
[6352]174
[6402]175  delete XMLDoc;
[6386]176  this->releaseLoadScreen();
[6352]177}
178
179
180/**
[6387]181 *  unload the data of this GameWorld
182 */
183ErrorMessage GameWorld::unloadData()
184{
185  delete this->shell;
186  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
187
[6981]188  this->dataTank->unloadData();
189
[6988]190  State::setCurrentStoryEntity(NULL);
[6845]191  if (this->dataXML)
192    delete this->dataXML;
[6387]193}
194
195
196/**
[6358]197 *  starts the GameWorld
[6352]198 */
[6387]199bool GameWorld::start()
[6352]200{
[6386]201  this->isPaused = false;
202  this->isRunning = true;
[6387]203
204  this->run();
[6352]205}
206
[6402]207
[6352]208/**
209 *  stops the world.
[6402]210 */
[6387]211bool GameWorld::stop()
[6352]212{
[6358]213  PRINTF(3)("GameWorld::stop() - got stop signal\n");
[6386]214  this->isRunning = false;
[6352]215}
216
[6402]217
[6352]218/**
[6402]219 *  pauses the game
220 */
[6387]221bool GameWorld::pause()
[6352]222{
223  this->isPaused = true;
224}
225
[6402]226
[6352]227/**
228 *  ends the pause Phase
[6409]229 */
[6387]230bool GameWorld::resume()
[6352]231{
232  this->isPaused = false;
233}
234
235
236/**
[6402]237 *  main loop of the world: executing all world relevant function
238 *
239 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
240 * all other member-entities of the world (tick to player, enemies etc.), checking for
241 * collisions drawing everything to the screen.
242 */
243void GameWorld::run()
[6352]244{
[6634]245  /* start the music */
246  if(this->dataTank->music != NULL)
247    this->dataTank->music->playback();
248
[6402]249  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
[6352]250
[7131]251  // initialize Timing
252  this->cycle = 0;
253  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
[7132]254    this->frameTimes[i] = 100;
[7131]255  this->dtS = 0.0f;
256  this->lastFrame = SDL_GetTicks ();
257
[6402]258  while( this->isRunning) /* @todo implement pause */
259  {
260    /* process intput */
261    this->handleInput ();
262    if( !this->isRunning)
263      break;
[6386]264
[6402]265    /* network synchronisation */
266    this->synchronize ();
267    /* process time */
268    this->tick ();
269    /* process collision */
270    this->collide ();
271    /* update the state */
272    this->update ();
273    /* draw everything */
274    this->display ();
275  }
276
277  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
[6352]278}
279
280
281/**
282 *  synchronize local data with remote data
283*/
[6358]284void GameWorld::synchronize ()
[6402]285{}
[6352]286
287
288/**
289 *  run all input processing
290
291   the command node is the central input event dispatcher. the node uses the even-queue from
292   sdl and has its own event-passing-queue.
293*/
[6358]294void GameWorld::handleInput ()
[6352]295{
296  EventHandler::getInstance()->process();
297}
298
[6402]299
300/**
301 *  ticks a WorldEntity list
302 * @param entityList list of the WorldEntities
303 * @param dt time passed since last frame
304 */
[6358]305void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
[6352]306{
[6710]307  std::list<WorldEntity*>::iterator entity, next;
308  next = entityList.begin();
309  while (next != entityList.end())
310  {
311    entity = next++;
[6352]312    (*entity)->tick(dt);
[6710]313  }
[6352]314}
315
[7131]316
[6352]317/**
318 *  advance the timeline
[6402]319 *
320 * this calculates the time used to process one frame (with all input handling, drawing, etc)
321 * the time is mesured in ms and passed to all world-entities and other classes that need
322 * a heart-beat.
323 */
[6358]324void GameWorld::tick ()
[6352]325{
326  Uint32 currentFrame = SDL_GetTicks();
[6402]327
[6386]328  if( !this->isPaused)
[6402]329  {
[7131]330    // CALCULATE FRAMERATE
331    Uint32 frameTimesIndex;
332    Uint32 getTicks;
333    Uint32 i;
[6402]334
[7131]335    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
336    getTicks = SDL_GetTicks();
337    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
338    this->lastFrame = getTicks;
[7132]339    ++this->cycle;
[7131]340    this->dtS = 0;
[7132]341    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
[7131]342      this->dtS += this->frameTimes[i];
[7132]343    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
[6352]344
[7131]345    // TICK everything
[6407]346    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
[6457]347    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
[6407]348    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
349    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
[6433]350    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
[6407]351    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
352    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
[6352]353
[6402]354    /* update tick the rest */
[6407]355    this->dataTank->localCamera->tick(this->dtS);
[6402]356    AnimationPlayer::getInstance()->tick(this->dtS);
357    PhysicsEngine::getInstance()->tick(this->dtS);
[6352]358
[6402]359    GraphicsEngine::getInstance()->tick(this->dtS);
[7035]360
361    if( likely(this->dataTank->gameRule != NULL))
362      this->dataTank->gameRule->tick(this->dtS);
[6402]363  }
[6352]364  this->lastFrame = currentFrame;
365}
366
367
368/**
369 *  this function gives the world a consistant state
[6402]370 *
371 * after ticking (updating the world state) this will give a constistant
372 * state to the whole system.
373 */
[6358]374void GameWorld::update()
[6352]375{
376  GraphicsEngine::getInstance()->update(this->dtS);
377  PNode::getNullParent()->updateNode (this->dtS);
378  SoundEngine::getInstance()->update();
[6827]379
380  if (this->dataTank->music != NULL)
381    this->dataTank->music->update();
[6352]382}
383
384
[6402]385/**
386 * kicks the CDEngine to detect the collisions between the object groups in the world
387 */
[6358]388void GameWorld::collide()
[6352]389{
[6407]390  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7004]391      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
[6407]392  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[7004]393      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
[7078]394  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
395  this->dataTank->objectManager->getObjectList(OM_GROUP_01));
[6433]396
[7083]397  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
398      this->dataTank->objectManager->getObjectList(OM_COMMON));
[6433]399  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[6407]400      this->dataTank->objectManager->getObjectList(OM_COMMON));
[7083]401
[6352]402}
403
404/**
405 *  render the current frame
[6402]406 *
407 * clear all buffers and draw the world
408 */
[6358]409void GameWorld::display ()
[6352]410{
411  // clear buffer
412  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
413  // draw world
414  this->draw();
415  // flip buffers
416  GraphicsEngine::swapBuffers();
417}
418
419
420/**
421 *  runs through all entities calling their draw() methods
422 */
[6358]423void GameWorld::draw ()
[6352]424{
425  GraphicsEngine* engine = GraphicsEngine::getInstance();
[6402]426
[6780]427  // set camera
428  this->dataTank->localCamera->apply ();
[7108]429  this->dataTank->localCamera->project ();
430  LightManager::getInstance()->draw();
[6416]431
432  /* draw all WorldEntiy groups */
433  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
434  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
435  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
436  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
[6433]437  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
[6416]438  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
439  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
440
[6780]441
[6402]442  if( unlikely( this->showBV))
443  {
444    CDEngine* engine = CDEngine::getInstance();
445    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
446    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
447    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
448    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
449    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
450    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
451  }
[6416]452
[6402]453  if( unlikely(this->showPNodes))
454    PNode::getNullParent()->debugDraw(0);
455
[6780]456  engine->draw();
[7035]457
[7108]458
[7035]459  // draw the game ruls
460  if( likely(this->dataTank->gameRule != NULL))
461    this->dataTank->gameRule->draw();
[6352]462}
463
464/**
[6402]465 *  shows the loading screen
466 */
467void GameWorld::displayLoadScreen ()
[6387]468{
[6402]469  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
[6407]470  this->dataTank->glmis = new GLMenuImageScreen();
471  this->dataTank->glmis->setMaximum(8);
[6402]472  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
[6387]473}
474
475
[6402]476/**
477 *  removes the loadscreen, and changes over to the game
478 */
479void GameWorld::releaseLoadScreen ()
[6387]480{
[6402]481  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
[6407]482  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
[6402]483  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
[6387]484}
485
[7004]486
487
488/**
489 * @brief toggles the PNode visibility in the world (drawn as boxes)
490 */
491void GameWorld::togglePNodeVisibility()
492{
493  if (State::getCurrentStoryEntity() != NULL &&
494      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
495  {
496    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes;
497  }
498  else
499    PRINTF(2)("The Current Story entity is not a GameWorld\n");
500};
501
502
503/**
504 * @brief toggles the bounding volume (BV) visibility
505*/
506void GameWorld::toggleBVVisibility()
507{
508  if (State::getCurrentStoryEntity() != NULL &&
509      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
510  {
511    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV;
512  }
513  else
514    PRINTF(2)("The Current Story entity is not a GameWorld\n");
515};
516
Note: See TracBrowser for help on using the repository browser.