Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/particleEngine: spread works, and many other functions implemented

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