Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7379 was 7374, checked in by bensch, 19 years ago

orxonox/trunk: added new Files shell_completion_plugin for the new Plugin Structure.
Also created the first namespace: OrxShell

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