Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/particleEngine: some minor fixes

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