Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: glmis-refitted

File size: 30.9 KB
Line 
1
2
3/*
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.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: Christian Meyer
16*/
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
19
20#include "world.h"
21
22#include "orxonox.h"
23
24#include "p_node.h"
25#include "null_parent.h"
26#include "helper_parent.h"
27#include "track_node.h"
28#include "world_entity.h"
29#include "player.h"
30#include "camera.h"
31#include "environment.h"
32#include "skysphere.h"
33#include "skybox.h"
34#include "satellite.h"
35#include "terrain.h"
36#include "light.h"
37#include "text_engine.h"
38
39#include "track_manager.h"
40#include "garbage_collector.h"
41#include "animation_player.h"
42
43#include "command_node.h"
44#include "glmenu_imagescreen.h"
45#include "list.h"
46#include "game_loader.h"
47
48#include "animation3d.h"
49
50#include "substring.h"
51
52using namespace std;
53
54WorldInterface* WorldInterface::singletonRef = 0;
55
56
57/**
58   \brief private constructor because of singleton
59*/
60WorldInterface::WorldInterface()
61{
62  this->worldIsInitialized = false;
63  this->worldReference = NULL;
64}
65
66/**
67   \brief public deconstructor
68*/
69WorldInterface::~WorldInterface()
70{
71  this->singletonRef = NULL;
72  this->worldIsInitialized = false;
73  this->worldReference = NULL;
74}
75
76/**
77   \brief gets the singleton instance
78   \returns singleton instance
79*/
80WorldInterface* WorldInterface::getInstance()
81{
82  if( singletonRef == NULL)
83    singletonRef = new WorldInterface();
84  return singletonRef;
85}
86
87
88/**
89   \brief initializes the interface
90   \param reference to the world
91
92   if the worldinterface is not initilizes, there wont be any
93   useable interface
94*/
95void WorldInterface::init(World* world)
96{
97  this->worldReference = world;
98  if( world != NULL)
99    {
100      this->worldIsInitialized = true;
101      PRINTF(3)("WorldInterface up and running\n");
102    }
103}
104
105
106/**
107   \brief gets the entity list from the world
108   \return entity list
109*/
110tList<WorldEntity>* WorldInterface::getEntityList()
111{
112  if( this->worldIsInitialized)
113    return this->worldReference->getEntities();
114  PRINT(1)("Someone tried to use the WorldInterface before it has been initizlized! this can result in SEGFAULTs!\n");
115  return NULL;
116}
117
118CREATE_FACTORY(World);
119
120World::World( TiXmlElement* root)
121{
122  this->constuctorInit("", -1);
123  this->path = NULL;
124  const char *string;
125  char *name;
126  int id;
127 
128  PRINTF0("Creating a World\n");
129 
130  // identifier
131  string = grabParameter( root, "identifier");
132  if( string == NULL || sscanf(string, "%d", &id) != 1)
133    {
134      PRINTF0("World is missing a proper 'identifier'\n");
135      this->setStoryID( -1);
136    }
137  else setStoryID( id);
138
139  // next id
140  string = grabParameter( root, "nextid");
141  if( string == NULL || sscanf(string, "%d", &id) != 1)
142    {
143      PRINTF0("World is missing a proper 'nextid'\n");
144      this->setStoryID( -1);
145    }
146  else setNextStoryID( id);
147 
148
149  // path
150  string = grabParameter( root, "path");
151  if( string == NULL)
152    {
153      PRINTF0("World is missing a proper 'path'\n");
154      this->setPath( NULL);
155    }
156  else
157    {
158      name = new char[strlen(string + 2)];
159      strcpy( name, string);
160      this->setPath( name);
161    }
162}
163
164/**
165    \brief create a new World
166   
167    This creates a new empty world!
168*/
169World::World (char* name)
170{
171  this->path = NULL;
172  this->constuctorInit(name, -1);
173  //NullParent* np = NullParent::getInstance();
174}
175
176/**
177   \brief creates a new World...
178   \param worldID with this ID
179*/
180World::World (int worldID)
181{
182  this->path = NULL;
183  this->constuctorInit(NULL, worldID);
184}
185
186/**
187    \brief remove the World from memory
188   
189    delete everything explicitly, that isn't contained in the parenting tree!
190    things contained in the tree are deleted automaticaly
191*/
192World::~World ()
193{
194  PRINTF(3)("World::~World() - deleting current world\n");
195  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
196  cn->unbind(this->localPlayer);
197  cn->reset();
198
199  ResourceManager::getInstance()->debug();
200  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
201  ResourceManager::getInstance()->debug();
202
203  delete WorldInterface::getInstance();
204
205  delete this->nullParent;
206  delete this->entities;
207  delete this->lightMan;
208  delete this->trackManager;
209  TextEngine::getInstance()->flush();
210
211  AnimationPlayer::getInstance()->debug();
212  delete AnimationPlayer::getInstance(); // this should be at the end of the unloading sequence.
213  //delete garbagecollecor
214  //delete animator
215
216
217}
218
219/**
220   \brief initializes the world.
221
222   set all stuff here that is world generic and does not use to much memory
223   because the real init() function StoryEntity::init() will be called
224   shortly before start of the game. 
225   since all worlds are initiated/referenced before they will be started.
226   NO LEVEL LOADING HERE - NEVER!
227*/
228void World::constuctorInit(char* name, int worldID)
229{
230  this->setClassName ("World");
231
232  //this->worldName = name;
233  //this->worldName = new char[strlen(name)+1];
234  //strcpy(this->worldName, name);
235  this->debugWorldNr = worldID;
236  this->entities = new tList<WorldEntity>();
237}
238
239
240/**
241   \brief this is executed before load
242
243   since the load function sometimes needs data, that has been init before
244   the load and after the proceeding storyentity has finished
245*/
246ErrorMessage World::preLoad()
247{
248  /* init the world interface */
249  WorldInterface* wi = WorldInterface::getInstance();
250  wi->init(this);
251  this->garbageCollector = GarbageCollector::getInstance();
252
253  this->trackManager = TrackManager::getInstance();
254  this->lightMan = LightManager::getInstance();
255  this->nullParent = NullParent::getInstance ();
256  this->nullParent->setName ("NullParent");
257
258  AnimationPlayer::getInstance(); // initializes the animationPlayer
259
260  this->localCamera = new Camera();
261  this->localCamera->setName ("camera");
262}
263
264
265/**
266   \brief loads the World by initializing all resources, and set their default values.
267*/
268ErrorMessage World::load()
269{       
270  PRINTF(0)("> Loading world: '%s'\n", getPath());
271 
272  GameLoader* loader = GameLoader::getInstance();
273 
274  if( getPath() == NULL)
275    {
276      PRINTF0("World has no path specified for loading");
277      return (ErrorMessage){213,"Path not specified","World::load()"};
278    }
279 
280  TiXmlDocument* XMLDoc = new TiXmlDocument( path);
281  // load the campaign document
282  if( !XMLDoc->LoadFile())
283    //this->glmis->step();
284 
285  {
286    // report an error
287    PRINTF0("Error loading XML File: %s @ %d:%d\n", XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
288    delete XMLDoc;
289    return (ErrorMessage){213,"XML File parsing error","World::load()"};
290  }
291 
292  // check basic validity
293  TiXmlElement* root = XMLDoc->RootElement();
294  assert( root != NULL);
295 
296  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
297    {
298      // report an error
299      PRINTF0("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
300      delete XMLDoc;
301      return (ErrorMessage){213,"Path not a WorldDataFile","World::load()"};
302    }
303 
304  // load the parameters
305  // name
306  char* temp;
307  const char* string = grabParameter( root, "name");
308  if( string == NULL)
309    {
310      PRINTF0("World is missing a proper 'name'\n");
311      string = "Unknown";
312      temp = new char[strlen(string + 2)];
313      strcpy( temp, string);
314      this->worldName = temp;
315    }
316  else
317    {
318      temp = new char[strlen(string + 2)];
319      strcpy( temp, string);
320      this->worldName = temp;
321    }
322 
323 
324  // find WorldEntities
325  TiXmlElement* element = root->FirstChildElement( "WorldEntities");
326 
327  if( element == NULL)
328    {
329      PRINTF0("World is missing 'WorldEntities'\n");
330    }
331  else
332    {
333      element = element->FirstChildElement();
334      // load Players/Objects/Whatever
335      PRINTF0("Loading WorldEntities\n");
336      while( element != NULL)
337        {
338          WorldEntity* created = (WorldEntity*) loader->fabricate( element);
339          if( created != NULL) this->spawn( created);
340          // if we load a 'Player' we use it as localPlayer
341          //todo do this more elegant
342          if( element->Value() != NULL && !strcmp( element->Value(), "Player")) localPlayer = (Player*) created;
343          if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) sky = (SkyBox*) created;
344          element = element->NextSiblingElement();
345          glmis->step(); //! \todo temporary
346        }
347      PRINTF0("Done loading WorldEntities\n");
348    }
349 
350  // find Track
351  /*element = root->FirstChildElement( "Track");
352  if( element == NULL)
353    {
354      PRINTF0("============>>>>>>>>>>>>>>>>>World is missing a 'Track'\n");
355    }
356  else
357    {   
358      //load track
359      PRINTF0("============>>>>>>>>>>>>>>>>Loading Track\n");
360
361      trackManager->loadTrack( element);
362      trackManager->finalize();
363      PRINTF0("============>>>>>>>>>>>>>>>>Done loading Track\n");
364    }*/
365 
366  // free the XML data
367
368  delete XMLDoc;
369  /* GENERIC LOADING PROCESS FINISHED */
370 
371  // bind input
372  Orxonox *orx = Orxonox::getInstance ();
373  orx->getLocalInput()->bind (localPlayer);
374 
375  // bind camera
376  //this->localCamera->bind (localPlayer);
377  this->localPlayer->addChild (this->localCamera);
378 
379 
380  // stuff beyond this point remains to be loaded properly
381 
382      // initializing the TrackManager
383  this->trackManager = TrackManager::getInstance();
384      //trackManager->addPoint(Vector(0,0,0));
385      trackManager->addPoint(Vector(150, -35, 5));
386      trackManager->addPoint(Vector(200,-35, 5));
387      trackManager->addPoint(Vector(250, -35, 5));
388      trackManager->addPoint(Vector(320,-33,-.55));
389      trackManager->setDuration(2);
390      trackManager->setSavePoint();
391
392      trackManager->addPoint(Vector(410, 0, 0));
393      trackManager->addPoint(Vector(510, 20, -10));
394      trackManager->addPoint(Vector(550, 20, -10));
395      trackManager->addPoint(Vector(570, 20, -10));
396      trackManager->setDuration(5);
397     
398      int fork11, fork12;
399      trackManager->fork(2, &fork11, &fork12);
400      trackManager->workOn(fork11);
401      trackManager->addPoint(Vector(640, 25, -30));
402      trackManager->addPoint(Vector(700, 40, -120));
403      trackManager->addPoint(Vector(800, 50, -150));
404      trackManager->addPoint(Vector(900, 60, -100));
405      trackManager->addPoint(Vector(900, 60, -70));
406      trackManager->addPoint(Vector(990, 65, -15));
407      trackManager->addPoint(Vector(1050, 65, -10));
408      trackManager->addPoint(Vector(1100, 65, -20));
409      trackManager->setDuration(10);
410
411      trackManager->workOn(fork12);
412      trackManager->addPoint(Vector(640, 25, 20));
413      trackManager->addPoint(Vector(670, 50, 120));
414      trackManager->addPoint(Vector(700, 70, 80));
415      trackManager->addPoint(Vector(800, 70, 65));
416      trackManager->addPoint(Vector(850, 65, 65));
417      trackManager->addPoint(Vector(920, 35, 40));
418      trackManager->addPoint(Vector(945, 40, 40));
419      trackManager->addPoint(Vector(970, 24, 40));
420      trackManager->addPoint(Vector(1000, 40, -7));
421      trackManager->setDuration(10);
422     
423
424      trackManager->join(2, fork11, fork12);
425
426      trackManager->workOn(5);
427      trackManager->addPoint(Vector(1200, 60, -50));
428      trackManager->addPoint(Vector(1300, 50, -50));
429      trackManager->addPoint(Vector(1400, 40, -50));
430      trackManager->addPoint(Vector(1500, 40, -60));
431      trackManager->addPoint(Vector(1600, 35, -55));
432      trackManager->addPoint(Vector(1700, 45, -40));
433      trackManager->addPoint(Vector(1750, 60, -40));
434      trackManager->addPoint(Vector(1770, 80, -40));
435      trackManager->addPoint(Vector(1800, 100, -40));
436      trackManager->setDuration(10);
437
438      trackManager->finalize();
439
440     
441  lightMan->setAmbientColor(.1,.1,.1);
442  lightMan->addLight();
443  //      lightMan->setAttenuation(1.0, .01, 0.0);
444  //      lightMan->setDiffuseColor(1,1,1);
445  //  lightMan->addLight(1);
446  //  lightMan->setPosition(20, 10, -20);
447  //  lightMan->setDiffuseColor(0,0,0);
448  lightMan->debug();
449  lightMan->setPosition(-5.0, 10.0, -40.0);
450 
451  //        trackManager->setBindSlave(env);
452  PNode* tn = trackManager->getTrackNode();
453  tn->addChild(this->localPlayer);
454 
455  //localCamera->setParent(TrackNode::getInstance());
456  tn->addChild(this->localCamera);
457  localCamera->lookAt(tn);
458  this->localPlayer->setMode(PNODE_ALL);
459  Vector* cameraOffset = new Vector (0, 5, -10);
460  trackManager->condition(2, LEFTRIGHT, this->localPlayer);
461 
462  this->sky->setParent(this->localCamera);
463
464  // initialize debug coord system
465  objectList = glGenLists(1);
466  glNewList (objectList, GL_COMPILE);
467 
468  //  trackManager->drawGraph(.01);
469  trackManager->debug(2);
470  glEndList();
471
472  terrain = new Terrain("worlds/newGround.obj");
473  terrain->setRelCoor(Vector(0,-10,0));
474  this->spawn(terrain);
475
476}
477
478void World::loadDebugWorld(int worldID)
479{
480  /*monitor progress*/
481  this->glmis->step();
482
483  // LIGHT initialisation
484
485  lightMan->setAmbientColor(.1,.1,.1);
486  lightMan->addLight();
487  //      lightMan->setAttenuation(1.0, .01, 0.0);
488  //      lightMan->setDiffuseColor(1,1,1);
489  //  lightMan->addLight(1);
490  //  lightMan->setPosition(20, 10, -20);
491  //  lightMan->setDiffuseColor(0,0,0);
492  lightMan->debug();
493
494  switch(this->debugWorldNr)
495    {
496      /*
497        this loads the hard-coded debug world. this only for simplicity and will be
498        removed by a reald world-loader, which interprets a world-file.
499        if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and
500        make whatever you want...
501      */
502    case DEBUG_WORLD_0:
503      {
504        lightMan->setPosition(-5.0, 10.0, -40.0);
505
506        // !\todo old track-system has to be removed
507
508        //create helper for player
509        //HelperParent* hp = new HelperParent ();
510        /* the player has to be added to this helper */
511
512        // create a player
513        this->localPlayer = new Player ();
514        this->localPlayer->setName ("player");
515        this->spawn (this->localPlayer);
516        /*monitor progress*/
517        //this->glmis->step();
518        this->glmis->step();
519
520        // bind input
521        Orxonox *orx = Orxonox::getInstance ();
522        orx->getLocalInput()->bind (this->localPlayer);
523           
524        // bind camera
525        this->localCamera = new Camera();
526        this->localCamera->setName ("camera");
527           
528        /*monitor progress*/
529        this->glmis->step();
530
531        sky = new SkyBox();
532        //      (SkyBox*)(sky)->setTexture("pictures/sky/skybox", "jpg");
533        sky->setParent(localCamera);
534        this->spawn(sky);
535
536        /*monitor progress*/
537        this->glmis->step();
538
539           
540        WorldEntity* env = new Environment();
541        env->setName ("env");
542        this->spawn(env);
543
544           
545        /*
546          Vector* es = new Vector (10, 5, 0);
547          Quaternion* qs = new Quaternion ();
548          WorldEntity* pr = new Primitive(P_CYLINDER);
549          pr->setName("primitive");
550          this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT);
551        */
552
553        /*monitor progress*/
554        this->glmis->step();
555
556        //          trackManager->setBindSlave(env);
557        PNode* tn = trackManager->getTrackNode();
558        tn->addChild(this->localPlayer);
559        this->localCamera->lookAt(tn);
560
561        //localCamera->setParent(TrackNode::getInstance());
562        tn->addChild(this->localCamera);
563        //          localCamera->lookAt(tn);
564        this->localPlayer->setMode(PNODE_ALL);
565        //Vector* cameraOffset = new Vector (0, 5, -10);
566        trackManager->condition(2, LEFTRIGHT, this->localPlayer);
567        this->glmis->step();
568        break;
569      }
570    case DEBUG_WORLD_1:
571      {
572        lightMan->setPosition(.0, .0, .0);
573        lightMan->setAttenuation(1.0, .01, 0.0);
574        lightMan->setSpecularColor(1,0,0);
575        this->nullParent = NullParent::getInstance ();
576        this->nullParent->setName ("NullParent");
577
578        // create a player
579        WorldEntity* myPlayer = new Player();
580        myPlayer->setName ("player");
581        this->spawn(myPlayer);
582        this->localPlayer = myPlayer;       
583           
584        // bind input
585        Orxonox *orx = Orxonox::getInstance();
586        orx->getLocalInput()->bind (myPlayer);
587           
588        // bind camera
589        this->localCamera = new Camera ();
590        this->localCamera->setName ("camera");
591        this->localCamera->lookAt(LightManager::getInstance()->getLight(0));
592        this->localCamera->setParent(this->localPlayer);
593
594        // Create SkySphere
595        sky = new Skysphere("pictures/sky-replace.jpg");
596        this->localPlayer->addChild(this->sky);
597        this->spawn(this->sky);
598        Vector* es = new Vector (20, 0, 0);
599        Quaternion* qs = new Quaternion ();
600
601        lightMan->getLight(0)->setParent(trackManager->getTrackNode());
602        break;
603      }
604    case DEBUG_WORLD_2:
605      {
606        lightMan->setAmbientColor(.1,.1,.1);
607        lightMan->addLight();
608        lightMan->setPosition(-5.0, 10.0, -40.0);
609        this->nullParent = NullParent::getInstance ();
610        this->nullParent->setName ("NullParent");
611
612        // !\todo old track-system has to be removed
613
614        //create helper for player
615        //HelperParent* hp = new HelperParent ();
616        /* the player has to be added to this helper */
617
618        // create a player
619        this->localPlayer = new Player ();
620        this->localPlayer->setName ("player");
621        this->spawn (this->localPlayer);
622        /*monitor progress*/
623        //this->glmis->step();     
624        this->glmis->step();
625
626        // bind input
627        Orxonox *orx = Orxonox::getInstance ();
628        orx->getLocalInput()->bind (this->localPlayer);
629           
630        // bind camera
631        this->localCamera = new Camera();
632        this->localCamera->setName ("camera");
633        this->localCamera->lookAt(this->localPlayer);
634        this->localCamera->setParent(this->localPlayer);
635           
636        /*monitor progress*/
637        this->glmis->step();
638
639        // Create SkySphere
640        this->sky = new Skysphere("pictures/sky-replace.jpg");
641        this->sky->setName("SkySphere");
642        this->spawn(this->sky);
643        this->localCamera->addChild(this->sky);
644        this->sky->setMode(PNODE_MOVEMENT);
645        /*monitor progress*/
646        this->glmis->step();
647
648
649        WorldEntity* baseNode = new Satellite(Vector(1,0,1), 1.2);
650        this->localPlayer->addChild(baseNode);
651        baseNode->setRelCoor(Vector(10.0, 2.0, 1.0));
652        this->spawn(baseNode);
653
654        WorldEntity* secondNode = new Satellite(Vector(0,0,1), 2.0);
655        baseNode->addChild(secondNode);
656        secondNode->setRelCoor(Vector(0.0, 0.0, 3.0));
657        this->spawn(secondNode);
658
659
660        WorldEntity* thirdNode = new Satellite(Vector(0,0,1), 1.0);
661        secondNode->addChild(thirdNode);
662        thirdNode->setRelCoor(Vector(2.0, 0.0, 0.0));
663        this->spawn(thirdNode);
664
665           
666   
667        WorldEntity* c = new Environment();
668        this->localPlayer->addChild(c);
669        c->setRelCoor(Vector(10.0, 2.0, -1.0));
670        this->spawn(c);
671
672
673           
674        Animation3D* animation = new Animation3D(c);
675        animation->setInfinity(ANIM_INF_REPLAY);
676
677
678        animation->addKeyFrame(Vector(0, 0, 0), Quaternion(0, Vector(0,1,0)), 1.0, ANIM_NEG_EXP, ANIM_LINEAR); 
679        animation->addKeyFrame(Vector(0, 2, 0), Quaternion(M_PI, Vector(0,1,0)), 1.0, ANIM_NEG_EXP, ANIM_LINEAR); 
680        animation->addKeyFrame(Vector(0, 0, 0), Quaternion(0, Vector(0,1,0)), 1.0, ANIM_NEG_EXP, ANIM_LINEAR); 
681
682
683
684
685
686
687        /*         
688          KeyFrame* f1 = new KeyFrame;
689          f1->position = new Vector(-1.1, 0.0, 2.6);
690          f1->direction = new Quaternion();
691          f1->time = 1.0;
692          f1->mode = NEG_EXP;
693                 
694                 
695          KeyFrame* f2 = new KeyFrame;
696          f2->position = new Vector(-2.1, 0.0, 2.6);
697          f2->direction = new Quaternion();
698          f2->time = 0.1;
699          f2->mode = NEG_EXP;
700                 
701          KeyFrame* f3 = new KeyFrame;
702          f3->position = new Vector(10.0, 2.0, -1.0);
703          f3->direction = new Quaternion();
704          f3->time = 0.2;
705          f3->mode = NEG_EXP;
706                 
707          KeyFrame* f4 = new KeyFrame;
708          f4->position = new Vector(10.0, 5.0, -1.0);
709          f4->direction = new Quaternion();
710          f4->time = 1.0;
711          f4->mode = NEG_EXP;
712                 
713                 
714                 
715          this->simpleAnimation->animatorBegin();
716          this->simpleAnimation->selectObject(b);
717          this->simpleAnimation->setAnimationMode(SINGLE);
718          this->simpleAnimation->addKeyFrame(f1);
719          this->simpleAnimation->addKeyFrame(f2);
720          this->simpleAnimation->start();
721          this->simpleAnimation->selectObject(c);
722          this->simpleAnimation->addKeyFrame(f3);
723          this->simpleAnimation->addKeyFrame(f4);
724          this->simpleAnimation->start();
725          this->simpleAnimation->animatorEnd();
726        */
727
728        /*
729          Vector* es = new Vector (10, 5, 0);
730          Quaternion* qs = new Quaternion ();
731          WorldEntity* pr = new Primitive(P_CYLINDER);
732          pr->setName("primitive");
733          this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT);
734        */
735
736        /*monitor progress*/
737        this->glmis->step();
738
739        //          trackManager->setBindSlave(env);
740        PNode* tn = trackManager->getTrackNode();
741        tn->addChild(this->localPlayer);
742
743        //localCamera->setParent(TrackNode::getInstance());
744        tn->addChild(this->localCamera);
745        //          localCamera->lookAt(tn);
746        this->localPlayer->setMode(PNODE_ALL);
747        //Vector* cameraOffset = new Vector (0, 5, -10);
748        trackManager->condition(2, LEFTRIGHT, this->localPlayer);
749        this->glmis->step();
750
751        break;
752      }
753    default:
754      printf("World::load() - no world with ID %i found", this->debugWorldNr );
755    }
756}
757
758
759
760/**
761   \brief initializes a new World shortly before start
762
763   this is the function, that will be loaded shortly before the world is
764   started
765*/
766ErrorMessage World::init()
767{
768  this->bPause = false;
769  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
770  cn->addToWorld(this);
771  cn->enable(true);
772}
773
774
775/**
776   \brief starts the World
777*/
778ErrorMessage World::start()
779{
780  PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr);
781  this->bQuitOrxonox = false;
782  this->bQuitCurrentGame = false;
783  this->mainLoop();
784}
785
786/**
787   \brief stops the world.
788
789   This happens, when the player decides to end the Level.
790*/
791ErrorMessage World::stop()
792{
793  PRINTF(3)("World::stop() - got stop signal\n");
794  this->bQuitCurrentGame = true;
795}
796
797/**
798   \brief pauses the Game
799*/
800ErrorMessage World::pause()
801{
802  this->isPaused = true;
803}
804
805/**
806   \brief ends the pause Phase
807*/
808ErrorMessage World::resume()
809{
810  this->isPaused = false;
811}
812
813/**
814   \brief destroys the World
815*/
816ErrorMessage World::destroy()
817{
818
819}
820
821/**
822   \brief shows the loading screen
823*/
824void World::displayLoadScreen ()
825{
826  PRINTF(3)("World::displayLoadScreen - start\n"); 
827 
828  //GLMenuImageScreen*
829  this->glmis = new GLMenuImageScreen();
830  this->glmis->init();
831  glmis->setBackgroundImage("pictures/load_screen.jpg");
832  this->glmis->setMaximum(8);
833  this->glmis->draw();
834 
835  PRINTF(3)("World::displayLoadScreen - end\n"); 
836}
837
838/**
839   \brief removes the loadscreen, and changes over to the game
840
841   \todo take out the delay
842*/
843void World::releaseLoadScreen ()
844{
845  PRINTF(3)("World::releaseLoadScreen - start\n"); 
846  this->glmis->setValue(this->glmis->getMaximum());
847  PRINTF(3)("World::releaseLoadScreen - end\n"); 
848  delete this->glmis;
849}
850
851
852/**
853   \brief gets the list of entities from the world
854   \returns entity list
855*/
856tList<WorldEntity>* World::getEntities()
857{
858  return this->entities;
859}
860
861
862/**
863   \brief this returns the current game time
864   \returns elapsed game time
865*/
866double World::getGameTime()
867{
868  return this->gameTime;
869}
870
871
872/**
873    \brief checks for collisions
874   
875    This method runs through all WorldEntities known to the world and checks for collisions
876    between them. In case of collisions the collide() method of the corresponding entities
877    is called.
878*/
879void World::collide ()
880{
881  /*
882  List *a, *b;
883  WorldEntity *aobj, *bobj;
884   
885  a = entities;
886 
887  while( a != NULL)
888    {
889      aobj = a->nextElement();
890      if( aobj->bCollide && aobj->collisioncluster != NULL)
891        {
892          b = a->nextElement();
893          while( b != NULL )
894            {
895              bobj = b->nextElement();
896              if( bobj->bCollide && bobj->collisioncluster != NULL )
897                {
898                  unsigned long ahitflg, bhitflg;
899                  if( check_collision ( &aobj->place, aobj->collisioncluster,
900                                        &ahitflg, &bobj->place, bobj->collisioncluster,
901                                        &bhitflg) );
902                  {
903                    aobj->collide (bobj, ahitflg, bhitflg);
904                    bobj->collide (aobj, bhitflg, ahitflg);
905                  }
906                }
907              b = b->nextElement();
908            }
909        }
910      a = a->enumerate();
911    }
912  */
913}
914
915/**
916    \brief runs through all entities calling their draw() methods
917*/
918void World::draw ()
919{
920  /* draw entities */
921  WorldEntity* entity;
922  glLoadIdentity();
923
924  //entity = this->entities->enumerate();
925  tIterator<WorldEntity>* iterator = this->entities->getIterator();
926  entity = iterator->nextElement();
927  while( entity != NULL ) 
928    { 
929      if( entity->bDraw ) entity->draw();
930      //entity = this->entities->nextElement();
931      entity = iterator->nextElement();
932    }
933  delete iterator;
934 
935  glCallList (objectList);
936
937  TextEngine::getInstance()->draw();
938  lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes //
939}
940
941
942/**
943   \brief function to put your own debug stuff into it. it can display informations about
944   the current class/procedure
945*/
946void World::debug()
947{
948  PRINTF(2)("debug() - starting debug\n");
949  PNode* p1 = NullParent::getInstance ();
950  PNode* p2 = new PNode (Vector(2, 2, 2), p1);
951  PNode* p3 = new PNode (Vector(4, 4, 4), p1);
952  PNode* p4 = new PNode (Vector(6, 6, 6), p2);
953
954  p1->debug ();
955  p2->debug ();
956  p3->debug ();
957  p4->debug ();
958
959  p1->shiftCoor (Vector(-1, -1, -1));
960
961  printf("World::debug() - shift\n");
962  p1->debug ();
963  p2->debug ();
964  p3->debug ();
965  p4->debug ();
966 
967  p1->update (0);
968
969  printf ("World::debug() - update\n");
970  p1->debug ();
971  p2->debug ();
972  p3->debug ();
973  p4->debug ();
974
975  p2->shiftCoor (Vector(-1, -1, -1));
976  p1->update (0);
977
978  p1->debug ();
979  p2->debug ();
980  p3->debug ();
981  p4->debug ();
982
983  p2->setAbsCoor (Vector(1,2,3));
984
985
986 p1->update (0);
987
988  p1->debug ();
989  p2->debug ();
990  p3->debug ();
991  p4->debug ();
992
993  delete p1;
994 
995 
996  /*
997  WorldEntity* entity;
998  printf("counting all entities\n");
999  printf("World::debug() - enumerate()\n");
1000  entity = entities->enumerate(); 
1001  while( entity != NULL )
1002    {
1003      if( entity->bDraw ) printf("got an entity\n");
1004      entity = entities->nextElement();
1005    }
1006  */
1007}
1008
1009
1010/**
1011  \brief main loop of the world: executing all world relevant function
1012
1013  in this loop we synchronize (if networked), handle input events, give the heart-beat to
1014  all other member-entities of the world (tick to player, enemies etc.), checking for
1015  collisions drawing everything to the screen.
1016*/
1017void World::mainLoop()
1018{
1019  this->lastFrame = SDL_GetTicks ();
1020  PRINTF(3)("World::mainLoop() - Entering main loop\n");
1021  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
1022    {
1023      PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize());
1024      // Network
1025      this->synchronize ();
1026      // Process input
1027      this->handleInput ();
1028      if( this->bQuitCurrentGame || this->bQuitOrxonox)
1029          break;
1030      // Process time
1031      this->tick ();
1032      // Update the state
1033      this->update ();     
1034      // Process collision
1035      this->collide ();
1036      // Draw
1037      this->display ();
1038
1039      //      for( int i = 0; i < 5000000; i++) {}
1040      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
1041    }
1042  PRINTF(3)("World::mainLoop() - Exiting the main loop\n");
1043}
1044
1045
1046/**
1047   \brief synchronize local data with remote data
1048*/
1049void World::synchronize ()
1050{
1051  // Get remote input
1052  // Update synchronizables
1053}
1054
1055
1056/**
1057   \brief run all input processing
1058
1059   the command node is the central input event dispatcher. the node uses the even-queue from
1060   sdl and has its own event-passing-queue.
1061*/
1062void World::handleInput ()
1063{
1064  // localinput
1065  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
1066  cn->process();
1067  // remoteinput
1068}
1069
1070
1071/**
1072   \brief advance the timeline
1073
1074   this calculates the time used to process one frame (with all input handling, drawing, etc)
1075   the time is mesured in ms and passed to all world-entities and other classes that need
1076   a heart-beat.
1077*/
1078void World::tick ()
1079{
1080  Uint32 currentFrame = SDL_GetTicks();
1081  if(!this->bPause)
1082    {
1083      this->dt = currentFrame - this->lastFrame;
1084     
1085      if( this->dt > 0)
1086        {
1087          float fps = 1000/dt;
1088
1089          // temporary, only for showing how fast the text-engine is
1090          char tmpChar[20];
1091          sprintf(tmpChar, "fps: %4.0f", fps);
1092        }
1093      else
1094        {
1095          /* the frame-rate is limited to 100 frames per second, all other things are for
1096             nothing.
1097          */
1098          PRINTF(2)("fps = 1000 - frame rate is adjusted\n");
1099          SDL_Delay(10);
1100          this->dt = 10;
1101        }
1102      //this->timeSlice (dt);
1103     
1104      /* function to let all entities tick (iterate through list) */
1105      float seconds = this->dt / 1000.0;     
1106      this->gameTime += seconds;
1107      //entity = entities->enumerate();
1108      tIterator<WorldEntity>* iterator = this->entities->getIterator();
1109      WorldEntity* entity = iterator->nextElement();
1110      while( entity != NULL) 
1111        { 
1112          entity->tick (seconds);
1113          entity = iterator->nextElement();
1114        }
1115      delete iterator;
1116
1117      /* update tick the rest */
1118      this->trackManager->tick(this->dt);
1119      this->localCamera->tick(this->dt);
1120      this->garbageCollector->tick(seconds);
1121
1122      AnimationPlayer::getInstance()->tick(seconds);
1123    }
1124  this->lastFrame = currentFrame;
1125}
1126
1127
1128/**
1129   \brief this function gives the world a consistant state
1130
1131   after ticking (updating the world state) this will give a constistant
1132   state to the whole system.
1133*/
1134void World::update()
1135{
1136  this->garbageCollector->update();
1137  this->nullParent->update (dt);
1138}
1139
1140
1141/**
1142   \brief render the current frame
1143   
1144   clear all buffers and draw the world
1145*/
1146void World::display ()
1147{
1148  // clear buffer
1149  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1150  // set camera
1151  this->localCamera->apply ();
1152  // draw world
1153  this->draw();
1154  // draw HUD
1155  /* \todo draw HUD */
1156  // flip buffers
1157  SDL_GL_SwapBuffers();
1158  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
1159  //SDL_Flip (screen);
1160}
1161
1162
1163/**
1164   \brief add and spawn a new entity to this world
1165   \param entity to be added
1166*/
1167void World::spawn(WorldEntity* entity)
1168{
1169  this->entities->add (entity);
1170  entity->postSpawn ();
1171}
1172
1173
1174/**
1175   \brief add and spawn a new entity to this world
1176   \param entity to be added
1177   \param absCoor At what coordinates to add this entity.
1178   \param absDir In which direction should it look.
1179*/
1180void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
1181{
1182  this->entities->add (entity);
1183
1184  entity->setAbsCoor (*absCoor);
1185  entity->setAbsDir (*absDir);
1186
1187  entity->postSpawn ();
1188}
1189
1190
1191/**
1192   \brief add and spawn a new entity to this world
1193   \param entity to be added
1194   \param entity to be added to (PNode)
1195   \param At what relative  coordinates to add this entity.
1196   \param In which relative direction should it look.
1197*/
1198void World::spawn(WorldEntity* entity, PNode* parentNode, 
1199                  Vector* relCoor, Quaternion* relDir, 
1200                  int parentingMode)
1201{
1202  this->nullParent = NullParent::getInstance();
1203  if( parentNode != NULL)
1204    {
1205      parentNode->addChild (entity);
1206     
1207      entity->setRelCoor (*relCoor);
1208      entity->setRelDir (*relDir);
1209      entity->setMode(parentingMode);
1210     
1211      this->entities->add (entity);
1212     
1213      entity->postSpawn ();
1214    }
1215}
1216
1217
1218
1219/**
1220  \brief commands that the world must catch
1221  \returns false if not used by the world
1222*/
1223bool World::command(Command* cmd)
1224{
1225  if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW0)) this->localCamera->setViewMode(VIEW_NORMAL);
1226  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW1)) this->localCamera->setViewMode(VIEW_BEHIND);
1227  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW2)) this->localCamera->setViewMode(VIEW_FRONT);
1228  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW3)) this->localCamera->setViewMode(VIEW_LEFT);
1229  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW4)) this->localCamera->setViewMode(VIEW_RIGHT);
1230  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW5)) this->localCamera->setViewMode(VIEW_TOP);
1231
1232  return false;
1233}
1234
1235void World::setPath( const char* name)
1236{
1237  if (this->path)
1238    delete this->path;
1239  if (ResourceManager::isFile(name))
1240  {
1241    this->path = new char[strlen(name)+1];
1242    strcpy(this->path, name);
1243  }
1244  else
1245    {
1246      this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1];
1247      sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
1248    }
1249}
1250
1251const char* World::getPath( void)
1252{
1253  return path;
1254}
Note: See TracBrowser for help on using the repository browser.