Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some renamings, and GameWorldData now has lists for what EntityLists should be drawed/ticked/collided(not yet implemented).

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