Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: AnimationPlayer updated, created a debug function, and general functionality

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