Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/particleEngine: some definitions

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