Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/story_entities/game_world.cc @ 6404

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

network: working on the last steps, completion is in reach. sadly the world isn't loaded anymore at the moment. continue work later. work flush

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