Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/particleEngine: inherit speed from emitter is now also an option
for this i had to write a new PNode function, getVelocity (patrick: you could also use this one in the shoot-class, maybe).

File size: 26.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 "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, 1);
382            testEmitter->setParent(localPlayer);
383            testEmitter2 = new ParticleEmitter(Vector(-1,0,0), .1, 1200, 1);
384            testEmitter2->setParent(tn);
385
386            testSystem = new ParticleSystem(100000);
387            testSystem->setLifeSpan(3);
388            testSystem->setConserve(.8);
389            testSystem->setInheritSpeed(1);
390
391            ParticleEngine::getInstance()->addConnection(testEmitter, testSystem);
392            ParticleEngine::getInstance()->addConnection(testEmitter2, testSystem);
393
394            break;
395          }
396        case DEBUG_WORLD_1:
397          {
398            lightMan->setPosition(.0, .0, .0);
399            lightMan->setAttenuation(1.0, .01, 0.0);
400            lightMan->setSpecularColor(1,0,0);
401            this->nullParent = NullParent::getInstance ();
402            this->nullParent->setName ("NullParent");
403
404            // create a player
405            WorldEntity* myPlayer = new Player();
406            myPlayer->setName ("player");
407            this->spawn(myPlayer);
408            this->localPlayer = myPlayer;           
409           
410            // bind input
411            Orxonox *orx = Orxonox::getInstance();
412            orx->getLocalInput()->bind (myPlayer);
413           
414            // bind camera
415            this->localCamera = new Camera ();
416            this->localCamera->setName ("camera");
417            this->localCamera->lookAt(LightManager::getInstance()->getLight(0));
418            this->localCamera->setParent(this->localPlayer);
419
420            // Create SkySphere
421            skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
422            this->localPlayer->addChild(this->skySphere);
423            this->spawn(this->skySphere);
424            Vector* es = new Vector (20, 0, 0);
425            Quaternion* qs = new Quaternion ();
426
427            lightMan->getLight(0)->setParent(trackManager->getTrackNode());
428            break;
429          }
430        case DEBUG_WORLD_2:
431          {
432            lightMan->setAmbientColor(.1,.1,.1);
433            lightMan->addLight();
434            lightMan->setPosition(-5.0, 10.0, -40.0);
435            this->nullParent = NullParent::getInstance ();
436            this->nullParent->setName ("NullParent");
437
438            // !\todo old track-system has to be removed
439
440            //create helper for player
441            //HelperParent* hp = new HelperParent ();
442            /* the player has to be added to this helper */
443
444            // create a player
445            this->localPlayer = new Player ();
446            this->localPlayer->setName ("player");
447            this->spawn (this->localPlayer);
448            /*monitor progress*/
449            //this->glmis->step();         
450            this->glmis->step();
451
452            // bind input
453            Orxonox *orx = Orxonox::getInstance ();
454            orx->getLocalInput()->bind (this->localPlayer);
455           
456            // bind camera
457            this->localCamera = new Camera();
458            this->localCamera->setName ("camera");
459            this->localCamera->lookAt(this->localPlayer);
460            this->localCamera->setParent(this->localPlayer);
461           
462            /*monitor progress*/
463            this->glmis->step();
464
465            // Create SkySphere
466            this->skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
467            this->skySphere->setName("SkySphere");
468            this->spawn(this->skySphere);
469            this->localCamera->addChild(this->skySphere);
470            this->skySphere->setMode(PNODE_MOVEMENT);
471            /*monitor progress*/
472            this->glmis->step();
473
474
475            WorldEntity* baseNode = new Satellite(Vector(1,0,1), 1.2);
476            this->localPlayer->addChild(baseNode);
477            baseNode->setRelCoor(Vector(10.0, 2.0, 1.0));
478            this->spawn(baseNode);
479
480            WorldEntity* secondNode = new Satellite(Vector(0,0,1), 2.0);
481            baseNode->addChild(secondNode);
482            secondNode->setRelCoor(Vector(0.0, 0.0, 3.0));
483            this->spawn(secondNode);
484
485
486            WorldEntity* thirdNode = new Satellite(Vector(0,0,1), 1.0);
487            secondNode->addChild(thirdNode);
488            thirdNode->setRelCoor(Vector(2.0, 0.0, 0.0));
489            this->spawn(thirdNode);
490
491           
492           
493
494            WorldEntity* b = new Environment();
495            this->localPlayer->addChild(b);
496            b->setRelCoor(Vector(10.0, 1.0, 1.0));
497            this->spawn(b);
498
499           
500            WorldEntity* c = new Environment();
501            this->localPlayer->addChild(c);
502            c->setRelCoor(Vector(10.0, 2.0, -1.0));
503            this->spawn(c);
504           
505            /*     
506                  KeyFrame* f1 = new KeyFrame;
507                  f1->position = new Vector(-1.1, 0.0, 2.6);
508                  f1->direction = new Quaternion();
509                  f1->time = 1.0;
510                  f1->mode = NEG_EXP;
511                 
512                 
513                  KeyFrame* f2 = new KeyFrame;
514                  f2->position = new Vector(-2.1, 0.0, 2.6);
515                  f2->direction = new Quaternion();
516                  f2->time = 0.1;
517                  f2->mode = NEG_EXP;
518                 
519                  KeyFrame* f3 = new KeyFrame;
520                  f3->position = new Vector(10.0, 2.0, -1.0);
521                  f3->direction = new Quaternion();
522                  f3->time = 0.2;
523                  f3->mode = NEG_EXP;
524                 
525                  KeyFrame* f4 = new KeyFrame;
526                  f4->position = new Vector(10.0, 5.0, -1.0);
527                  f4->direction = new Quaternion();
528                  f4->time = 1.0;
529                  f4->mode = NEG_EXP;
530                 
531                 
532                 
533                  this->simpleAnimation->animatorBegin();
534                  this->simpleAnimation->selectObject(b);
535                  this->simpleAnimation->setAnimationMode(SINGLE);
536                  this->simpleAnimation->addKeyFrame(f1);
537                  this->simpleAnimation->addKeyFrame(f2);
538                  this->simpleAnimation->start();
539                  this->simpleAnimation->selectObject(c);
540                  this->simpleAnimation->addKeyFrame(f3);
541                  this->simpleAnimation->addKeyFrame(f4);
542                  this->simpleAnimation->start();
543                  this->simpleAnimation->animatorEnd();
544            */
545
546            /*
547            Vector* es = new Vector (10, 5, 0);
548            Quaternion* qs = new Quaternion ();
549            WorldEntity* pr = new Primitive(P_CYLINDER);
550            pr->setName("primitive");
551            this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT);
552            */
553
554            /*monitor progress*/
555            this->glmis->step();
556
557            //      trackManager->setBindSlave(env);
558            PNode* tn = trackManager->getTrackNode();
559            tn->addChild(this->localPlayer);
560
561            //localCamera->setParent(TrackNode::getInstance());
562            tn->addChild(this->localCamera);
563            //      localCamera->lookAt(tn);
564            this->localPlayer->setMode(PNODE_ALL);
565            //Vector* cameraOffset = new Vector (0, 5, -10);
566            trackManager->condition(2, LEFTRIGHT, this->localPlayer);
567            this->glmis->step();
568
569            break;
570          }
571        default:
572          printf("World::load() - no world with ID %i found", this->debugWorldNr );
573        }
574    }
575  else if(this->worldName != NULL)
576    {
577
578    }
579
580  // initialize debug coord system
581  objectList = glGenLists(1);
582  glNewList (objectList, GL_COMPILE);
583 
584  //  trackManager->drawGraph(.01);
585  trackManager->debug(2);
586  glEndList();
587
588  terrain = new Terrain("../data/worlds/newGround.obj");
589  terrain->setRelCoor(Vector(0,-10,0));
590  this->spawn(terrain);
591
592}
593
594
595/**
596   \brief initializes a new World shortly before start
597
598   this is the function, that will be loaded shortly before the world is
599   started
600*/
601ErrorMessage World::init()
602{
603  this->bPause = false;
604  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
605  cn->addToWorld(this);
606  cn->enable(true);
607}
608
609
610/**
611   \brief starts the World
612*/
613ErrorMessage World::start()
614{
615  PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr);
616  this->bQuitOrxonox = false;
617  this->bQuitCurrentGame = false;
618  this->mainLoop();
619}
620
621/**
622   \brief stops the world.
623
624   This happens, when the player decides to end the Level.
625*/
626ErrorMessage World::stop()
627{
628  PRINTF(3)("World::stop() - got stop signal\n");
629  this->bQuitCurrentGame = true;
630}
631
632/**
633   \brief pauses the Game
634*/
635ErrorMessage World::pause()
636{
637  this->isPaused = true;
638}
639
640/**
641   \brief ends the pause Phase
642*/
643ErrorMessage World::resume()
644{
645  this->isPaused = false;
646}
647
648/**
649   \brief destroys the World
650*/
651ErrorMessage World::destroy()
652{
653
654}
655
656/**
657   \brief shows the loading screen
658*/
659void World::displayLoadScreen ()
660{
661  PRINTF(3)("World::displayLoadScreen - start\n"); 
662 
663  //GLMenuImageScreen*
664  this->glmis = GLMenuImageScreen::getInstance();
665  this->glmis->init();
666  this->glmis->setMaximum(8);
667  this->glmis->draw();
668 
669  PRINTF(3)("World::displayLoadScreen - end\n"); 
670}
671
672/**
673   \brief removes the loadscreen, and changes over to the game
674
675   \todo take out the delay
676*/
677void World::releaseLoadScreen ()
678{
679  PRINTF(3)("World::releaseLoadScreen - start\n"); 
680  this->glmis->setValue(this->glmis->getMaximum());
681  //SDL_Delay(500);
682  PRINTF(3)("World::releaseLoadScreen - end\n"); 
683}
684
685
686/**
687   \brief gets the list of entities from the world
688   \returns entity list
689*/
690tList<WorldEntity>* World::getEntities()
691{
692  return this->entities;
693}
694
695
696/**
697   \brief this returns the current game time
698   \returns elapsed game time
699*/
700double World::getGameTime()
701{
702  return this->gameTime;
703}
704
705
706/**
707    \brief checks for collisions
708   
709    This method runs through all WorldEntities known to the world and checks for collisions
710    between them. In case of collisions the collide() method of the corresponding entities
711    is called.
712*/
713void World::collide ()
714{
715  /*
716  List *a, *b;
717  WorldEntity *aobj, *bobj;
718   
719  a = entities;
720 
721  while( a != NULL)
722    {
723      aobj = a->nextElement();
724      if( aobj->bCollide && aobj->collisioncluster != NULL)
725        {
726          b = a->nextElement();
727          while( b != NULL )
728            {
729              bobj = b->nextElement();
730              if( bobj->bCollide && bobj->collisioncluster != NULL )
731                {
732                  unsigned long ahitflg, bhitflg;
733                  if( check_collision ( &aobj->place, aobj->collisioncluster,
734                                        &ahitflg, &bobj->place, bobj->collisioncluster,
735                                        &bhitflg) );
736                  {
737                    aobj->collide (bobj, ahitflg, bhitflg);
738                    bobj->collide (aobj, bhitflg, ahitflg);
739                  }
740                }
741              b = b->nextElement();
742            }
743        }
744      a = a->enumerate();
745    }
746  */
747}
748
749/**
750    \brief runs through all entities calling their draw() methods
751*/
752void World::draw ()
753{
754  /* draw entities */
755  WorldEntity* entity;
756  glLoadIdentity();
757
758  //entity = this->entities->enumerate();
759  tIterator<WorldEntity>* iterator = this->entities->getIterator();
760  entity = iterator->nextElement();
761  while( entity != NULL ) 
762    { 
763      if( entity->bDraw ) entity->draw();
764      //entity = this->entities->nextElement();
765      entity = iterator->nextElement();
766    }
767  delete iterator;
768 
769  glCallList (objectList);
770
771  TextEngine::getInstance()->draw();
772
773  ParticleEngine::getInstance()->draw();
774
775  lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes //
776}
777
778
779/**
780   \brief function to put your own debug stuff into it. it can display informations about
781   the current class/procedure
782*/
783void World::debug()
784{
785  PRINTF(2)("debug() - starting debug\n");
786  PNode* p1 = NullParent::getInstance ();
787  PNode* p2 = new PNode (Vector(2, 2, 2), p1);
788  PNode* p3 = new PNode (Vector(4, 4, 4), p1);
789  PNode* p4 = new PNode (Vector(6, 6, 6), p2);
790
791  p1->debug ();
792  p2->debug ();
793  p3->debug ();
794  p4->debug ();
795
796  p1->shiftCoor (Vector(-1, -1, -1));
797
798  printf("World::debug() - shift\n");
799  p1->debug ();
800  p2->debug ();
801  p3->debug ();
802  p4->debug ();
803 
804  p1->update (0);
805
806  printf ("World::debug() - update\n");
807  p1->debug ();
808  p2->debug ();
809  p3->debug ();
810  p4->debug ();
811
812  p2->shiftCoor (Vector(-1, -1, -1));
813  p1->update (0);
814
815  p1->debug ();
816  p2->debug ();
817  p3->debug ();
818  p4->debug ();
819
820  p2->setAbsCoor (Vector(1,2,3));
821
822
823 p1->update (0);
824
825  p1->debug ();
826  p2->debug ();
827  p3->debug ();
828  p4->debug ();
829
830  delete p1;
831 
832 
833  /*
834  WorldEntity* entity;
835  printf("counting all entities\n");
836  printf("World::debug() - enumerate()\n");
837  entity = entities->enumerate(); 
838  while( entity != NULL )
839    {
840      if( entity->bDraw ) printf("got an entity\n");
841      entity = entities->nextElement();
842    }
843  */
844}
845
846
847/**
848  \brief main loop of the world: executing all world relevant function
849
850  in this loop we synchronize (if networked), handle input events, give the heart-beat to
851  all other member-entities of the world (tick to player, enemies etc.), checking for
852  collisions drawing everything to the screen.
853*/
854void World::mainLoop()
855{
856  this->lastFrame = SDL_GetTicks ();
857  PRINTF(3)("World::mainLoop() - Entering main loop\n");
858  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
859    {
860      PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize());
861      // Network
862      this->synchronize ();
863      // Process input
864      this->handleInput ();
865      if( this->bQuitCurrentGame || this->bQuitOrxonox)
866          break;
867      // Process time
868      this->tick ();
869      // Update the state
870      this->update ();     
871      // Process collision
872      this->collide ();
873      // Draw
874      this->display ();
875
876      //      for( int i = 0; i < 5000000; i++) {}
877      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
878    }
879  PRINTF(3)("World::mainLoop() - Exiting the main loop\n");
880}
881
882
883/**
884   \brief synchronize local data with remote data
885*/
886void World::synchronize ()
887{
888  // Get remote input
889  // Update synchronizables
890}
891
892
893/**
894   \brief run all input processing
895
896   the command node is the central input event dispatcher. the node uses the even-queue from
897   sdl and has its own event-passing-queue.
898*/
899void World::handleInput ()
900{
901  // localinput
902  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
903  cn->process();
904  // remoteinput
905}
906
907
908/**
909   \brief advance the timeline
910
911   this calculates the time used to process one frame (with all input handling, drawing, etc)
912   the time is mesured in ms and passed to all world-entities and other classes that need
913   a heart-beat.
914*/
915void World::tick ()
916{
917  Uint32 currentFrame = SDL_GetTicks();
918  if(!this->bPause)
919    {
920      this->dt = currentFrame - this->lastFrame;
921     
922      if( this->dt > 0)
923        {
924          float fps = 1000/dt;
925
926          // temporary, only for showing how fast the text-engine is
927          char tmpChar[20];
928          sprintf(tmpChar, "fps: %4.0f", fps);
929        }
930      else
931        {
932          /* the frame-rate is limited to 100 frames per second, all other things are for
933             nothing.
934          */
935          PRINTF(2)("fps = 1000 - frame rate is adjusted\n");
936          SDL_Delay(10);
937          this->dt = 10;
938        }
939      //this->timeSlice (dt);
940     
941      /* function to let all entities tick (iterate through list) */
942      float seconds = this->dt / 1000.0;     
943      this->gameTime += seconds;
944      //entity = entities->enumerate();
945      tIterator<WorldEntity>* iterator = this->entities->getIterator();
946      WorldEntity* entity = iterator->nextElement();
947      while( entity != NULL) 
948        { 
949          entity->tick (seconds);
950          entity = iterator->nextElement();
951        }
952      delete iterator;
953      //skySphere->updatePosition(localCamera->absCoordinate);
954     
955      /* update tick the rest */
956      this->trackManager->tick(this->dt);
957      this->localCamera->tick(this->dt);
958      this->garbageCollector->tick(seconds);
959
960      AnimationPlayer::getInstance()->tick(seconds);
961
962      ParticleEngine::getInstance()->tick(seconds);
963    }
964  this->lastFrame = currentFrame;
965}
966
967
968/**
969   \brief this function gives the world a consistant state
970
971   after ticking (updating the world state) this will give a constistant
972   state to the whole system.
973*/
974void World::update()
975{
976  this->garbageCollector->update();
977  this->nullParent->update (dt);
978}
979
980
981/**
982   \brief render the current frame
983   
984   clear all buffers and draw the world
985*/
986void World::display ()
987{
988  // clear buffer
989  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
990  // set camera
991  this->localCamera->apply ();
992  // draw world
993  this->draw();
994  // draw HUD
995  /* \todo draw HUD */
996  // flip buffers
997  SDL_GL_SwapBuffers();
998  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
999  //SDL_Flip (screen);
1000}
1001
1002
1003/**
1004   \brief add and spawn a new entity to this world
1005   \param entity to be added
1006*/
1007void World::spawn(WorldEntity* entity)
1008{
1009  this->entities->add (entity);
1010  entity->postSpawn ();
1011}
1012
1013
1014/**
1015   \brief add and spawn a new entity to this world
1016   \param entity to be added
1017   \param absCoor At what coordinates to add this entity.
1018   \param absDir In which direction should it look.
1019*/
1020void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
1021{
1022  this->entities->add (entity);
1023
1024  entity->setAbsCoor (*absCoor);
1025  entity->setAbsDir (*absDir);
1026
1027  entity->postSpawn ();
1028}
1029
1030
1031/**
1032   \brief add and spawn a new entity to this world
1033   \param entity to be added
1034   \param entity to be added to (PNode)
1035   \param At what relative  coordinates to add this entity.
1036   \param In which relative direction should it look.
1037*/
1038void World::spawn(WorldEntity* entity, PNode* parentNode, 
1039                  Vector* relCoor, Quaternion* relDir, 
1040                  int parentingMode)
1041{
1042  this->nullParent = NullParent::getInstance();
1043  if( parentNode != NULL)
1044    {
1045      parentNode->addChild (entity);
1046     
1047      entity->setRelCoor (*relCoor);
1048      entity->setRelDir (*relDir);
1049      entity->setMode(parentingMode);
1050     
1051      this->entities->add (entity);
1052     
1053      entity->postSpawn ();
1054    }
1055}
1056
1057
1058
1059/**
1060  \brief commands that the world must catch
1061  \returns false if not used by the world
1062*/
1063bool World::command(Command* cmd)
1064{
1065  if( !strcmp( cmd->cmd, "view0")) this->localCamera->setViewMode(VIEW_NORMAL);
1066  else if( !strcmp( cmd->cmd, "view1")) this->localCamera->setViewMode(VIEW_BEHIND);
1067  else if( !strcmp( cmd->cmd, "view2")) this->localCamera->setViewMode(VIEW_FRONT);
1068  else if( !strcmp( cmd->cmd, "view3")) this->localCamera->setViewMode(VIEW_LEFT);
1069  else if( !strcmp( cmd->cmd, "view4")) this->localCamera->setViewMode(VIEW_RIGHT);
1070  else if( !strcmp( cmd->cmd, "view5")) this->localCamera->setViewMode(VIEW_TOP);
1071
1072  return false;
1073}
1074
Note: See TracBrowser for help on using the repository browser.