Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7692 was 7677, checked in by bensch, 19 years ago

orxonox/trunk: POD's

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