Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: play/rewind and so on work perfectly. (you can try them out on [1-5] keys
1 play
2 stop
3 pause
4 replay
5 rewind

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