Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/story_entities/world.cc @ 3828

Last change on this file since 3828 was 3828, checked in by patrick, 19 years ago

orxonox/trunk: little chang removing frames and changing signes in substr

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