Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7391 was 7391, checked in by patrick, 18 years ago

orxonox: working on mission goals and mission manager

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