Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3752 was 3752, checked in by patrick, 19 years ago

orxonox/trunk: weapon now works perfectly: it shoots and moves to it. now i will have to change the projectile model itself….

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