Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7391 was 7391, checked in by patrick, 18 years ago

orxonox: working on mission goals and mission manager

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