Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7771 was 7739, checked in by patrick, 19 years ago

trunk: tried to add the command for bvvisibility toggling with depth level. execution results in segfault :(

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