Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3845 was 3832, checked in by patrick, 20 years ago

orxonox/trunk: animation class functions implemented, list enhanced

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