Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the powerups back to the trunk

File size: 12.1 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
22#include "resource_manager.h"
23#include "state.h"
[6404]24#include "class_list.h"
25#include "substring.h"
[6352]26
[6404]27#include "game_loader.h"
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
44#include "factory.h"
45#include "fast_factory.h"
[6352]46#include "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
66
[6404]67using namespace std;
[6352]68
69
[6358]70SHELL_COMMAND(speed, GameWorld, setSpeed);
71SHELL_COMMAND(togglePNodeVisibility, GameWorld, togglePNodeVisibility);
72SHELL_COMMAND(toggleBVVisibility, GameWorld, toggleBVVisibility);
[6352]73
74
75
[6358]76GameWorld::GameWorld(const TiXmlElement* root)
[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;
88
[6352]89  this->path = NULL;
90}
91
92/**
[6358]93 *  remove the GameWorld from memory
[6352]94 *
95 *  delete everything explicitly, that isn't contained in the parenting tree!
96 *  things contained in the tree are deleted automaticaly
97 */
[6358]98GameWorld::~GameWorld ()
[6352]99{
[6408]100  PRINTF(4)("Deleted GameWorld\n");
[6352]101}
102
103
104
105/**
[6358]106 * loads the parameters of a GameWorld from an XML-element
[6352]107 * @param root the XML-element to load from
108 */
[6358]109void GameWorld::loadParams(const TiXmlElement* root)
[6352]110{
[6512]111  StoryEntity::loadParams(root);
[6352]112
[6358]113  LoadParam(root, "path", this, GameWorld, setPath)
[6402]114  .describe("The Filename of this GameWorld (relative from the data-dir)");
[6376]115
[6634]116//   LoadParam(root, "soundtrack", this->dataTank, GameWorldData, setSoundTrack);
117
[6376]118  PRINTF(4)("Loaded GameWorld specific stuff\n");
[6352]119}
120
[6368]121
[6352]122/**
123 * this is executed just before load
124 *
125 * since the load function sometimes needs data, that has been initialized
126 * before the load and after the proceeding storyentity has finished
127*/
[6370]128ErrorMessage GameWorld::init()
[6352]129{
130  this->cycle = 0;
131  /* init the world interface */
132  this->shell = new Shell();
133
[6498]134  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
[6407]135  this->dataTank->init();
[6352]136}
137
138
139/**
[6358]140 *  loads the GameWorld by initializing all resources, and set their default values.
[6352]141 */
[6372]142ErrorMessage GameWorld::loadData()
[6352]143{
[6370]144  this->displayLoadScreen();
145
[6407]146  PRINTF(0)("Loading the GameWorld\n");
147
[6352]148  PRINTF(3)("> Loading world: '%s'\n", getPath());
149  TiXmlElement* element;
150  GameLoader* loader = GameLoader::getInstance();
151
152  if( getPath() == NULL)
[6402]153  {
[6634]154    PRINTF(1)("GameWorld has no path specified for loading\n");
[6402]155    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
156  }
[6352]157
158  TiXmlDocument* XMLDoc = new TiXmlDocument( getPath());
[6402]159  // load the xml world file for further loading
[6352]160  if( !XMLDoc->LoadFile())
161  {
162    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getPath(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
163    delete XMLDoc;
[6358]164    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
[6352]165  }
166  // check basic validity
167  TiXmlElement* root = XMLDoc->RootElement();
168  assert( root != NULL);
169  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
170  {
[6402]171    // report an error
172    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
173    delete XMLDoc;
174    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
[6352]175  }
[6402]176  /* the whole loading process for the GameWorld */
[6407]177  this->dataTank->loadData(root);
[6352]178
[6402]179  delete XMLDoc;
[6386]180  this->releaseLoadScreen();
[6352]181}
182
183
184/**
[6387]185 *  unload the data of this GameWorld
186 */
187ErrorMessage GameWorld::unloadData()
188{
189  delete this->shell;
190  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
191
[6407]192  this->dataTank->unloadData();
[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  this->lastFrame = SDL_GetTicks ();
250  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
[6352]251
[6402]252  while( this->isRunning) /* @todo implement pause */
253  {
254    ++this->cycle;
255    /* process intput */
256    this->handleInput ();
257    if( !this->isRunning)
258      break;
[6386]259
[6402]260    /* network synchronisation */
261    this->synchronize ();
262    /* process time */
263    this->tick ();
264    /* process collision */
265    this->collide ();
266    /* update the state */
267    this->update ();
268    /* draw everything */
269    this->display ();
270  }
271
272  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
[6352]273}
274
275
276/**
277 *  synchronize local data with remote data
278*/
[6358]279void GameWorld::synchronize ()
[6402]280{}
[6352]281
282
283/**
284 *  run all input processing
285
286   the command node is the central input event dispatcher. the node uses the even-queue from
287   sdl and has its own event-passing-queue.
288*/
[6358]289void GameWorld::handleInput ()
[6352]290{
291  EventHandler::getInstance()->process();
292}
293
[6402]294
295/**
296 *  ticks a WorldEntity list
297 * @param entityList list of the WorldEntities
298 * @param dt time passed since last frame
299 */
[6358]300void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
[6352]301{
[6710]302  std::list<WorldEntity*>::iterator entity, next;
303  next = entityList.begin();
304  while (next != entityList.end())
305  {
306    entity = next++;
[6352]307    (*entity)->tick(dt);
[6710]308  }
[6352]309}
310
311/**
312 *  advance the timeline
[6402]313 *
314 * this calculates the time used to process one frame (with all input handling, drawing, etc)
315 * the time is mesured in ms and passed to all world-entities and other classes that need
316 * a heart-beat.
317 */
[6358]318void GameWorld::tick ()
[6352]319{
320  Uint32 currentFrame = SDL_GetTicks();
[6402]321
[6386]322  if( !this->isPaused)
[6402]323  {
324    this->dt = currentFrame - this->lastFrame;
325
326    /* limit the the frame rate to 100 frames per second (fps) */
327    if( this->dt < 10)
[6352]328    {
[6402]329      /* the frame-rate is limited to 100 frames per second, all other things are for nothing. */
[6412]330      //PRINTF(0)("fps = 1000 - frame rate is adjusted\n");
[6402]331      SDL_Delay(10 - dt);
332      this->dt = 10;
333    }
[6352]334
[6402]335    this->dtS = (float)this->dt / 1000.0f * this->speed;
336    this->gameTime += this->dtS;
[6352]337
[6407]338    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
[6457]339    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
[6407]340    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
341    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
[6433]342    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
[6407]343    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
344    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
[6352]345
[6402]346    /* update tick the rest */
[6407]347    this->dataTank->localCamera->tick(this->dtS);
[6402]348    AnimationPlayer::getInstance()->tick(this->dtS);
349    PhysicsEngine::getInstance()->tick(this->dtS);
[6352]350
[6402]351    GraphicsEngine::getInstance()->tick(this->dtS);
352  }
[6352]353  this->lastFrame = currentFrame;
354}
355
356
357/**
358 *  this function gives the world a consistant state
[6402]359 *
360 * after ticking (updating the world state) this will give a constistant
361 * state to the whole system.
362 */
[6358]363void GameWorld::update()
[6352]364{
365  GraphicsEngine::getInstance()->update(this->dtS);
366  PNode::getNullParent()->updateNode (this->dtS);
367  SoundEngine::getInstance()->update();
368  //music->update();
369}
370
371
[6402]372/**
373 * kicks the CDEngine to detect the collisions between the object groups in the world
374 */
[6358]375void GameWorld::collide()
[6352]376{
[6407]377  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[6433]378  this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
[6407]379  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[6433]380  this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
381
382  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[6407]383      this->dataTank->objectManager->getObjectList(OM_COMMON));
[6352]384}
385
386/**
387 *  render the current frame
[6402]388 *
389 * clear all buffers and draw the world
390 */
[6358]391void GameWorld::display ()
[6352]392{
393  // clear buffer
394  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
395  // set camera
[6407]396  this->dataTank->localCamera->apply ();
[6352]397  // draw world
398  this->draw();
399  // flip buffers
400  GraphicsEngine::swapBuffers();
401}
402
403
404/**
405 *  runs through all entities calling their draw() methods
406 */
[6358]407void GameWorld::draw ()
[6352]408{
409  GraphicsEngine* engine = GraphicsEngine::getInstance();
[6402]410
411   /* to draw the bounding boxes of the objects at level 2 for debug purp */
[6416]412
413  /* debug draw the PNodes */
414
415  /* draw all WorldEntiy groups */
416  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
417  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
418  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
419  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
[6433]420  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
[6416]421  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
422  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
423
[6402]424  if( unlikely( this->showBV))
425  {
426    CDEngine* engine = CDEngine::getInstance();
427    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
428    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
429    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
430    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
431    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
432    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
433  }
[6416]434
[6402]435  if( unlikely(this->showPNodes))
436    PNode::getNullParent()->debugDraw(0);
437
438  /* final draw command */
[6352]439  engine->draw();
440}
441
442
443/**
[6366]444 *  sets the track path of this world
445 * @param name the name of the path
446 */
[6358]447void GameWorld::setPath( const char* name)
[6352]448{
449  if (this->path)
450    delete this->path;
451  if (ResourceManager::isFile(name))
452  {
453    this->path = new char[strlen(name)+1];
454    strcpy(this->path, name);
455  }
456  else
[6402]457  {
458    this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1];
459    sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
460  }
[6352]461}
462
[6387]463
[6402]464/**
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
Note: See TracBrowser for help on using the repository browser.