Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: return value of quat-slerp was wrong

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