Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cleanup of world (moved some stuff into the preload sequence. still there is a lot to do there

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