Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: animation player is not yet working, but it look more like the desired actions than ever…:)

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