Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/particleEngine/src/story_entities/world.cc @ 3942

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

orxonox/branches/particleEngine: minor patches, and now it renders particle-faces

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