Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: renamed Array to tArray

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