Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now the Text is real sexy, smashing in and smoothly fading out

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