Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cool baking

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