Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/world.cc @ 5308

Last change on this file since 5308 was 5298, checked in by bensch, 20 years ago

orxonox/trunk: output-issues

File size: 29.1 KB
RevLine 
[1853]1
[4010]2
[4555]3/*
[1853]4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
[1855]12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
[2190]15   co-programmer: Christian Meyer
[1853]16*/
17
[3590]18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
19
[2190]20#include "world.h"
[3608]21
[5205]22#include "shell_command.h"
[3620]23
[4347]24#include "state.h"
25
[3608]26#include "p_node.h"
27#include "null_parent.h"
[4326]28#include "pilot_node.h"
[3608]29#include "track_node.h"
[2190]30#include "world_entity.h"
[2036]31#include "player.h"
[2190]32#include "camera.h"
[2816]33#include "environment.h"
[3419]34#include "skysphere.h"
[3803]35#include "skybox.h"
[3750]36#include "satellite.h"
[4245]37#include "test_entity.h"
[3608]38#include "terrain.h"
[3436]39#include "light.h"
[3790]40#include "text_engine.h"
[4726]41#include "load_param.h"
[5174]42#include "shell.h"
[3620]43
[3646]44#include "track_manager.h"
45#include "garbage_collector.h"
[4940]46#include "fast_factory.h"
[3812]47#include "animation_player.h"
[4176]48#include "particle_engine.h"
[4245]49#include "graphics_engine.h"
[4338]50#include "physics_engine.h"
[4396]51#include "fields.h"
[3646]52
[4488]53#include "md2Model.h"
54
[3608]55#include "glmenu_imagescreen.h"
56#include "list.h"
[4010]57#include "game_loader.h"
[2036]58
[3964]59#include "animation3d.h"
[3608]60
[4010]61#include "substring.h"
[3608]62
[4261]63#include "factory.h"
[4245]64
[4287]65#include "projectile.h"
[4405]66#include "event_handler.h"
[4287]67
[4504]68#include "sound_engine.h"
[4961]69#include "ogg_player.h"
[4504]70
[4747]71#include "class_list.h"
72
[4917]73#include "cd_engine.h"
[4976]74#include "npc.h"
[5266]75#include "npc2.h"
[4820]76
[5205]77SHELL_COMMAND(speed, World, setSpeed);
78
[1856]79using namespace std;
[1853]80
[4978]81//! This creates a Factory to fabricate a World
[4010]82CREATE_FACTORY(World);
[3620]83
[4261]84World::World(const TiXmlElement* root)
[4010]85{
86  this->constuctorInit("", -1);
[4094]87  this->path = NULL;
[4555]88
[4261]89  this->loadParams(root);
[4010]90}
91
[4555]92/**
[4836]93  *  create a new World
[4555]94
[2551]95    This creates a new empty world!
[1858]96*/
[4978]97World::World (const char* name)
[1855]98{
[4094]99  this->path = NULL;
[4010]100  this->constuctorInit(name, -1);
[3573]101  //NullParent* np = NullParent::getInstance();
[1855]102}
103
[3449]104/**
[4836]105 *  creates a new World...
106 * @param worldID with this ID
[3449]107*/
[2636]108World::World (int worldID)
109{
[4094]110  this->path = NULL;
[4010]111  this->constuctorInit(NULL, worldID);
[2636]112}
113
[4555]114/**
[4838]115 *  remove the World from memory
[4555]116
[3365]117    delete everything explicitly, that isn't contained in the parenting tree!
118    things contained in the tree are deleted automaticaly
[4838]119 */
[2190]120World::~World ()
[1872]121{
[5206]122  delete this->shell;
[3546]123  PRINTF(3)("World::~World() - deleting current world\n");
[3677]124
[4978]125  // here everything that is alocated by the World is deleted
[4837]126  delete this->entities;
[4830]127  State::setWorldEntityList(NULL);
128
[5115]129
[5296]130  // delete all the initialized Engines.
[4735]131  delete LightManager::getInstance();
[4822]132  delete TrackManager::getInstance();
133  delete ParticleEngine::getInstance();
[5296]134  delete AnimationPlayer::getInstance();
[4978]135  delete PhysicsEngine::getInstance();
[4822]136
[4978]137  // external engines initialized by the orxonox-class get deleted
[4504]138  SoundEngine::getInstance()->flushAllBuffers();
[4830]139  SoundEngine::getInstance()->flushAllSources();
[4979]140  FastFactory::flushAll(true);
[4504]141
[5115]142
[4978]143  // erease everything that is left.
[4870]144  delete NullParent::getInstance();
[4872]145
[4978]146  // unload the resources !!
[4136]147  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
[5218]148
149  delete[] this->path;
[1872]150}
[1858]151
[3526]152/**
[4978]153 * initializes the world.
154 * @param name the name of the world
155 * @param worldID the ID of this world
156 *
157 * set all stuff here that is world generic and does not use to much memory
158 * because the real init() function StoryEntity::init() will be called
159 * shortly before start of the game.
160 * since all worlds are initiated/referenced before they will be started.
161 * NO LEVEL LOADING HERE - NEVER!
[3526]162*/
[4978]163void World::constuctorInit(const char* name, int worldID)
[3526]164{
[4320]165  this->setClassID(CL_WORLD, "World");
[2636]166
[4978]167  this->setName(name);
[3526]168  this->debugWorldNr = worldID;
[5211]169  this->gameTime = 0.0f;
[5205]170  this->setSpeed(1.0);
[4961]171  this->music = NULL;
[5211]172  this->shell = NULL;
173  this->entities = NULL;
[3629]174}
[3526]175
[4978]176/**
177 * loads the parameters of a World from an XML-element
178 * @param root the XML-element to load from
179 */
[4261]180void World::loadParams(const TiXmlElement* root)
181{
[4600]182  PRINTF(4)("Creating a World\n");
[4261]183
184  LoadParam<World>(root, "identifier", this, &World::setStoryID)
185    .describe("Sets the StoryID of this world");
[4834]186
[4261]187  LoadParam<World>(root, "nextid", this, &World::setNextStoryID)
188    .describe("Sets the ID of the next world");
[4834]189
[4261]190  LoadParam<World>(root, "path", this, &World::setPath)
191    .describe("The Filename of this World (relative from the data-dir)");
192}
193
[3629]194/**
[4978]195 * this is executed just before load
196 *
197 * since the load function sometimes needs data, that has been initialized
198 * before the load and after the proceeding storyentity has finished
[3629]199*/
200ErrorMessage World::preLoad()
201{
[4829]202  State::setWorldEntityList(this->entities = new tList<WorldEntity>());
203  this->cycle = 0;
204
[3620]205  /* init the world interface */
[5206]206  this->shell = new Shell();
[4010]207
[4735]208  LightManager::getInstance();
[4978]209  NullParent::getInstance ();
[3993]210
[4010]211  AnimationPlayer::getInstance(); // initializes the animationPlayer
[4338]212  PhysicsEngine::getInstance();
[4010]213
[4015]214  this->localCamera = new Camera();
[4978]215  this->localCamera->setName ("World-Camera");
[4555]216
[4827]217  State::setCamera(this->localCamera, this->localCamera->getTarget());
[4347]218
[4245]219  GraphicsEngine::getInstance()->displayFPS(true);
[4918]220
221  CDEngine::getInstance()->setEntityList( this->entities);
[3526]222}
223
224
[3449]225/**
[4836]226 *  loads the World by initializing all resources, and set their default values.
[3449]227*/
[3459]228ErrorMessage World::load()
[4555]229{
[4104]230  PRINTF(3)("> Loading world: '%s'\n", getPath());
231  TiXmlElement* element;
[4010]232  GameLoader* loader = GameLoader::getInstance();
[4555]233
[4010]234  if( getPath() == NULL)
[2636]235    {
[4104]236      PRINTF(1)("World has no path specified for loading");
[4324]237      this->loadDebugWorld(this->getStoryID());
[4010]238      return (ErrorMessage){213,"Path not specified","World::load()"};
239    }
[4555]240
[4010]241  TiXmlDocument* XMLDoc = new TiXmlDocument( path);
242  // load the campaign document
[4555]243  if( !XMLDoc->LoadFile())
[4010]244  {
245    // report an error
[4104]246    PRINTF(1)("loading XML File: %s @ %d:%d\n", XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
[4010]247    delete XMLDoc;
248    return (ErrorMessage){213,"XML File parsing error","World::load()"};
249  }
[4555]250
[4010]251  // check basic validity
252  TiXmlElement* root = XMLDoc->RootElement();
253  assert( root != NULL);
[4555]254
[4010]255  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
256    {
257      // report an error
[4104]258      PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
[4010]259      delete XMLDoc;
260      return (ErrorMessage){213,"Path not a WorldDataFile","World::load()"};
261    }
[4555]262
[4010]263  // load the parameters
264  // name
265  const char* string = grabParameter( root, "name");
266  if( string == NULL)
267    {
[4104]268      PRINTF(2)("World is missing a proper 'name'\n");
[5211]269      this->setName("Unknown");
[4010]270    }
271  else
272    {
[5211]273      this->setName(string);
[4010]274    }
[4978]275
[4104]276  ////////////////
277  // LOADSCREEN //
278  ////////////////
279  element = root->FirstChildElement("LoadScreen");
280  if (element == NULL)
281    {
282      PRINTF(2)("no LoadScreen specified, loading default\n");
283
284      glmis->setBackgroundImage("pictures/load_screen.jpg");
285      this->glmis->setMaximum(8);
286      this->glmis->draw();
287    }
288  else
289    {
[4261]290      this->glmis->loadParams(element);
[4104]291      this->glmis->draw();
292    }
293  this->glmis->draw();
[4726]294
295  ////////////////////////
296  // find WorldEntities //
297  ////////////////////////
298
[4104]299  element = root->FirstChildElement("WorldEntities");
[4555]300
[4010]301  if( element == NULL)
302    {
[4104]303      PRINTF(1)("World is missing 'WorldEntities'\n");
[4010]304    }
305  else
306    {
307      element = element->FirstChildElement();
308      // load Players/Objects/Whatever
[4104]309      PRINTF(4)("Loading WorldEntities\n");
[4010]310      while( element != NULL)
[4555]311        {
312          WorldEntity* created = dynamic_cast<WorldEntity*>( loader->fabricate( element));
313          if( created != NULL) this->spawn( created);
314          // if we load a 'Player' we use it as localPlayer
315          //todo do this more elegant
[4919]316          if( element->Value() != NULL && !strcmp( element->Value(), "Player"))
317          {
318            localPlayer = (Player*) created;
319            CDEngine::getInstance()->setPlayer(localPlayer);
320          }
[4555]321          if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) sky = (SkyBox*) created;
[4918]322          if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
323          {
324            terrain = (Terrain*) created;
325            CDEngine::getInstance()->setTerrain(terrain);
326          }
[4555]327          element = element->NextSiblingElement();
[4836]328          glmis->step(); //! @todo temporary
[4555]329        }
[4104]330      PRINTF(4)("Done loading WorldEntities\n");
[4010]331    }
[4555]332
[4726]333    //////////////////////////////
334    // LOADING ADDITIONAL STUFF //
335    //////////////////////////////
336
[4735]337    LoadParam<LightManager>(root, "LightManager", LightManager::getInstance(), &LightManager::loadParams);
338
[4726]339    LoadParam<ParticleEngine>(root, "ParticleEngine", ParticleEngine::getInstance(), &ParticleEngine::loadParams);
[4730]340    LoadParam<PhysicsEngine>(root, "PhysicsEngine", PhysicsEngine::getInstance(), &PhysicsEngine::loadParams);
[4726]341
[4010]342  // find Track
[4222]343  element = root->FirstChildElement( "Track");
[4010]344  if( element == NULL)
345    {
[4228]346      PRINTF(0)("World is missing a 'Track'\n");
[4010]347    }
348  else
[4555]349    {
[4010]350      //load track
[4228]351      PRINTF(4)("Loading Track\n");
[4010]352
[4822]353      TrackManager::getInstance()->loadParams( element);
354      TrackManager::getInstance()->finalize();
[4222]355    }
[4555]356
[4010]357  // free the XML data
[4015]358
[4010]359  delete XMLDoc;
[4015]360  /* GENERIC LOADING PROCESS FINISHED */
[4555]361
[4010]362  // bind input
[4822]363  EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_UP);
364  EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_DOWN);
365  EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_LEFT);
366  EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_RIGHT);
367  EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_FIRE1);
368  EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_NEXT_WEAPON);
369  EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_PREVIOUS_WEAPON);
[4555]370
[4010]371  // bind camera
372  //this->localCamera->bind (localPlayer);
[4969]373 // this->localPlayer->addChild (this->localCamera);
[4245]374
[4555]375
[4822]376  //        TrackManager::getInstance()->setBindSlave(env);
377  PNode* tn = TrackManager::getInstance()->getTrackNode();
[4010]378  tn->addChild(this->localPlayer);
[4555]379
[4010]380  //localCamera->setParent(TrackNode::getInstance());
381  tn->addChild(this->localCamera);
382  localCamera->lookAt(tn);
[4620]383  localCamera->setClipRegion(1, 10000.0);
[4444]384  this->localPlayer->setParentMode(PNODE_ALL);
[4822]385  TrackManager::getInstance()->condition(1, LEFTRIGHT, this->localPlayer);
[4501]386
[4015]387  this->sky->setParent(this->localCamera);
[5258]388  this->sky->setParentMode(PNODE_MOVEMENT);
[3368]389
[4010]390  // initialize debug coord system
391  objectList = glGenLists(1);
392  glNewList (objectList, GL_COMPILE);
[4555]393
[4822]394  //TrackManager::getInstance()->drawGraph(.01);
395  //TrackManager::getInstance()->debug(2);
[4010]396  glEndList();
[3993]397
[4504]398  SoundEngine::getInstance()->setListener(this->localCamera);
[4176]399
[4347]400
[4709]401
[4715]402  ////////////
403  // STATIC //
404  ////////////
405
[4730]406  Gravity* test = new Gravity();
[4715]407
[4709]408  // SYSTEM TRAILING THE PLAYER
[4347]409  // Creating a Test Particle System
[4430]410
[4730]411  //new PhysicsConnection(system, gravity);
[4397]412  //    new PhysicsConnection(this->localPlayer, gravity);
[4347]413
[4721]414//   TestEntity* testEntity = new TestEntity();
415//   testEntity->setRelCoor(Vector(570, 10, -15));
416//   testEntity->setRelDir(Quaternion(M_PI, Vector(0, 1, 0)));
417//   this->spawn(testEntity);
[4397]418
[5294]419//   TestEntity* testEntity2 = new TestEntity();
420//   testEntity2->setAnim(STAND);
421//   testEntity2->setRelCoor(Vector(2400.0, 10.0, -30.0));
422//   testEntity2->setRelDir(Quaternion(M_PI, Vector(0, 1, 0)));
423//   //testEntity2->setParent(this->localPlayer);
424//   this->spawn(testEntity2);
425//
426//   TestEntity* testEntity3 = new TestEntity();
427//   testEntity3->setAnim(BOOM);
428//   testEntity3->setRelCoor(Vector(2450.0, 10.0, -40.0));
429//   testEntity3->setRelDir(Quaternion(M_PI, Vector(0, 1, 0)));
430//   this->spawn(testEntity3);
431//
432//   TestEntity* testEntity4 = new TestEntity();
433//   testEntity4->setAnim(FLIP);
434//   testEntity4->setRelCoor(Vector(2500.0, 10.0, -22.0));
435//   testEntity4->setRelDir(Quaternion(M_PI, Vector(0, 1, 0)));
436//   this->spawn(testEntity4);
437//
438//   TestEntity* testEntity5 = new TestEntity();
439//   testEntity5->setAnim(WAVE);
440//   testEntity5->setRelCoor(Vector(2420.0, 10.0, -50.0));
441//   testEntity5->setRelDir(Quaternion(M_PI, Vector(0, 1, 0)));
442//   this->spawn(testEntity5);
443//
444//   TestEntity* testEntity6 = new TestEntity();
445//   testEntity6->setAnim(WAVE);
446//   testEntity6->setRelCoor(Vector(2420.0, 10.0, -20.0));
447//   testEntity6->setRelDir(Quaternion(M_PI, Vector(0, 1, 0)));
448//   this->spawn(testEntity6);
449//
450//   TestEntity* testEntity7 = new TestEntity();
451//   testEntity7->setAnim(WAVE);
452//   testEntity7->setRelCoor(Vector(2500.0, 10.0, -50.0));
453//   testEntity7->setRelDir(Quaternion(M_PI, Vector(0, 1, 0)));
454//   this->spawn(testEntity7);
[4488]455
456
[4574]457
[5298]458//  PhysicsEngine::getInstance()->debug();
[4721]459
[4978]460
461
[4976]462  for(int i = 0; i < 100; i++)
463  {
[5266]464    WorldEntity* tmp = new NPC2();
[5049]465    char npcChar[10];
466    sprintf (npcChar, "NPC_%d", i);
[5256]467        tmp->setName(npcChar);
[5260]468    tmp->setAbsCoor(((float)rand()/RAND_MAX) * 5000, 50/*+ (float)rand()/RAND_MAX*20*/, ((float)rand()/RAND_MAX -.5) *50);
[4976]469    this->spawn(tmp);
[4747]470
[4976]471
472  }
473
474
475
[5298]476//  ClassList::debug();
[4961]477
[5211]478  this->music = NULL;//(OggPlayer*)ResourceManager::getInstance()->load("sound/00-luke_grey_-_hypermode.ogg", OGG, RP_LEVEL);
479  //music->playback();
[4010]480}
[3365]481
[4245]482
[4324]483
[4326]484/**
[4978]485 * creates a debug world: only for experimental stuff
[4326]486*/
[4010]487void World::loadDebugWorld(int worldID)
488{
489  /*monitor progress*/
490  this->glmis->step();
[4228]491  // stuff beyond this point remains to be loaded properly
[3194]492
[4228]493  // initializing the TrackManager
[4822]494  TrackManager::getInstance()->addPointV(Vector(150, -35, 5));
495  TrackManager::getInstance()->addPointV(Vector(200,-35, 5));
496  TrackManager::getInstance()->addPointV(Vector(250, -35, 5));
497  TrackManager::getInstance()->addPointV(Vector(320,-33,-.55));
498  TrackManager::getInstance()->setDuration(1);
499  TrackManager::getInstance()->setSavePoint();
[4228]500
[4822]501  TrackManager::getInstance()->addPointV(Vector(410, 0, 0));
502  TrackManager::getInstance()->addPointV(Vector(510, 20, -10));
503  TrackManager::getInstance()->addPointV(Vector(550, 20, -10));
504  TrackManager::getInstance()->addPointV(Vector(570, 20, -10));
505  TrackManager::getInstance()->setDuration(2);
[4555]506
[4822]507  TrackManager::getInstance()->forkS("testFork1,testFork2");
508  TrackManager::getInstance()->workOnS("testFork1");
509  TrackManager::getInstance()->addPointV(Vector(640, 25, -30));
510  TrackManager::getInstance()->addPointV(Vector(700, 40, -120));
511  TrackManager::getInstance()->addPointV(Vector(800, 50, -150));
512  TrackManager::getInstance()->addPointV(Vector(900, 60, -100));
513  TrackManager::getInstance()->addPointV(Vector(900, 60, -70));
514  TrackManager::getInstance()->addPointV(Vector(990, 65, -15));
515  TrackManager::getInstance()->addPointV(Vector(1050, 65, -10));
516  TrackManager::getInstance()->addPointV(Vector(1100, 65, -20));
517  TrackManager::getInstance()->setDuration(4);
[4228]518
[4822]519  TrackManager::getInstance()->workOnS("testFork2");
520  TrackManager::getInstance()->addPointV(Vector(640, 25, 20));
521  TrackManager::getInstance()->addPointV(Vector(670, 50, 120));
522  TrackManager::getInstance()->addPointV(Vector(700, 70, 80));
523  TrackManager::getInstance()->addPointV(Vector(800, 70, 65));
524  TrackManager::getInstance()->addPointV(Vector(850, 65, 65));
525  TrackManager::getInstance()->addPointV(Vector(920, 35, 40));
526  TrackManager::getInstance()->addPointV(Vector(945, 40, 40));
527  TrackManager::getInstance()->addPointV(Vector(970, 24, 40));
528  TrackManager::getInstance()->addPointV(Vector(1000, 40, -7));
[4508]529
[4822]530  TrackManager::getInstance()->setDuration(4);
[4555]531
532
[4822]533  TrackManager::getInstance()->joinS("testFork1,testFork2");
[4555]534
[4822]535  TrackManager::getInstance()->addPointV(Vector(1200, 60, -50));
536  TrackManager::getInstance()->addPointV(Vector(1300, 50, -50));
537  TrackManager::getInstance()->addPointV(Vector(1400, 40, -50));
538  TrackManager::getInstance()->addPointV(Vector(1500, 40, -60));
539  TrackManager::getInstance()->addPointV(Vector(1600, 35, -55));
540  TrackManager::getInstance()->addPointV(Vector(1700, 45, -40));
541  TrackManager::getInstance()->addPointV(Vector(1750, 60, -40));
542  TrackManager::getInstance()->addPointV(Vector(1770, 80, -40));
543  TrackManager::getInstance()->addPointV(Vector(1800, 100, -40));
544  TrackManager::getInstance()->setDuration(10);
[4555]545
[4822]546  TrackManager::getInstance()->finalize();
[4228]547
[4555]548
[4010]549  // LIGHT initialisation
[4735]550  LightManager::getInstance()->setAmbientColor(.1,.1,.1);
[4736]551//  LightManager::getInstance()->addLight();
[4735]552  LightManager::getInstance()->debug();
[3368]553
[4010]554  switch(this->debugWorldNr)
555    {
556      /*
[4555]557        this loads the hard-coded debug world. this only for simplicity and will be
558        removed by a reald world-loader, which interprets a world-file.
559        if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and
560        make whatever you want...
[4010]561      */
562    case DEBUG_WORLD_0:
563      {
[4735]564        LightManager::getInstance()->getLight()->setAbsCoor(-5.0, 10.0, -40.0);
[4010]565
566
[4555]567        this->localPlayer = new Player ();
568        this->localPlayer->setName ("player");
569        this->spawn (this->localPlayer);
570        this->localPlayer->setRelCoor(Vector(5,0,0));
571        /*monitor progress*/
572        this->glmis->step();
[4010]573
[4418]574
[4822]575        EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_FIRE1);
576        EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_NEXT_WEAPON);
577        EventHandler::getInstance()->subscribe(this->localPlayer, ES_GAME, KeyMapper::PEV_PREVIOUS_WEAPON);
[4418]578
[4555]579        /*
580        Field* testField = new Gravity();
581        testField->setMagnitude(10);
582        new PhysicsConnection(this->localPlayer, testField);
583        */
[4397]584
[4555]585        // bind camera
586        this->localCamera = new Camera();
587        this->localCamera->setName ("camera");
588        /*monitor progress*/
589        this->glmis->step();
[2816]590
[3419]591
[4555]592        // Create SkySphere
[4621]593        this->sky = new Skysphere("pictures/sky-replace.jpg");
594        this->sky->setName("SkySphere");
595        this->spawn(this->sky);
[4555]596        this->localCamera->addChild(this->sky);
597        this->sky->setParentMode(PNODE_MOVEMENT);
598        /*monitor progress*/
599        this->glmis->step();
[3368]600
[3521]601
[4555]602        terrain = new Terrain("worlds/newGround.obj");
603        terrain->setRelCoor(Vector(0,-10,0));
604        this->spawn(terrain);
605        /*monitor progress*/
606        this->glmis->step();
[2816]607
[4555]608        this->pilotNode = new PilotNode();
609        this->spawn(this->pilotNode);
610        this->pilotNode->setAbsCoor(Vector(150, -35, 5));
611        this->pilotNode->addChild(this->localPlayer);
612        this->pilotNode->addChild(this->localCamera);
613        this->localCamera->lookAt(this->localPlayer);
[4422]614
[4822]615        EventHandler::getInstance()->subscribe(this->pilotNode, ES_GAME, KeyMapper::PEV_UP);
616        EventHandler::getInstance()->subscribe(this->pilotNode, ES_GAME, KeyMapper::PEV_DOWN);
617        EventHandler::getInstance()->subscribe(this->pilotNode, ES_GAME, KeyMapper::PEV_LEFT);
618        EventHandler::getInstance()->subscribe(this->pilotNode, ES_GAME, KeyMapper::PEV_RIGHT);
619        EventHandler::getInstance()->subscribe(this->pilotNode, ES_GAME, EV_MOUSE_MOTION);
[4422]620
[4555]621        /*
[4822]622        PNode* tn = TrackManager::getInstance()->getTrackNode();
[4555]623        tn->addChild(this->localPlayer);
624        this->localCamera->lookAt(tn);
625
626        tn->addChild(this->localCamera);
627        this->localPlayer->setParentMode(PNODE_ALL);
[4822]628        TrackManager::getInstance()->condition(2, LEFTRIGHT, this->localPlayer);
[4555]629        */
630        this->glmis->step();
631        break;
[4010]632      }
633    case DEBUG_WORLD_1:
634      {
[3365]635
[4555]636        break;
[4010]637      }
638    case DEBUG_WORLD_2:
639      {
[3727]640
[4555]641        break;
[4010]642      }
643    default:
[4324]644      break;
[2636]645    }
[4010]646}
[2636]647
[3459]648/**
[4836]649 *  initializes a new World shortly before start
[4978]650 *
651 * this is the function, that will be loaded shortly before the world is
652 * started
[3459]653*/
654ErrorMessage World::init()
655{
656  this->bPause = false;
[4326]657  this->pilotNode = NULL;
[5051]658
659  /* update the object position before game start - so there are no wrong coordinates used in the first processing */
660  NullParent::getInstance()->update (0.001f);
661  NullParent::getInstance()->update (0.001f);
[5084]662
[3459]663}
664
665
666/**
[4836]667 *  starts the World
[3459]668*/
669ErrorMessage World::start()
670{
[3546]671  PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr);
[3459]672  this->bQuitOrxonox = false;
673  this->bQuitCurrentGame = false;
674  this->mainLoop();
675}
676
677/**
[4836]678 *  stops the world.
[3459]679
680   This happens, when the player decides to end the Level.
681*/
682ErrorMessage World::stop()
683{
[3546]684  PRINTF(3)("World::stop() - got stop signal\n");
[3459]685  this->bQuitCurrentGame = true;
686}
687
688/**
[4836]689 *  pauses the Game
[3459]690*/
691ErrorMessage World::pause()
692{
693  this->isPaused = true;
694}
695
696/**
[4836]697 *  ends the pause Phase
[3459]698*/
699ErrorMessage World::resume()
700{
701  this->isPaused = false;
702}
703
704/**
[4836]705 *  destroys the World
[3459]706*/
707ErrorMessage World::destroy()
708{
[3566]709
[3459]710}
711
712/**
[4836]713 *  shows the loading screen
[3459]714*/
715void World::displayLoadScreen ()
716{
[4555]717  PRINTF(3)("World::displayLoadScreen - start\n");
718
719  //GLMenuImageScreen*
[4099]720  this->glmis = new GLMenuImageScreen();
[3675]721  this->glmis->setMaximum(8);
[4555]722
723  PRINTF(3)("World::displayLoadScreen - end\n");
[3459]724}
725
726/**
[4836]727 *  removes the loadscreen, and changes over to the game
[3459]728
[4836]729   @todo take out the delay
[3459]730*/
731void World::releaseLoadScreen ()
732{
[4555]733  PRINTF(3)("World::releaseLoadScreen - start\n");
[3459]734  this->glmis->setValue(this->glmis->getMaximum());
[4555]735  PRINTF(3)("World::releaseLoadScreen - end\n");
[4099]736  delete this->glmis;
[3459]737}
738
739
[3620]740/**
[4836]741 *  gets the list of entities from the world
742 * @returns entity list
[3620]743*/
744tList<WorldEntity>* World::getEntities()
745{
746  return this->entities;
747}
748
749
[3646]750/**
[4836]751 *  this returns the current game time
752 * @returns elapsed game time
[3646]753*/
754double World::getGameTime()
755{
756  return this->gameTime;
757}
758
759
[4555]760/**
[4836]761 *  function to put your own debug stuff into it. it can display informations about
[3225]762   the current class/procedure
763*/
[2640]764void World::debug()
765{
[5115]766  PRINTF(0)("Printing out the List of alive WorldEntities:\n");
767  tIterator<WorldEntity>* iterator = this->entities->getIterator();
768  WorldEntity* entity = iterator->firstElement();
769  while( entity != NULL)
770  {
771    PRINTF(0)("%s::%s\n", entity->getClassName(), entity->getName());
772    entity = iterator->nextElement();
773  }
774  delete iterator;
[2640]775}
[2636]776
[2640]777
[3449]778/**
[3225]779  \brief main loop of the world: executing all world relevant function
780
781  in this loop we synchronize (if networked), handle input events, give the heart-beat to
782  all other member-entities of the world (tick to player, enemies etc.), checking for
783  collisions drawing everything to the screen.
784*/
[2636]785void World::mainLoop()
786{
[3365]787  this->lastFrame = SDL_GetTicks ();
[3546]788  PRINTF(3)("World::mainLoop() - Entering main loop\n");
[5045]789
[4836]790  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* @todo implement pause */
[2551]791    {
[4558]792      ++this->cycle;
[5048]793      PRINTF(4)("World::mainloop() - number of entities: %i\n", this->entities->getSize());
[2636]794      // Network
[3365]795      this->synchronize ();
[2636]796      // Process input
[3365]797      this->handleInput ();
[3215]798      if( this->bQuitCurrentGame || this->bQuitOrxonox)
[4555]799          break;
[2636]800      // Process time
[3551]801      this->tick ();
[5045]802      // Process collision
803      this->collide ();
[3551]804      // Update the state
[4555]805      this->update ();
[2636]806      // Draw
[3365]807      this->display ();
[5045]808    }
[3548]809
[3546]810  PRINTF(3)("World::mainLoop() - Exiting the main loop\n");
[1899]811}
812
[3459]813
[2190]814/**
[4836]815 *  synchronize local data with remote data
[1855]816*/
[2636]817void World::synchronize ()
[1855]818{
[2636]819  // Get remote input
820  // Update synchronizables
[1855]821}
[2636]822
[3459]823
[2636]824/**
[4836]825 *  run all input processing
[3225]826
827   the command node is the central input event dispatcher. the node uses the even-queue from
828   sdl and has its own event-passing-queue.
[2636]829*/
[3225]830void World::handleInput ()
[2636]831{
832  // localinput
[4407]833  //CommandNode* cn = Orxonox::getInstance()->getLocalInput();
834  //cn->process();
835
836  EventHandler::getInstance()->process();
837
[2636]838  // remoteinput
839}
840
[3459]841
[2636]842/**
[4836]843 *  advance the timeline
[3225]844
845   this calculates the time used to process one frame (with all input handling, drawing, etc)
846   the time is mesured in ms and passed to all world-entities and other classes that need
847   a heart-beat.
[2636]848*/
[3551]849void World::tick ()
[2636]850{
851  Uint32 currentFrame = SDL_GetTicks();
852  if(!this->bPause)
853    {
[3644]854      this->dt = currentFrame - this->lastFrame;
[4555]855
[4610]856      if( this->dt > 10)
[4555]857        {
858          float fps = 1000/dt;
[3790]859
[4555]860          // temporary, only for showing how fast the text-engine is
861          char tmpChar[20];
862          sprintf(tmpChar, "fps: %4.0f", fps);
863        }
[2636]864      else
[4555]865        {
866          /* the frame-rate is limited to 100 frames per second, all other things are for
867             nothing.
868          */
[5048]869          PRINTF(3)("fps = 1000 - frame rate is adjusted\n");
[4610]870          SDL_Delay(10-dt);
[4555]871          this->dt = 10;
872        }
873
[5205]874      this->dtS = (float)this->dt / 1000.0 * this->speed;
[4145]875      this->gameTime += this->dtS;
[4833]876
[3654]877      tIterator<WorldEntity>* iterator = this->entities->getIterator();
[5115]878      WorldEntity* entity = iterator->firstElement();
[4555]879      while( entity != NULL)
880        {
881          entity->tick (this->dtS);
882          entity = iterator->nextElement();
883        }
[3654]884      delete iterator;
[4010]885
[3459]886      /* update tick the rest */
[4959]887      TrackManager::getInstance()->tick(this->dtS);
[4832]888      this->localCamera->tick(this->dtS);
[4558]889      // tick the engines
[4245]890      AnimationPlayer::getInstance()->tick(this->dtS);
[4979]891//      if (this->cycle > 5)
[4558]892        PhysicsEngine::getInstance()->tick(this->dtS);
[4396]893
[4558]894      ParticleEngine::getInstance()->tick(this->dtS);
895      GarbageCollector::getInstance()->tick(this->dtS);
[4396]896
[4831]897
[4558]898      /** actualy the Graphics Engine should tick the world not the other way around...
[4555]899         but since we like the things not too complicated we got it this way around
900         until there is need or time to do it the other way around.
[4836]901         @todo: GraphicsEngine ticks world: separation of processes and data...
[4681]902
903        bensch: in my opinion the GraphicsEngine could draw the world, but not tick it,
904         beceause graphics have nothing(or at least not much) to do with Motion.
[4245]905      */
906      GraphicsEngine::getInstance()->tick(this->dtS);
[2636]907    }
908  this->lastFrame = currentFrame;
909}
910
[3216]911
[2636]912/**
[4836]913 *  this function gives the world a consistant state
[3551]914
915   after ticking (updating the world state) this will give a constistant
916   state to the whole system.
917*/
918void World::update()
919{
[4822]920  GarbageCollector::getInstance()->update();
[4978]921  NullParent::getInstance()->update (this->dtS);
[5084]922  GraphicsEngine::getInstance()->update(this->dtS);
[4504]923
924  SoundEngine::getInstance()->update();
[4978]925  //music->update();
[3551]926}
927
928
[4917]929void World::collide()
930{
[4918]931  CDEngine::getInstance()->checkCollisions();
[4917]932}
933
[3551]934/**
[4836]935 *  render the current frame
[4555]936
[3225]937   clear all buffers and draw the world
[2636]938*/
939void World::display ()
940{
941  // clear buffer
942  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
943  // set camera
944  this->localCamera->apply ();
945  // draw world
946  this->draw();
947  // draw HUD
[4837]948  /** @todo draw HUD */
[2636]949  // flip buffers
[4681]950  GraphicsEngine::swapBuffers();
[3365]951  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
952  //SDL_Flip (screen);
[2636]953}
954
[2644]955
[3225]956/**
[4917]957 *  runs through all entities calling their draw() methods
958 */
959void World::draw ()
960{
961  /* draw entities */
962  WorldEntity* entity;
963  glLoadIdentity();
964  tIterator<WorldEntity>* iterator = this->entities->getIterator();
[5115]965  entity = iterator->firstElement();
[4917]966  while( entity != NULL )
967  {
[5055]968    if( entity->isVisible() ) entity->draw();
969    //entity->drawBVTree(2, 226);  // to draw the bounding boxes of the objects at level 2 for debug purp
[4917]970    entity = iterator->nextElement();
971  }
972  delete iterator;
973
974  glCallList (objectList);
975
976  ParticleEngine::getInstance()->draw();
977
978  GraphicsEngine::getInstance()->draw();
979  //TextEngine::getInstance()->draw();
980}
981
982/**
[4836]983 *  add and spawn a new entity to this world
984 * @param entity to be added
[3225]985*/
[2644]986void World::spawn(WorldEntity* entity)
987{
[3365]988  this->entities->add (entity);
[3233]989  entity->postSpawn ();
[2816]990}
991
992
[3225]993/**
[4836]994 *  add and spawn a new entity to this world
995 * @param entity to be added
996 * @param absCoor At what coordinates to add this entity.
997 * @param absDir In which direction should it look.
[3225]998*/
[3365]999void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
[2816]1000{
[3529]1001  this->entities->add (entity);
1002
[3809]1003  entity->setAbsCoor (*absCoor);
1004  entity->setAbsDir (*absDir);
[3365]1005
[3233]1006  entity->postSpawn ();
[2644]1007}
[2816]1008
1009
[3521]1010/**
[4836]1011 *  add and spawn a new entity to this world
1012 * @param entity to be added
1013 * @param entity to be added to (PNode)
1014 * @param At what relative  coordinates to add this entity.
1015 * @param In which relative direction should it look.
[3521]1016*/
[4555]1017void World::spawn(WorldEntity* entity, PNode* parentNode,
[4765]1018                  Vector* relCoor, Quaternion* relDir)
[3521]1019{
[4978]1020  NullParent::getInstance();
[3529]1021  if( parentNode != NULL)
[3521]1022    {
1023      parentNode->addChild (entity);
[4555]1024
[3809]1025      entity->setRelCoor (*relCoor);
1026      entity->setRelDir (*relDir);
[4555]1027
[3521]1028      this->entities->add (entity);
[4555]1029
[3521]1030      entity->postSpawn ();
1031    }
1032}
1033
1034
1035
[3449]1036/**
[3225]1037  \brief commands that the world must catch
[4836]1038  @returns false if not used by the world
[3225]1039*/
[3216]1040bool World::command(Command* cmd)
1041{
[4091]1042  if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW0)) this->localCamera->setViewMode(VIEW_NORMAL);
1043  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW1)) this->localCamera->setViewMode(VIEW_BEHIND);
1044  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW2)) this->localCamera->setViewMode(VIEW_FRONT);
1045  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW3)) this->localCamera->setViewMode(VIEW_LEFT);
1046  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW4)) this->localCamera->setViewMode(VIEW_RIGHT);
1047  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW5)) this->localCamera->setViewMode(VIEW_TOP);
[3216]1048  return false;
1049}
[3365]1050
[4010]1051void World::setPath( const char* name)
1052{
[4094]1053  if (this->path)
1054    delete this->path;
1055  if (ResourceManager::isFile(name))
1056  {
1057    this->path = new char[strlen(name)+1];
1058    strcpy(this->path, name);
1059  }
1060  else
1061    {
1062      this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1];
1063      sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
1064    }
[4010]1065}
1066
1067const char* World::getPath( void)
1068{
1069  return path;
1070}
Note: See TracBrowser for help on using the repository browser.