Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

lighting war

File size: 13.0 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
[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  this->cycle = 0;
127  /* init the world interface */
128  this->shell = new Shell();
129
[6498]130  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
[6407]131  this->dataTank->init();
[6352]132}
133
134
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
[6992]148  if( getLoadFile() == NULL)
[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
[6992]154  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
[6402]155  // load the xml world file for further loading
[6352]156  if( !XMLDoc->LoadFile())
157  {
[6992]158    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
[6352]159    delete XMLDoc;
[6358]160    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
[6352]161  }
162  // check basic validity
163  TiXmlElement* root = XMLDoc->RootElement();
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");
169    delete XMLDoc;
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
[6402]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{
186  delete this->shell;
187  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
188
[6981]189  this->dataTank->unloadData();
190
[6988]191  State::setCurrentStoryEntity(NULL);
[6845]192  if (this->dataXML)
193    delete this->dataXML;
[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
[6409]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{
[6634]246  /* start the music */
247  if(this->dataTank->music != NULL)
248    this->dataTank->music->playback();
249
[6402]250  this->lastFrame = SDL_GetTicks ();
251  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
[6352]252
[6402]253  while( this->isRunning) /* @todo implement pause */
254  {
255    ++this->cycle;
256    /* process intput */
257    this->handleInput ();
258    if( !this->isRunning)
259      break;
[6386]260
[6402]261    /* network synchronisation */
262    this->synchronize ();
263    /* process time */
264    this->tick ();
265    /* process collision */
266    this->collide ();
267    /* update the state */
268    this->update ();
269    /* draw everything */
270    this->display ();
271  }
272
273  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
[6352]274}
275
276
277/**
278 *  synchronize local data with remote data
279*/
[6358]280void GameWorld::synchronize ()
[6402]281{}
[6352]282
283
284/**
285 *  run all input processing
286
287   the command node is the central input event dispatcher. the node uses the even-queue from
288   sdl and has its own event-passing-queue.
289*/
[6358]290void GameWorld::handleInput ()
[6352]291{
292  EventHandler::getInstance()->process();
293}
294
[6402]295
296/**
297 *  ticks a WorldEntity list
298 * @param entityList list of the WorldEntities
299 * @param dt time passed since last frame
300 */
[6358]301void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
[6352]302{
[6710]303  std::list<WorldEntity*>::iterator entity, next;
304  next = entityList.begin();
305  while (next != entityList.end())
306  {
307    entity = next++;
[6352]308    (*entity)->tick(dt);
[6710]309  }
[6352]310}
311
312/**
313 *  advance the timeline
[6402]314 *
315 * this calculates the time used to process one frame (with all input handling, drawing, etc)
316 * the time is mesured in ms and passed to all world-entities and other classes that need
317 * a heart-beat.
318 */
[6358]319void GameWorld::tick ()
[6352]320{
321  Uint32 currentFrame = SDL_GetTicks();
[6402]322
[6386]323  if( !this->isPaused)
[6402]324  {
325    this->dt = currentFrame - this->lastFrame;
326
327    /* limit the the frame rate to 100 frames per second (fps) */
328    if( this->dt < 10)
[6352]329    {
[6402]330      /* the frame-rate is limited to 100 frames per second, all other things are for nothing. */
[6412]331      //PRINTF(0)("fps = 1000 - frame rate is adjusted\n");
[6402]332      SDL_Delay(10 - dt);
333      this->dt = 10;
334    }
[6352]335
[6402]336    this->dtS = (float)this->dt / 1000.0f * this->speed;
337    this->gameTime += this->dtS;
[6352]338
[6407]339    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
[6457]340    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
[6407]341    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
342    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
[6433]343    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
[6407]344    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
345    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
[6352]346
[6402]347    /* update tick the rest */
[6407]348    this->dataTank->localCamera->tick(this->dtS);
[6402]349    AnimationPlayer::getInstance()->tick(this->dtS);
350    PhysicsEngine::getInstance()->tick(this->dtS);
[6352]351
[6402]352    GraphicsEngine::getInstance()->tick(this->dtS);
[7035]353
354    if( likely(this->dataTank->gameRule != NULL))
355      this->dataTank->gameRule->tick(this->dtS);
[6402]356  }
[6352]357  this->lastFrame = currentFrame;
358}
359
360
361/**
362 *  this function gives the world a consistant state
[6402]363 *
364 * after ticking (updating the world state) this will give a constistant
365 * state to the whole system.
366 */
[6358]367void GameWorld::update()
[6352]368{
369  GraphicsEngine::getInstance()->update(this->dtS);
370  PNode::getNullParent()->updateNode (this->dtS);
371  SoundEngine::getInstance()->update();
[6827]372
373  if (this->dataTank->music != NULL)
374    this->dataTank->music->update();
[6352]375}
376
377
[6402]378/**
379 * kicks the CDEngine to detect the collisions between the object groups in the world
380 */
[6358]381void GameWorld::collide()
[6352]382{
[6407]383  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7004]384      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
[6407]385  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[7004]386      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
[7078]387  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
388  this->dataTank->objectManager->getObjectList(OM_GROUP_01));
[6433]389
[7083]390  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
391      this->dataTank->objectManager->getObjectList(OM_COMMON));
[6433]392  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[6407]393      this->dataTank->objectManager->getObjectList(OM_COMMON));
[7083]394
[6352]395}
396
397/**
398 *  render the current frame
[6402]399 *
400 * clear all buffers and draw the world
401 */
[6358]402void GameWorld::display ()
[6352]403{
404  // clear buffer
405  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
406  // draw world
407  this->draw();
408  // flip buffers
409  GraphicsEngine::swapBuffers();
410}
411
412
413/**
414 *  runs through all entities calling their draw() methods
415 */
[6358]416void GameWorld::draw ()
[6352]417{
418  GraphicsEngine* engine = GraphicsEngine::getInstance();
[6402]419
[6780]420  // set camera
421  this->dataTank->localCamera->apply ();
[7108]422  this->dataTank->localCamera->project ();
423  LightManager::getInstance()->draw();
[6416]424
425  /* draw all WorldEntiy groups */
426  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
427  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
428  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
429  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
[6433]430  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
[6416]431  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
432  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
433
[6780]434
[6402]435  if( unlikely( this->showBV))
436  {
437    CDEngine* engine = CDEngine::getInstance();
438    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
439    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
440    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
441    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
442    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
443    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
444  }
[6416]445
[6402]446  if( unlikely(this->showPNodes))
447    PNode::getNullParent()->debugDraw(0);
448
[6780]449  engine->draw();
[7035]450
[7108]451
[7035]452  // draw the game ruls
453  if( likely(this->dataTank->gameRule != NULL))
454    this->dataTank->gameRule->draw();
[6352]455}
456
457/**
[6402]458 *  shows the loading screen
459 */
460void GameWorld::displayLoadScreen ()
[6387]461{
[6402]462  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
[6407]463  this->dataTank->glmis = new GLMenuImageScreen();
464  this->dataTank->glmis->setMaximum(8);
[6402]465  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
[6387]466}
467
468
[6402]469/**
470 *  removes the loadscreen, and changes over to the game
471 */
472void GameWorld::releaseLoadScreen ()
[6387]473{
[6402]474  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
[6407]475  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
[6402]476  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
[6387]477}
478
[7004]479
480
481/**
482 * @brief toggles the PNode visibility in the world (drawn as boxes)
483 */
484void GameWorld::togglePNodeVisibility()
485{
486  if (State::getCurrentStoryEntity() != NULL &&
487      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
488  {
489    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes;
490  }
491  else
492    PRINTF(2)("The Current Story entity is not a GameWorld\n");
493};
494
495
496/**
497 * @brief toggles the bounding volume (BV) visibility
498*/
499void GameWorld::toggleBVVisibility()
500{
501  if (State::getCurrentStoryEntity() != NULL &&
502      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
503  {
504    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV;
505  }
506  else
507    PRINTF(2)("The Current Story entity is not a GameWorld\n");
508};
509
Note: See TracBrowser for help on using the repository browser.