Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5401 was 5401, checked in by bensch, 19 years ago

orxonox/trunk: new Layering system works, now i have to do the check on reparenting, so no lower layers are children of high-layer-parents

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